Vous êtes sur la page 1sur 19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary

Login

Register

FAQ

Search

Itiscurrently20Oct201519:31

Viewunansweredposts|Viewactivetopics
BoardindexCompilersmikroCCompilersOlderversionsofmikroCCompilersmikroCforPICmikroCGeneral

AlltimesareUTC+1hour

DS1307MikroCLibrary
Page1of3 [44posts]

Gotopage1,2,3Next

Printview

Previoustopic|Nexttopic

Author
halvarezo

Message
Postsubject:DS1307MikroCLibrary

Posted:29Aug200800:24

Is there any DS1307 MikroC library working? It could help me developing my new project which is based on a RTC. Thanks.
Joined:28Aug200805:42
Posts:5
Top

dronology

Joined:29Dec200723:48
Posts:106
Location:istanbul

Postsubject:

Posted:29Aug200808:48

Sometime ago I red one of the embedded codedevelopers (//I dont remember his/her name at the moment, sorry for
that) code. It was written about widely used "PCF8530". By a little inspiration and reading datasheet I've my own DS1370
code. Here it is;

Code:Selectall

/********************************************************
*RealTimeClockDS1307Libraryfor18F4620*
*(forPICIC.)*
*byDenizELMASLI*
*Version2.0*
*CreationDate:28.12.200810:12Friday*
*RevisionDate:08.06.200803:42Saturday*
*Ide(s):Code::Blocksv1.0(C++developement)*
*MikroCV7.0*
*TargetPlatform:PIC*
*MCU:18F4620*
*Contact:elmasli@gmail.com*
*Web:www.dronology.com*
**
*ThiscodeinintendedtoworkwithPICIC.This*
*sourcecodecontainsfunctionstodriveDS1307real*
*timeclock.I2CFunctionsusedinver2.0*
*******************************************************
*/

voidstart1307s();
voidds1307_init();
voidds1307_set_date_time(charday,charmth,charyear,chardow,charhr,charmin,intsec);
voidds1307_get_time_date(char*day,char*mth,char*year,char*dow,char*hr,char*min,char*sec);

voidds1307_init(){
intseconds=0;
I2C_Start();

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

1/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
I2C_Wr(0xD0);//WRtoRTC
I2C_Wr(0x00);//REG0
I2C_Start();
I2C_Wr(0xD1);//RDfromRTC
seconds=Bcd2Dec(I2C_Rd(0));//Readcurrent"seconds"inDS1307
I2C_Stop();
seconds=(seconds&0x7F);
Delay_ms(50);
I2C_Start();
I2C_Wr(0xD0);//WRtoRTC
I2C_Wr(0x00);//REG0
I2C_Wr(Dec2Bcd(seconds));//Startoscillatorwithcurrent"secondsvalue
I2C_Start();
I2C_Wr(0xD0);//WRtoRTC
I2C_Wr(0x07);//ControlRegister
I2C_Wr(0x80);//Disablesquarewaveoutputpin
I2C_Stop();
}//~

voidds1307_set_date_time(charday,charmth,charyear,chardow,charhr,charmin,intsec){
sec=(sec&0x7F);
hr=(hr&0x3F);
I2C_Start();
I2C_Wr(0xD0);//I2Cwriteaddress
I2C_Wr(0x00);//StartatREG0Seconds
I2C_Wr(Dec2Bcd(sec));//REG0
I2C_Wr(Dec2Bcd(min));//REG1
I2C_Wr(Dec2Bcd(hr));//REG2
I2C_Wr(Dec2Bcd(dow));//REG3
I2C_Wr(Dec2Bcd(day));//REG4
I2C_Wr(Dec2Bcd(mth));//REG5
I2C_Wr(Dec2Bcd(year));//REG6
I2C_Wr(0x80);//REG7Disablesquarewaveoutputpin
I2C_Stop();
}//~

voidds1307_get_time_date(char*day,char*mth,char*year,char*dow,char*hr,char*min,char*sec){
I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0x00);//StartatREG3Dayofweek
I2C_Start();
I2C_Wr(0xD1);
*sec=Bcd2Dec(I2C_Rd(1)&0x7F);
*min=Bcd2Dec(I2C_Rd(1)&0x7F);
*hr=Bcd2Dec(I2C_Rd(1)&0x3F);
*dow=Bcd2Dec(I2C_Rd(1)&0x7F);//REG3
*day=Bcd2Dec(I2C_Rd(1)&0x3F);//REG4
*mth=Bcd2Dec(I2C_Rd(1)&0x1F);//REG5
*year=Bcd2Dec(I2C_Rd(0));//REG6
I2C_Stop();
}//~

_________________
R. Giskard Reventlov & R. Daneel Olivaw
http://www.circuitechs.com
http://www.dronology.com
Lasteditedbydronologyon07Oct200814:58,edited1timeintotal.

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

2/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary

Top

halvarezo

Postsubject:Thanks

Posted:29Aug200815:42

Thanks, it could be a great help to me but it is a working code? I mean, did you porbe it on HW. I hope you can help me.
Thanks a lot.
Joined:28Aug200805:42
Posts:5
Top

