Thread: Interface DS1302(RTC) with 8051 to run time on lcd. Time run but got Problem,help pls

  1. #16
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    In DS1302InputByte and DS1302OutputByte functions, you set DS1302_CLK to 1, then the next command, you set it to 0. You need to put a delay in between those two commands. A delay loop like you used in your other code should work, just make sure you loop enough times to satisfy the timing requirement.

    Also, as for your array confusion, you really need to find any class notes and textbooks you have for programming C, and read through the array sections carefully. Do all the examples in the textbook. Find some tutorials on line: we have one here, and Google will turn up dozens more. Write some simple programs using arrays, but not on your embedded device, on your PC where it's easy to test and debug.

  2. #17
    Registered User
    Join Date
    Apr 2011
    Posts
    21

    thank

    Hi,
    yeah,i did think about what you explain.
    The hint you gave saying that it is not 7 or 9.
    So i think it should be 8,am i right?
    Well i have change it to 8 for the array but the problem still remain there.

    even i dont put array there. i change the code to print out "Time:" on the top row then follow by that running time on the second row. But that 85 problem still exist too. I show the code that i change below. Pls,let me know what else i can do. I really want to solve this error. I trying hard now,pls guide me. Thx

    Code:
    void LCD_TimerDisp()
    {
    	unsigned char alphabet;
    	
        for(alphabet=0; alphabet<5; alphabet++)
    	{
    		LCD_print(msg1[alphabet]);
    	}
    
    	do
    	{
    	DS1302_GetTime(&CurrentTime);
    	TimeToStr(&CurrentTime);
    	DateToStr(&CurrentTime);
    	LCD_command(0xc0);
    	for(alphabet=0; alphabet<8; alphabet++)
    	{
    		LCD_print(CurrentTime.TimeString[alphabet]);
    	}
    	LCD_command(0x06);
        for(alphabet=0; alphabet<5; alphabet++)
    	{
    		LCD_print(msg1[alphabet]);
    	}
    	delay(1000);
    	}
    	while(1);
    }
    For the msg1 ,i have define it as "time:"

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, so we've established you can reliably print text to the LCD. You have no problem printing a string like "time:". That means that all the code you wrote is fine. The problem is with the DS1302 code. Just because you found it on some website doesn't mean it's correct or you can't change it. The reason I brought up the timing thing is because if the guy who wrote it was working on a 8 MHz chip and you're working on a 40 MHz chip, your processor is 5x faster, meaning each pulse you send on DS1302_CLK is 1/5 the time. What was sufficient for the clock on his slow processor might not be on your faster processor. That's why I suggested the delay loop:
    Code:
    void DS1302InputByte(unsigned char d) 	//实时时钟写入一字节(内部函数) 
    {  
        unsigned char i; 
        ACC = d; 
        for(i=8; i>0; i--) 
        { 
            DS1302_IO = ACC0;           	//相当于汇编中的 RRC 
            DS1302_CLK = 1; 
            delay(10);  // delay 10 units
            DS1302_CLK = 0; 
            ACC = ACC >> 1;  
        }  
    } 
     
    unsigned char DS1302OutputByte(void) 	//实时时钟读取一字节(内部函数) 
    {  
        unsigned char i; 
        for(i=8; i>0; i--) 
        { 
            ACC = ACC >>1;         			//相当于汇编中的 RRC  
            ACC7 = DS1302_IO; 
            DS1302_CLK = 1; 
            delay(10);  // delay 10 units
            DS1302_CLK = 0; 
        }  
        return(ACC);  
    }
    Note, I'm not sure this is the problem, but it can't hurt to try at this point. I also don't know what an acceptable delay value is. Maybe it's 10, maybe 1000. You'll have to play around with it, and it might not work at all. Other than that, I don't know what to tell you. Perhaps there's some wiring issue with your DS1302. It doesn't look like it, but the schematic in your other post is hard to read.

  4. #19
    Registered User
    Join Date
    Apr 2011
    Posts
    21

    Thank

    Well,thank for re-commanding me to add delay at the input and output areas.
    I have tried that but the LCD print " 00:00:00" now. So i have remove the delay.

    What can i do to solve the 85 problem?? I have try many ways but non of it works. The 85 still jump about when the timing is run.
    What i know is the RTC chip should not have problem because the time can run.
    Any suggestions? I will try and see what i can do about it.
    Thanks.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does this mean you've done something like this, to isolate the problem area?
    Code:
    void LCD_TimerDisp()
    {
        unsigned char alphabet;
        unsigned char nonchar[8];
        
        for(alphabet=0; alphabet<8; alphabet++)
        {
            nonchar[alphabet] = ' ';
        }
        
        do
        {
    //        DS1302_GetTime(&CurrentTime);
    //        TimeToStr(&CurrentTime);
    //        DateToStr(&CurrentTime);
            //!! If this doesn't work, there is something wrong
            //!! with your display code.
            CurrentTime.TimeString[0] = '1';
            CurrentTime.TimeString[1] = '2';
            CurrentTime.TimeString[2] = ':';
            CurrentTime.TimeString[3] = '3';
            CurrentTime.TimeString[4] = '4';
            CurrentTime.TimeString[5] = ':';
            CurrentTime.TimeString[6] = '5';
            CurrentTime.TimeString[7] = '6';
            LCD_command(0xc0);
            for(alphabet=0; alphabet<8; alphabet++)
            {
                LCD_print(CurrentTime.TimeString[alphabet]);
            }
            LCD_command(0x06);
            for(alphabet=0; alphabet<8; alphabet++)
            {
                LCD_print(nonchar[alphabet]);
            }
            delay(1000);
        }
        while(1);
    }
    
    
    
    void LCD_TimerDisp()
    {
        unsigned char alphabet;
        unsigned char nonchar[8];
        
        for(alphabet=0; alphabet<8; alphabet++)
        {
            nonchar[alphabet] = ' ';
        }
        
        do
        {
    //        DS1302_GetTime(&CurrentTime);
            //!! If this works, there is something wrong with your
            //!! DS1302_GetTime input code.
            CurrentTime.Hour = 12;
            CurrentTime.Minute = 34;
            CurrentTime.Second = 56;
            TimeToStr(&CurrentTime);
            DateToStr(&CurrentTime);
            LCD_command(0xc0);
            for(alphabet=0; alphabet<8; alphabet++)
            {
                LCD_print(CurrentTime.TimeString[alphabet]);
            }
            LCD_command(0x06);
            for(alphabet=0; alphabet<8; alphabet++)
            {
                LCD_print(nonchar[alphabet]);
            }
            delay(1000);
        }
        while(1);
    }
    If so, what have you discovered?

    It might be suspected that you're not allowing enough time in your various short delays to reliably read data from devices (or write data to them).
    You've got a whole second before the display changes, so you can affort to be quite leisurely (say 50 milliseconds per read) to slowly gather all the data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Apr 2011
    Posts
    21

    Hi,thanks

    I have tried both. Both of them works. So there is a problem with my DS1302_GetTime input code??
    I don't understand what you mean by 50ms, i mean the delay.
    So is it something wrong with my delay now? I have to add in delay or change the length of it??
    Sorry,i don't really get what you mean.
    can you tell me what should i try or do now??

    Thanks.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read post #18 again

    I think you need to slow down the reading of the clock in some way.

    It takes time to flip from output to input, or input to output.
    Having output a request for data, it can take time for the data to become valid.

    Bear this in mind when you're trying to manipulate the hardware. Getting garbage is a really good sign that you're doing something too fast.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Apr 2011
    Posts
    21
    Hello,
    I have tried what i was told to do in post #18.
    After i add delay in the DS1302InputByte and DS1302OutputByte,
    the Lcd only displayed " 00:00:00 " & the time did not run at all.
    I have also tried to play around by changing the length of the delay
    but it wont make also different at all.

    Well i not sure why that happen. Like what you have say, i think i understand what you mean.
    Is there any other place which you think i can add in delay or any code to change??

    Thanks

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look at all the code which runs when you call DS1302_GetTime() in detail.

    Make sure you really understand what each line of code is doing.

    For each line of code which is modifying the hardware in some way (any port access), make sure you also read the specs for whatever it is that is being accessed. Focus especially on any timing requirements. If times are too short, or two long, then you're likely to be getting junk data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-04-2011, 03:20 PM
  2. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  3. Converting Zulu Time to Current Time
    By Caldus in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 08:54 PM
  4. Replies: 3
    Last Post: 06-13-2003, 06:47 AM