ds1307 drives me crazy..

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
hii,

i am finding problem with ds1307. it is interfaced to at89c51. the code
is as follows:

void main()
{
init_timer(); //for lcd delay
init_lcd();
set_time();
while(1)
    display_time();
}

void set_time()
{
rtc_start();
shiftout_rtc(RTC_WRITE);
shiftout_rtc(0);
shiftout_rtc(0x20);
shiftout_rtc(0x00); //0x00 has no effect, on reading, i get
            //30 for minutes irrespective of the
        //value i send here.
rtc_stop();
}

void display_time()
{
int i, j;
char disp_buffer[2], seconds, minutes;
rtc_start();
shiftout_rtc(RTC_WRITE);
shiftout_rtc(0);
rtc_start();
shiftout_rtc(RTC_READ);
seconds = shiftin_rtc()&0x7f;
send_ack();
minutes = shiftin_rtc()&0x7f;
send_nack();
rtc_stop();
pass_cmd(0x80);
sprintf(disp_buffer, "%x", seconds);
for(j = 0; j < 2; j++)
        {
        pass_data(disp_buffer[j]);
        }
pass_data(':');
sprintf(disp_buffer, "%x", minutes);
for(j = 0; j < 2; j++)
        {
        pass_data(disp_buffer[j]);
        }
}

void send_ack()
{
SCL_RTC = 1;
delay_ep(100);
SDA_RTC = 0;
delay_ep(100);
SCL_RTC = 0;
delay_ep(100);
}

void send_nack()
{

SCL_RTC = 1;
delay_ep(100);
SDA_RTC = 1;
delay_ep(100);
SCL_RTC = 0;
delay_ep(100);
}

initial value got from reading the rtc is correct in case of seconds.
it begins at 20, but increments bit slowly, in about 2 seconds.  after
60, it becomes 0, then 10, 20, etc.  also, minutes starts at 30
irrespective of the value i send, and doesn't increment properly.
i use 32.768 watch xtal.  there is no problem with rtc_start(),
shiftout_rtc(), or rtc_stop() as they worked fine with an eeprom
application i did earlier. i guess the trouble is with setting/reading
the rtc, but i cant locate exactly.

any help will be appreciated. thanx


ds1307 drives me crazy..(addentum)
hii folks,
even the seconds do not work properly. ofcourse, it increments every
second, but starts from 20 irrespective of the value i pass.



DigitalBug wrote:
Quoted text here. Click to load it


Re: ds1307 drives me crazy..
I don't see any code for converting between BCD format and binary.
This can cause very odd appearing values.

wrote:

Quoted text here. Click to load it

-

Re: ds1307 drives me crazy..

hii,

Nigel Thomson wrote:
Quoted text here. Click to load it

the code i wrote for sending data into ds1307 is as follows:

void shiftout_rtc(char c)
{
int i;
shftData_rtc=c;
for(i=0;i<=7;i++)
    {
    SDA_RTC=shftoutbit_rtc;
    delay_ep(100);
    SCL_RTC=1;
    delay_ep(100);
    SCL_RTC=0;
    delay_ep(100);
    shftData_rtc <<= 1;
    }
while(1) //this is waiting for ACK
    {
    SCL_RTC = 1;
    delay_ep(100);
    if(!SDA_RTC)
        {
        SCL_RTC = 0;
        delay_ep(100);
        break;
        }
    SCL_RTC = 0;
    delay_ep(100);
    }
}

for reading data sent by ds1307, i wrote the following code:

char shiftin_rtc()
{
char i;
shftData_rtc=0;
for(i=0;i<=7;i++)
    {
    shftData_rtc <<=1;
    SCL_RTC=1;
    delay_ep(100);
    shftinbit_rtc=SDA_RTC;
    delay_ep(100);
    SCL_RTC=0;
    delay_ep(100);
    }
return(shftData_rtc);
}

the chars i get as return value from the above function is displayed as
minutes and seconds in the lcd by following code:

void display_time()
{
int i, j;
char disp_buffer[2], seconds, minutes;
rtc_start();
shiftout_rtc(RTC_WRITE);
shiftout_rtc(0);
rtc_start();
shiftout_rtc(RTC_READ);
seconds = shiftin_rtc()&0x7f;
send_ack();
minutes = shiftin_rtc()&0x7f;
send_nack();
rtc_stop();
pass_cmd(0x80);
sprintf(disp_buffer, "%x", seconds);
for(j = 0; j < 2; j++)
        {
        pass_data(disp_buffer[j]);
        }
pass_data(' ');
sprintf(disp_buffer, "%x", minutes);
for(j = 0; j < 2; j++)
        {
        pass_data(disp_buffer[j]);
        }
}

here, i simply display the hex value i get.  eg, if i get seconds as
0x10, i display 10 on the lcd, similarly for minutes.

i observe that seconds change every second and minutes change every
minute, but the values are not correct.

i guess there is some manipulation to be done with the received chars,
but i dont know what to do.  any help will be appreciated.

thanx,
sharaf


Re: ds1307 drives me crazy..
Hi,

DigitalBug schrieb:

Quoted text here. Click to load it
 > ...
Quoted text here. Click to load it

I found that many RTCs are upset when you access them too often.
Limit your reads to once per seconds or so, and see what happens.

HTH
Wolfgang

--
From-address is Spam trap
Use: wolfgang (dot) mahringer (at) sbg (dot) at


Re: ds1307 drives me crazy..

Quoted text here. Click to load it

hi friends,

that was the problem.  when i access the rtc only when required, things
get displayed correctly.  thanx folks for your help.

sharaf


Site Timeline