dronology

Postsubject:

Posted:29Aug200816:47

If it wasn't working how could I send? I'm not interested in wasting time here by sending rubbish codes to people? Check
my previous code developements from my profile and previous messages. And RTC code: Ofcourse it is working. Some
addons and modifications can be done. LCD out functions etc. Try to improve this code for your purpose.

Joined:29Dec200723:48
Posts:106
Location:istanbul

_________________
R. Giskard Reventlov & R. Daneel Olivaw
http://www.circuitechs.com
http://www.dronology.com

Top

halvarezo

Postsubject:

Posted:29Aug200816:51

Ok, I'll improve the code for my purpose and I'll Post it. Thanks a lot. I've seen you have done many developements. I am
starting with Mikro C because I used to program with MPLAB C18. Thanks for your time.
Joined:28Aug200805:42
Posts:5
Top

dronology

Postsubject:

Posted:29Aug200817:00

You are welcome :)


_________________
R. Giskard Reventlov & R. Daneel Olivaw

Joined:29Dec200723:48
Posts:106
Location:istanbul

http://www.circuitechs.com
http://www.dronology.com

Top

petrd

Joined:14Feb200811:04
Posts:71
Location:Russia,Voronezh

Postsubject:CodeforDS1307

Posted:25Sep200807:10

I try use any code, but i have problem. I write may code, which based on exemples for EasyPic5. Work this code tested on
EasyPic5 and PIC16F887.
1. Code for init DS1307:
Code:Selectall

*Description:
ThisprojectissimpledemonstrationhowtosetdateandtimeonDS1307
RTC(realtimeclock).ThecodeuseMSSPmoduleatPORTC.
Theexamplesetsthefollowing:
TIME:09:00:00
DATE:25/09/2008
PleaserefertoDS1307datasheetformoreinformationonformatdate
settings.
*Testconfiguration:
MCU:PIC16F887
Dev.Board:EasyPIC5
Oscillator:HS,8.000MHz
Ext.Modules:ICRTC(DS1307)
SW:mikroCv8.2.0.0

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

3/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
*NOTES:
ForproperI2Ccommunication,pinsonPORTCmustbeinthepullupmode,
RC3pin6DS1307SCL,
RC4pin5DS1307SDA,
andtheLEDsonboardswitchedOFF!
*/
voidmain(){
I2C_Init(100000);//initializefullmastermode
I2C_Start();//issuestartsignal
I2C_Wr(0xD0);//addressDS1307
I2C_Wr(0);//startfromwordataddress(REG0)
I2C_Wr(0x80);//write$80toREG0.(pausecounter+0sec)
I2C_Wr(0);//write0tominuteswordto(REG1)
I2C_Wr(0x09);//write12tohoursword(24hoursmode)(REG2)
I2C_Wr(0x05);//write5Thursday(REG3)
I2C_Wr(0x25);//write25todateword(REG4)
I2C_Wr(0x09);//write9(September)tomonthword(REG5)
I2C_Wr(0x08);//write08toyearword(REG6)
I2C_Stop();//issuestopsignal
I2C_Start();//issuestartsignal
I2C_Wr(0xD0);//addressDS1307
I2C_Wr(0);//startfromwordataddress0
I2C_Wr(0);//write0toREG0(enablecounting+0sec)
I2C_Stop();//issuestopsignal
}//~!

2. Code for read DS1307:


Code:Selectall

*Description:
ThisprojectissimpledemonstrationhowtoreaddateandtimefromDS1307
RTC(realtimeclock).ThecodeMCUuseMSSPmoduleatPORTC.
DateandtimeareprintedatLCD.
*Testconfiguration:
MCU:PIC16F887
Dev.Board:EasyPIC5
Oscillator:HS,8.000MHz
Ext.Modules:ICRTC(DS1307),LCD2x16chars
SW:mikroCv8.2.0.0
*NOTES:
ForproperI2Ccommunication,pinsonPORTCmustbeinthepullupmode,
RC3pin6DS1307SCL,
RC4pin5DS1307SDA,
andtheLEDsonboardswitchedOFF!
*/
unsignedcharsec,min1,hr,week_day,day,mn,year;
char*txt,tnum[4];
voidZero_Fill(char*value){//filltextrepesentation
if(value[1]==0){//withleadingzero
value[1]=value[0];
value[0]=48;
value[2]=0;
}
}//~
//ReadstimeanddateinformationfromRTC(DS1307)
voidRead_Time(char*sec,char*min,char*hr,char*week_day,char*day,char*mn,char*year){
I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0);
I2C_Repeated_Start();
I2C_Wr(0xD1);
*sec=I2C_Rd(1);
*min=I2C_Rd(1);

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

