This is an example program to interface Maxim DS1788X series RTC to a PIC18F microcontroller. The DS17287, DS17487, and DS17887 integrate a quartz crystal and lithium energy source into a 24-pin encapsulated DIP package. The same code may be used to interface Older DS1288X series RTCs with little or no modifications.
Pin connections with uC are specified in “rtc.h” file. Here is the main program file and complete project to be compiled with HI-TECH PIC18 compiler.
The routines in rtc.h can also be used to access the built-in NVRAM of these ICs.
/* Sample code to interface DS1788X or DS1288X RTC to PIC18 microcontrollers To be compiled with HI-TECH PIC18 Compilers Freely distributable in this original form. (c) 2009 Pasan Hettiarachchi (c) Microsolutions Any questions or comments? admin@microsolutions.info */ #include <pic18.h> #include"delay.h" #include"serial.h" #include "rtc.h" #include <stdio.h> __CONFIG (1,ECDB4 & OSCSDIS); __CONFIG (2,WDTDIS); __CONFIG (4,LVPDIS & STVRDIS); __CONFIG (5,CPB); void main(){ dateTimeRTC dateTime ; ADCON1=0x06; // for newer PIC18F4580 etc.. not important CMCON=0x07; // TRISC=0b10111111; // TRISB=0b11100001; //LATB1-LATB4 are used as control pins for the RTC parallel interface init_comms(); /* Put correct values to dateTime structure and uncomment these lines to set the date time and intialize RTC dateTime.year=4; dateTime.month=10; dateTime.dateOfTheMonth=31; dateTime.hours=13; dateTime.dayOfTheWeek=1; dateTime.minutes=27; dateTime.seconds=0; intialiseRTC(&dateTime,0); */ while(1){ DelayMs(250); if (readDateTime(&dateTime)) { printf("%02d-%02d-%02d %02d:%02d:%02d\n",dateTime.year,dateTime.month,dateTime.dateOfTheMonth, dateTime.hours,dateTime.minutes,dateTime.seconds); } } }
Download complete: DS17887-PIC18F (2011-09-02 link updated)
Can in use DS1307?
Of course you can :) But you have to use the I2C bus. Interfacing DS1307 will not be much different from interfacing TMP101 described here.
http://microsolutions.info/2011/08/tmp101-temperature-sensor.html
Pingback: Compact Flash based serial (RS232) data logger powered by PIC18F458 – Microsolutions