Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

programming doubt for pic with RTC in HITECH-C

Status
Not open for further replies.

azarutz

Member level 2
Joined
Mar 4, 2012
Messages
42
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
India
Activity points
1,579
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.
timekeeper register.png
 

@ararutz

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

Why are you not using hardware feature to free i2c bus. Attached are the working code that i always use to Read/Write data of DS1307. Enjoy!
 

Attachments

  • ds1307.c.txt
    3.3 KB · Views: 61
  • ds1307.h.txt
    529 bytes · Views: 56
  • i2c.c.txt
    1.2 KB · Views: 77
  • i2c.h.txt
    1.6 KB · Views: 67
@ararutz

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.
 
  • Like
Reactions: zia

    zia

    Points: 2
    Helpful Answer Positive Rating
@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 ?
 

Attachments

  • rtc.txt
    3.9 KB · Views: 78
Last edited:

Hi,
My initilization procedure goes like that.

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
 
Last edited:
  • Like
Reactions: zia

    zia

    Points: 2
    Helpful Answer Positive Rating
@babar_ali
Hi, same result it didn't read from rtc and date only updated in rtc not time.
 

@babar_ali
Hi i got output . Thanks for help . can you tell me why we clear watch dog timer when slave sends ack .
 

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...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top