4/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
*hr=I2C_Rd(1);
*week_day=I2C_Rd(1);
*day=I2C_Rd(1);
*mn=I2C_Rd(1);
*year=I2C_Rd(0);
I2C_Stop();
}//~
//Formatsdateandtime
voidTransform_Time(char*sec,char*min,char*hr,char*week_day,char*day,char*mn,char*year){
*sec=((*sec&0x70)>>4)*10+(*sec&0x0F);
*min=((*min&0xF0)>>4)*10+(*min&0x0F);
*hr=((*hr&0x30)>>4)*10+(*hr&0x0F);
*week_day=(*week_day&0x07);
*day=((*day&0xF0)>>4)*10+(*day&0x0F);
*mn=((*mn&0x10)>>4)*10+(*mn&0x0F);
*year=((*year&0xF0)>>4)*10+(*year&0x0F);
}//~
//OutputvaluestoLCD
voidDisplay_Time(charsec,charmin,charhr,charweek_day,charday,charmn,charyear){
switch(week_day){
case1:txt="Sun";break;
case2:txt="Mon";break;
case3:txt="Tue";break;
case4:txt="Wed";break;
case5:txt="Thu";break;
case6:txt="Fri";break;
case7:txt="Sat";break;
}
LCD_Out(1,1,txt);
ByteToStr(day,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,6,txt);

ByteToStr(mn,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,9,txt);

ByteToStr(year,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,14,txt);

ByteToStr(hr,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,6,txt);

ByteToStr(min,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,9,txt);

ByteToStr(sec,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,12,txt);
}//~
//Performsprojectwideinit
voidInit_Main(){
ANSEL=0;
ANSELH=0;
Lcd_Config(&PORTB,4,5,6,3,2,1,0);//Lcd_Init_EP5,seeAutocomplete
I2C_Init(100000);//initializeI2C

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

5/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
LCD_Chr(1,8,'.');
LCD_Chr(1,11,'.');
txt="Time:";
LCD_Out(2,1,txt);
LCD_Chr(2,8,':');
LCD_Chr(2,11,':');
txt="20";
LCD_Out(1,12,txt);
LCD_Cmd(LCD_CURSOR_OFF);
}//~
//Mainprocedure
voidmain(){
Init_Main();//performinitialization
while(1){
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);//readtimefromRTC(DS1307)
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);//formatdateandtime
Display_Time(sec,min1,hr,week_day,day,mn,year);//prepareanddisplayonLCD
Delay_ms(1000);//wait1s
}
}//

Best regards!

Top
masterblastersachin

Postsubject:Re:CodeforDS1307

Posted:26Sep200812:52

petrd wrote:
Joined:07May200810:01
Posts:34
Location:BANGALORE

I try use any code, but i have problem. I write may code, which based on exemples for EasyPic5. Work this code tested
on EasyPic5 and PIC16F887.
1. Code for init DS1307:
Code:Selectall

*Description:
ThisprojectissimpledemonstrationhowtosetdateandtimeonDS1307
RTC(realtimeclock).ThecodeuseMSSPmoduleatPORTC.
Theexamplesetsthefollowing:
TIME:09:00:00
DATE:25/09/2008
PleaserefertoDS1307datasheetformoreinformationonformatdate
settings.
*Testconfiguration:
MCU:PIC16F887
Dev.Board:EasyPIC5
Oscillator:HS,8.000MHz
Ext.Modules:ICRTC(DS1307)
SW:mikroCv8.2.0.0
*NOTES:
ForproperI2Ccommunication,pinsonPORTCmustbeinthepullupmode,
RC3pin6DS1307SCL,
RC4pin5DS1307SDA,
andtheLEDsonboardswitchedOFF!
*/
voidmain(){
I2C_Init(100000);//initializefullmastermode
I2C_Start();//issuestartsignal
I2C_Wr(0xD0);//addressDS1307
I2C_Wr(0);//startfromwordataddress(REG0)
I2C_Wr(0x80);//write$80toREG0.(pausecounter+0sec)
I2C_Wr(0);//write0tominuteswordto(REG1)
I2C_Wr(0x09);//write12tohoursword(24hoursmode)(REG2)
I2C_Wr(0x05);//write5Thursday(REG3)
I2C_Wr(0x25);//write25todateword(REG4)
I2C_Wr(0x09);//write9(September)tomonthword(REG5)

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

6/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
I2C_Wr(0x08);//write08toyearword(REG6)
I2C_Stop();//issuestopsignal
I2C_Start();//issuestartsignal
I2C_Wr(0xD0);//addressDS1307
I2C_Wr(0);//startfromwordataddress0
I2C_Wr(0);//write0toREG0(enablecounting+0sec)
I2C_Stop();//issuestopsignal
}//~!

2. Code for read DS1307:


Code:Selectall

