Hi i am writting program for rtc (ds 1307) to display it in lcd using pic 16f877a controller .i read ds 1307 datasheet it stated that if we want to write in timekeeper register then we should give BCD value . 50 sec in dec is written as 01010000 in bcd so it requires 8 free bit in a register to update but in sec register it has one clock halt bit(CH),three bit for 10 seconds and four bit for secondas i couldn't understand the conversion please help me. I attached timekeeper register of 1307 .
and in pic i2c programming there is a i2c wait routine every time we start and stop we wait some time to get the bus if it free . i dono what i put in wait routine .is whether i put some approximate delay like 10ms or any flag bit till it clear . i searched some code and i didn't understand what actually they did for wait routine.please help me to clear my doubt.
See there are 8 bits for seconds. The max value for seconds bcd is 59 which is 101 1001. The bit 0 to bit 3 will take the value 1001 and bit 6 to bit 4 will take the value 101.
and in pic i2c programming there is a i2c wait routine every time we start and stop we wait some time to get the bus if it free . i dono what i put in wait routine .is whether i put some approximate delay like 10ms or any flag bit till it clear . i searched some code and i didn't understand what actually they did for wait routine.please help me to clear my doubt. View attachment 85035
See there are 8 bits for seconds. The max value for seconds bcd is 59 which is 101 1001. The bit 0 to bit 3 will take the value 1001 and bit 6 to bit 4 will take the value 101.
Hi thanks for reply , in time keeper register they mention about 10 seconds for bit 6 to bit 4 and also 10 min (3-bit) fot bit 6 to bit 4 . i didn't understand that conversion. actually it counts from 0 to 5 it only takes 5 seconds how 10? and also in month?
- - - Updated - - -
Hi babar_ali
thanks for code it used a lot to understand about i2c .
Code:
sec &= 0x7F;
min &= 0x7F;
hr &= (0x7F|0x40);
i cant understand these conversion please explain me .
Hi thanks for reply , in time keeper register they mention about 10 seconds for bit 6 to bit 4 and also 10 min (3-bit) fot bit 6 to bit 4 . i didn't understand that conversion. actually it counts from 0 to 5 it only takes 5 seconds how 10? and also in month?
Hi babar_ali
thanks for code it used a lot to understand about i2c .
Code:
sec &= 0x7F;
min &= 0x7F;
hr &= (0x7F|0x40);
i cant understand these conversion please explain me .
I think you need to study the very basic concept of Digital Logic Design i.e. BCD numbers. Whenever in you are in BCD you cannot go pass 9 (1001). If youa are going above 9 then first study how to add subtract BCD's. Also you wrote "in time keeper register they mention about 10 seconds for bit 6 to bit 4". This 10 sec means that for every over flow of BCD lower nibble it will increment to 1. This means that if lower nibble (speaking for seconds) is overflown 5 times and One minute is over. This also means that the upper nibble is keeping track of max value i.e. (5).
The chunk of code you copied is that when ever you push some value to the function it should not exceed its max value. its a safety that one should not try to put 255 in seconds register. I hope you got the idea.
@babar_ali
Hi thanks for quick explanation that was helpfull .Now i understood the timekeeper register that is lower nibble increments every second but upper nibble increments one time after 10 seconds that was great . Can you attach your main function ? I am sure it will fix my other confussion too .
Hi,
what are your confusion? discuss! I'll not provide the main function code. All you have to do is to call functions from ds1307.c code. Try to write your self.
@babar_ali
It seems that controller didn't read from rtc and it writes date into rtc correctly but not time.my lcd displaying 0:0:0 for time and date.is there i missing anything in code ? and why wdt wants to clear ?
I2Cinitilise();
ds1307_get_time(); // By default you will get 12:0:0
ds1307_get_date(); // By default you will get 01:01:0, dow =1
//write the same data as in to ds1307 again
ds1307_set_time(hr,min,sec);
ds1307_set_date(date, mth, year, day);
Tell me the results. Try to put value as i did in dispaly function
Hi, I copied that chunk of code from one of my firmware. Watch dog was used there if it does not respond it will reset the hardware (microcontroller) as the processor will be stuck at "while(!SSPIF);" location i.e worst case condition... other wise system will continue...