*Description:
ThisprojectissimpledemonstrationhowtoreaddateandtimefromDS1307
RTC(realtimeclock).ThecodeMCUuseMSSPmoduleatPORTC.
DateandtimeareprintedatLCD.
*Testconfiguration:
MCU:PIC16F887
Dev.Board:EasyPIC5
Oscillator:HS,8.000MHz
Ext.Modules:ICRTC(DS1307),LCD2x16chars
SW:mikroCv8.2.0.0
*NOTES:
ForproperI2Ccommunication,pinsonPORTCmustbeinthepullupmode,
RC3pin6DS1307SCL,
RC4pin5DS1307SDA,
andtheLEDsonboardswitchedOFF!
*/
unsignedcharsec,min1,hr,week_day,day,mn,year;
char*txt,tnum[4];
voidZero_Fill(char*value){//filltextrepesentation
if(value[1]==0){//withleadingzero
value[1]=value[0];
value[0]=48;
value[2]=0;
}
}//~
//ReadstimeanddateinformationfromRTC(DS1307)
voidRead_Time(char*sec,char*min,char*hr,char*week_day,char*day,char*mn,char*year){
I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0);
I2C_Repeated_Start();
I2C_Wr(0xD1);
*sec=I2C_Rd(1);
*min=I2C_Rd(1);
*hr=I2C_Rd(1);
*week_day=I2C_Rd(1);
*day=I2C_Rd(1);
*mn=I2C_Rd(1);
*year=I2C_Rd(0);
I2C_Stop();
}//~
//Formatsdateandtime
voidTransform_Time(char*sec,char*min,char*hr,char*week_day,char*day,char*mn,char*year){
*sec=((*sec&0x70)>>4)*10+(*sec&0x0F);
*min=((*min&0xF0)>>4)*10+(*min&0x0F);
*hr=((*hr&0x30)>>4)*10+(*hr&0x0F);
*week_day=(*week_day&0x07);
*day=((*day&0xF0)>>4)*10+(*day&0x0F);
*mn=((*mn&0x10)>>4)*10+(*mn&0x0F);
*year=((*year&0xF0)>>4)*10+(*year&0x0F);
}//~

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

7/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
//OutputvaluestoLCD
voidDisplay_Time(charsec,charmin,charhr,charweek_day,charday,charmn,charyear){
switch(week_day){
case1:txt="Sun";break;
case2:txt="Mon";break;
case3:txt="Tue";break;
case4:txt="Wed";break;
case5:txt="Thu";break;
case6:txt="Fri";break;
case7:txt="Sat";break;
}
LCD_Out(1,1,txt);
ByteToStr(day,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,6,txt);

ByteToStr(mn,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,9,txt);

ByteToStr(year,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,14,txt);

ByteToStr(hr,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,6,txt);

ByteToStr(min,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,9,txt);

ByteToStr(sec,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,12,txt);
}//~
//Performsprojectwideinit
voidInit_Main(){
ANSEL=0;
ANSELH=0;
Lcd_Config(&PORTB,4,5,6,3,2,1,0);//Lcd_Init_EP5,seeAutocomplete
I2C_Init(100000);//initializeI2C
LCD_Chr(1,8,'.');
LCD_Chr(1,11,'.');
txt="Time:";
LCD_Out(2,1,txt);
LCD_Chr(2,8,':');
LCD_Chr(2,11,':');
txt="20";
LCD_Out(1,12,txt);
LCD_Cmd(LCD_CURSOR_OFF);
}//~
//Mainprocedure
voidmain(){
Init_Main();//performinitialization
while(1){
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);//readtimefromRTC(DS1307)
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);//formatdateandtime
Display_Time(sec,min1,hr,week_day,day,mn,year);//prepareanddisplayonLCD

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

8/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
Delay_ms(1000);//wait1s
}
}//

Best regards!

hello sir , i burrn this program to 16F877A,


when i m reading the ds1307 ,at first second it displays the correct value but after one second it displays random values on
lcd....
is it a DS1307 chips problem or something else is there....
_________________
SACHIN
http://belgaum.myminicity.com/
Lasteditedbymasterblastersachinon07Oct200812:03,edited1timeintotal.

Top

petrd

Postsubject:

Posted:28Sep200819:13

Quote:
Joined:14Feb200811:04
Posts:71
Location:Russia,Voronezh

hello sir , i burrn this program to 16F877A,


when i m reading the ds1307 ,at first second it displays the correct value but after one second it displays random
values on lcd....
is it a DS1307 chips problem or something else is there....

Hi!
Now i'm testing code with MCU PIC16F877A code work.
For PIC16F877A i'm write:
Code:Selectall

voidInit_Main(){
//ANSEL=0;
//ANSELH=0;
ADCON1=7;

In config word i set: DEBUG_off, LVP_off, WDT_off, HS_OSC.


In may circuit pullup resistors (10 k) set on external board, near DS1307 and pullup resistors RC3 and RC4 on EasyPic5
off. I used LCD WH1602BYYHCTK.

Top
masterblastersachin

Postsubject:Re:CodeforDS1307

Posted:01Oct200809:07

petrd wrote:
Joined:07May200810:01
Posts:34
Location:BANGALORE

I try use any code, but i have problem. I write may code, which based on exemples for EasyPic5. Work this code tested
on EasyPic5 and PIC16F887.
1. Code for init DS1307:
Code:Selectall

*Description:
ThisprojectissimpledemonstrationhowtosetdateandtimeonDS1307
RTC(realtimeclock).ThecodeuseMSSPmoduleatPORTC.
Theexamplesetsthefollowing:
TIME:09:00:00
DATE:25/09/2008
PleaserefertoDS1307datasheetformoreinformationonformatdate
settings.
*Testconfiguration:

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

9/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
MCU:PIC16F887
Dev.Board:EasyPIC5
Oscillator:HS,8.000MHz
Ext.Modules:ICRTC(DS1307)
SW:mikroCv8.2.0.0
*NOTES:
ForproperI2Ccommunication,pinsonPORTCmustbeinthepullupmode,
RC3pin6DS1307SCL,
RC4pin5DS1307SDA,
andtheLEDsonboardswitchedOFF!
*/
voidmain(){
I2C_Init(100000);//initializefullmastermode
I2C_Start();//issuestartsignal
I2C_Wr(0xD0);//addressDS1307
I2C_Wr(0);//startfromwordataddress(REG0)
I2C_Wr(0x80);//write$80toREG0.(pausecounter+0sec)
I2C_Wr(0);//write0tominuteswordto(REG1)
I2C_Wr(0x09);//write12tohoursword(24hoursmode)(REG2)
I2C_Wr(0x05);//write5Thursday(REG3)
I2C_Wr(0x25);//write25todateword(REG4)
I2C_Wr(0x09);//write9(September)tomonthword(REG5)
I2C_Wr(0x08);//write08toyearword(REG6)
I2C_Stop();//issuestopsignal
I2C_Start();//issuestartsignal
I2C_Wr(0xD0);//addressDS1307
I2C_Wr(0);//startfromwordataddress0
I2C_Wr(0);//write0toREG0(enablecounting+0sec)
I2C_Stop();//issuestopsignal
}//~!

2. Code for read DS1307:


Code:Selectall

*Description:
ThisprojectissimpledemonstrationhowtoreaddateandtimefromDS1307
RTC(realtimeclock).ThecodeMCUuseMSSPmoduleatPORTC.
DateandtimeareprintedatLCD.
*Testconfiguration:
MCU:PIC16F887
Dev.Board:EasyPIC5
Oscillator:HS,8.000MHz
Ext.Modules:ICRTC(DS1307),LCD2x16chars
SW:mikroCv8.2.0.0
*NOTES:
ForproperI2Ccommunication,pinsonPORTCmustbeinthepullupmode,
RC3pin6DS1307SCL,
RC4pin5DS1307SDA,
andtheLEDsonboardswitchedOFF!
*/
unsignedcharsec,min1,hr,week_day,day,mn,year;
char*txt,tnum[4];
voidZero_Fill(char*value){//filltextrepesentation
if(value[1]==0){//withleadingzero
value[1]=value[0];
value[0]=48;
value[2]=0;
}
}//~
//ReadstimeanddateinformationfromRTC(DS1307)
voidRead_Time(char*sec,char*min,char*hr,char*week_day,char*day,char*mn,char*year){
I2C_Start();
I2C_Wr(0xD0);

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

10/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
I2C_Wr(0);
I2C_Repeated_Start();
I2C_Wr(0xD1);
*sec=I2C_Rd(1);
*min=I2C_Rd(1);
*hr=I2C_Rd(1);
*week_day=I2C_Rd(1);
*day=I2C_Rd(1);
*mn=I2C_Rd(1);
*year=I2C_Rd(0);
I2C_Stop();
}//~
//Formatsdateandtime
voidTransform_Time(char*sec,char*min,char*hr,char*week_day,char*day,char*mn,char*year){
*sec=((*sec&0x70)>>4)*10+(*sec&0x0F);
*min=((*min&0xF0)>>4)*10+(*min&0x0F);
*hr=((*hr&0x30)>>4)*10+(*hr&0x0F);
*week_day=(*week_day&0x07);
*day=((*day&0xF0)>>4)*10+(*day&0x0F);
*mn=((*mn&0x10)>>4)*10+(*mn&0x0F);
*year=((*year&0xF0)>>4)*10+(*year&0x0F);
}//~
//OutputvaluestoLCD
voidDisplay_Time(charsec,charmin,charhr,charweek_day,charday,charmn,charyear){
switch(week_day){
case1:txt="Sun";break;
case2:txt="Mon";break;
case3:txt="Tue";break;
case4:txt="Wed";break;
case5:txt="Thu";break;
case6:txt="Fri";break;
case7:txt="Sat";break;
}
LCD_Out(1,1,txt);
ByteToStr(day,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,6,txt);

ByteToStr(mn,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,9,txt);

ByteToStr(year,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,14,txt);

ByteToStr(hr,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,6,txt);

ByteToStr(min,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,9,txt);

ByteToStr(sec,tnum);
txt=rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,12,txt);
}//~
//Performsprojectwideinit

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

11/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
voidInit_Main(){
ANSEL=0;
ANSELH=0;
Lcd_Config(&PORTB,4,5,6,3,2,1,0);//Lcd_Init_EP5,seeAutocomplete
I2C_Init(100000);//initializeI2C
LCD_Chr(1,8,'.');
LCD_Chr(1,11,'.');
txt="Time:";
LCD_Out(2,1,txt);
LCD_Chr(2,8,':');
LCD_Chr(2,11,':');
txt="20";
LCD_Out(1,12,txt);
LCD_Cmd(LCD_CURSOR_OFF);
}//~
//Mainprocedure
voidmain(){
Init_Main();//performinitialization
while(1){
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);//readtimefromRTC(DS1307)
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);//formatdateandtime
Display_Time(sec,min1,hr,week_day,day,mn,year);//prepareanddisplayonLCD
Delay_ms(1000);//wait1s
}
}//

Best regards!

thanks dear this code works ,the problem occurs becuase of i didn't pull up the i2c pins high..............
thanks every body.............
_________________
SACHIN
http://belgaum.myminicity.com/
Lasteditedbymasterblastersachinon07Oct200812:02,edited1timeintotal.

Top

masterblastersachin

Postsubject:adjustthetimeofRTCusing4keys

Posted:03Oct200808:16

hello ,
i want to introduce the 4 keys in the previously posted program to adjust the timimg....
1key as> set
2nd key>up
3rd key>down
4th key > ok....
if i want to set the time press the set key,
for posioning the cursor to hour or min or sec press the set key ....
for adjusting the time use up or down key...
after setting the time press OK key for displaying new time....
please help me.....
how can i do it....
thanking you....

Joined:07May200810:01
Posts:34
Location:BANGALORE

_________________
SACHIN
http://belgaum.myminicity.com/

Top

Mohan

Postsubject:

Posted:06Oct200814:32

Hi,

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

12/19

10/21/2015
Joined:23Jan200812:27
Posts:12
Location:Pondicherry

mikroElektronikaViewtopicDS1307MikroCLibrary
Try this out. I used the code at http://www.mikroe.com/forum/viewtopic.php?t=15813
and adapted for EP5, PIC16F877A, 8Mhz crystal & DS1307
LCD at PORTB with EP5 default config, PORTD pull down, Switch Jumper J17 set to VCC

Code:Selectall

//DefinitionsforI2CdevicesBIOS
#defineDS1307_HWAddress0xD0//HardwareaddressoftheRTC
//KeypadPortandlines
#defineKEYPAD_PORTPORTD//Selectkeypadport
#defineKEYPAD_MENUF0//Menu/Exit
#defineKEYPAD_SELECTF1//Select
#defineKEYPAD_INCF2//Incnumber
#defineKEYPAD_DECF3//Decnumber
#defineKEYPAD_RESETF7//Reset
#ifndefHIGH
#defineHIGH1
#defineLOW0
#endif//HIGH+LOW
unsignedcharbSec,bMin,bStd,bDay,bMon,bYear,oldstate;
unsignedintuSec,uMin,uStd,uDay,uMon,uYear;
unsignedcharoutput[21];
externvoidmenu(void);
externvoidStoreToRTC(void);
externvoidInitDisplay(void);
externvoidSet_Position(unsignedcharSign);
externvoidIncDecSign(unsignedSign,unsigneddirect);
externunsignedcharDS1307_Read(unsignedcharAddr);
externvoidDS1307_Write(unsignedcharAddr,unsignedcharData);
//ReadsabytefromtheDS1307(softwarecontrolled)
//Input:addresstowriteto
//Output:thebytevalueinthememoryposition
//Desc:thefunctionreadsasinglebyte(Data)atatimetothedataaddressindicatedinAddr.
unsignedcharDS1307_Read(unsignedcharAddr)
{
unsignedcharAddrBuff;
AddrBuff=DS1307_HWAddress;
I2C_Start();//IssueI2Cstartsignal
I2C_Wr(AddrBuff);//Sendhardwareaddressforwritting
I2C_Wr(Addr);//Sendthedataaddress
I2C_Repeated_Start();
AddrBuff.F0=HIGH;//Configurethedatadirectionbitforreading
I2C_Wr(AddrBuff);//Senddeviceaddress
AddrBuff=I2C_Rd(0);//ReadthebytefromtheslaveandNACKtoendcomms
I2C_Stop();//Freeupthebus
return(AddrBuff);
}
//WritesabytetotheDS1307(softwarecontrolled)
//Input:addresstowriteto,valuetobewritten
//Output:none
//Desc:thefunctionwritesasinglebyte(Data)atatimetothedataaddressindicatedinDataAddr.
voidDS1307_Write(unsignedcharAddr,unsignedcharData)
{
I2C_Start();//IssueI2Cstartsignal
I2C_Wr(DS1307_HWAddress);//Sendhardwareaddressforwritting
I2C_Wr(Addr);//Sendlowbyteofthedataaddress
I2C_Wr(Data);//Sendthedata
I2C_Stop();//Freethebus
return;
}

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

13/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
voidInitDisplay(void)
{
Lcd_Config(&PORTB,4,5,6,3,2,1,0);//Lcd_Init_EP5,seeAutocomplete
Lcd_Cmd(LCD_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);//Turnoffcursor
Lcd_Out(1,1,"Time:00:00:00");
Lcd_Out(2,1,"Date:00.00.0000");
Delay_ms(2000);
}
voidReset_Clock(void)
{
bStd=0;
bMin=0;
bSec=0;
bDay=18;
bMon=7;
bYear=8;
StoreToRTC();
InitDisplay();
}
voidSet_Position(unsignedcharSign)
{
switch(Sign)
{
case0:uStd=(unsignedint)bStd;
sprinti(&output,"%02u",uStd);
Lcd_Out(1,7,output);
Lcd_Out(1,8,"");
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
break;
case1:uMin=(unsignedint)bMin;
sprinti(&output,"%02u",uMin);
Lcd_Out(1,10,output);
Lcd_Out(1,11,"");
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
break;
case2:uSec=(unsignedint)bSec;
sprinti(&output,"%02u",uSec);
Lcd_Out(1,13,output);
Lcd_Out(1,14,"");
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
break;
case3:uDay=(unsignedint)bDay;
sprinti(&output,"%02u",uDay);
Lcd_Out(2,7,output);
Lcd_Out(2,8,"");
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
break;
case4:uMon=(unsignedint)bMon;
sprinti(&output,"%02u",uMon);
Lcd_Out(2,10,output);
Lcd_Out(2,11,"");
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
break;
case5:uYear=(unsignedint)bYear+2000;
sprinti(&output,"%04u",uYear);
Lcd_Out(2,13,output);
Lcd_Out(2,16,"");
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
break;
};
}
voidIncDecSign(unsignedSign,unsigneddirect)
{
switch(Sign)
{

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

14/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
case0:if(!direct)
{
bStd++;
if(bStd>23)
bStd=23;
}else{
if(bStd>0)
bStd;
};
uStd=(unsignedint)bStd;
break;
case1:if(!direct)
{
bMin++;
if(bMin>59)
bMin=59;
}else{
if(bMin>0)
bMin;
};
uMin=(unsignedint)bMin;
break;
case2:if(!direct)
{
bSec++;
if(bSec>59)
bSec=59;
}else{
if(bSec>0)
bSec;
};
uSec=(unsignedint)bSec;
break;
case3:if(!direct)
{
bDay++;
if(bDay>31)
bDay=31;
}else{
if(bDay>0)
bDay;
};
uDay=(unsignedint)bDay;
break;
case4:if(!direct)
{
bMon++;
if(bMon>12)
bMon=12;
}else{
if(bMon>0)
bMon;
};
uMon=(unsignedint)bMon;
break;
case5:if(!direct)
{
bYear++;
if(bYear>99)
bYear=99;
}else{
if(bYear>0)
bYear;
};
uYear=(unsignedint)bYear+2000;
break;
};
}

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

15/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
//StoreallSettingsintoDS1307
/////////////////////////////////
voidStoreToRTC(void)
{
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);
Lcd_Out(1,1,"StoretoRTC");
bSec=(((bSec/10)<<4)&0xF0)+((bSec(bSec/10)*10)&0b00001111);
DS1307_Write(0x00,bSec);
bMin=(((bMin/10)<<4)&0xF0)+((bMin(bMin/10)*10)&0b00001111);
DS1307_Write(0x01,bMin);
bStd=(((bStd/10)<<4)&0xF0)+((bStd(bStd/10)*10)&0b00001111);
DS1307_Write(0x02,bStd);
//bDay=((bDay&0x30)>>4)*10+(bDay&0b00001111);
bDay=(((bDay/10)<<4)&0x30)+((bDay(bDay/10)*10)&0b00001111);
DS1307_Write(0x04,bDay);
bMon=(((bMon/10)<<4)&0x10)+((bMon(bMon/10)*10)&0b00001111);
DS1307_Write(0x05,bMon);
bYear=(((bYear/10)<<4)&0xC0)+((bYear(bYear/10)*10)&0b00001111);
DS1307_Write(0x06,bYear);
delay_ms(2000);
}
//KeyPort,inputviabutton
//////////////////////////////
//F0=Menu/Exit
//F1=Select
//F2=INCNumber
//F3=DECNumber
//F7=ResetClockChip
voidmenu(void)
{
unsignedcharSig;
Sig=0;
Lcd_Out(1,1,"Set");
Lcd_Out(2,1,"");
Lcd_Cmd(LCD_BLINK_CURSOR_ON);
Set_Position(Sig);
Delay_ms(2000);
do
{
//Menukeypressed?
////////////////////
if(KEYPAD_PORT.KEYPAD_MENU==1){
delay_ms(250);
break;
};
if(KEYPAD_PORT.KEYPAD_SELECT==1){
delay_ms(250);
Sig++;
if(Sig>5)
Sig=0;
Set_Position(Sig);
};
//Increment
if(KEYPAD_PORT.KEYPAD_INC==1){
delay_ms(100);
IncDecSign(Sig,0);
Set_Position(Sig);
};
//Decrement
if(KEYPAD_PORT.KEYPAD_DEC==1){

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

16/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
delay_ms(100);
IncDecSign(Sig,1);
Set_Position(Sig);
};
Delay_ms(100);
}while(1);
//StoreallSettingsinDS1307
///////////////////////////////
StoreToRTC();
//ExittoClock
Lcd_Cmd(Lcd_CURSOR_OFF);

//RefreshDisplay
InitDisplay();
}
voidmain(void)
{
TRISD=0b10001111;
PORTB=0x00;
TRISC=0x00;
PORTC=0b00;
TRISD=0x00;
PORTD=0x00;
TRISE=0x00;
PORTE=0x00;
ADCON1=7;//disableanaloginputs
I2C_Init(100000);//initializeI2C
I2C_Start();//issuestartsignal
I2C_Wr(0xD0);//addressDS1307
I2C_Wr(0);//startfromwordataddress0
I2C_Wr(0);//write0toREG0(enablecounting+0sec)
I2C_Stop();//issuestopsignal
InitDisplay();
oldstate=0;
//ClockRoutine
////////////////
do
{
delay_ms(100);
bSec=DS1307_Read(0x00);
bSec=((bSec&0xF0)>>4)*10+(bSec&0b00001111);
uSec=(unsignedint)bSec;
bMin=DS1307_Read(0x01);
bMin=((bMin&0xF0)>>4)*10+(bMin&0b00001111);
uMin=(unsignedint)bMin;
bStd=DS1307_Read(0x02);
bStd=((bStd&0xF0)>>4)*10+(bStd&0b00001111);
uStd=(unsignedint)bStd;
bDay=DS1307_Read(0x04);
bDay=((bDay&0x30)>>4)*10+(bDay&0b00001111);
uDay=(unsignedint)bDay;
bMon=DS1307_Read(0x05);
bMon=((bMon&0x10)>>4)*10+(bMon&0b00001111);
uMon=(unsignedint)bMon;
bYear=DS1307_Read(0x06);
bYear=((bYear&0xC0)>>4)*10+(bYear&0b00001111);
uYear=(unsignedint)bYear+2000;
sprinti(&output,"%02u:%02u:%02u",uStd,uMin,uSec);

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

17/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary
Lcd_out(1,7,output);
sprinti(&output,"%02u.%02u.%04u",uDay,uMon,uYear);
Lcd_out(2,7,output);
//Menukeypressed?
////////////////////
if(KEYPAD_PORT.KEYPAD_MENU==1){
delay_ms(250);
menu();
}elseif(KEYPAD_PORT.KEYPAD_RESET==1){
delay_ms(250);
Reset_Clock();
};
}while(1);
}

_________________
Hobbyist, Pondicherry
India

Top

masterblastersachin

Postsubject:

Posted:06Oct200814:44

thanx yaar.....
_________________
SACHIN
http://belgaum.myminicity.com/

Joined:07May200810:01
Posts:34
Location:BANGALORE

Top

majerv

Postsubject:18F4550RTCProblem

Posted:20Oct200812:11

Hi Folks!
I've tried dronology's program on a 16F877 and it runs perfect, but not on a 18F4550. I've connected the SDA(pin5) to the
RB0 of the PIC and SCL(pin6) to the RB1. I don't know what the problem could be.

Joined:26Sep200800:54
Posts:26

I think it must be a config problem.


Any Idea?
Thanx...

Top

dronology

Postsubject:Re:18F4550RTCProblem

Posted:21Oct200813:29

Dear majerv,
Joined:29Dec200723:48
Posts:106
Location:istanbul

It seems that 18F4550 and 18F4620 (my code based on 4620) have huge differences. SDA and SCL pins of 4620 are located
on PORTC. SDA>RC4 and SCL>RC3. I checked 4550. SCL pin is located on RB1 and SDA is located on RB0. On 4550 RB0
and RB1 pins contain extra properties such as Analog input and interrupt. Have you disabled analoginputs and interrupts
on this chip before using DS1307 codes?
_________________
R. Giskard Reventlov & R. Daneel Olivaw
http://www.circuitechs.com
http://www.dronology.com

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

18/19

10/21/2015

mikroElektronikaViewtopicDS1307MikroCLibrary

Top

Displaypostsfromprevious: Allposts

Sortby Posttime

Ascending

Page1of3 [44posts]

Go
Gotopage1,2,3Next

BoardindexCompilersmikroCCompilersOlderversionsofmikroCCompilersmikroCforPICmikroCGeneral

AlltimesareUTC+1hour

Whoisonline
Usersbrowsingthisforum:Noregisteredusersand3guests
Youcannotpostnewtopicsinthisforum
Youcannotreplytotopicsinthisforum
Youcannotedityourpostsinthisforum
Youcannotdeleteyourpostsinthisforum
Youcannotpostattachmentsinthisforum
Searchfor:

Go

Jumpto: mikroCGeneral

Go

PoweredbyphpBBForumSoftwarephpBBGroup

http://www.mikroe.com/forum/viewtopic.php?t=10706&view=next

19/19

Vous aimerez peut-être aussi