/* ***************************************************************************** * * File Name - rtc_set.c * Function - Demonstrates writing to the uFlash RTC over the I2C connection * Note that you should run the rtc_read program to verify the correct time * * Once this program is run the RTC on the uFlash will maintain the date & time * so long as the RTC battery has charge. * * You will need: * (1) uFlash & CCS PIC C compiler (PCM/PCW) * * Instructions: * (1) Modify the data and time to the current values * (2) Compile & downloading to the uFlash * (3) Run the rtc_read program to confirm correct opperation * * * Since there is no logical difference between an I2C chip located on the * internal I2C bus of a uFlash and one located on the uConnect bus of a * uFlash876B, we can use the uConnect library code to simplify access to * the RTC. This means that this code would also work for a uFlash876B with * an RTC located externally on the uConnect bus. * ***************************************************************************** */ #include #include /* ******************************* rtc buffer locations ************************ */ #define SEC 0 // RTC data offsets in the buffer #define MIN 1 // note that RTC data is stored in #define HOUR 2 // Binary Coded Decimal (BCD) format #define DOW 3 #define DAY 4 #define MONTH 5 #define YEAR 6 /* ******************************* rtc buffer ********************************** * * This RAM buffer mirrors the format of the RTC on chip registers * All values are stored in Binary Coded Decimal (BCD) * See DS1307 data sheet for details * * Modify these values to represent the current date & time before * compiling & running the program. * * Note the use of hex values to represent the the BCD time, so to enter * the 23rd of the month enter the value 0x23 in field 4 of the buffer */ BYTE rtc[7]= { 0x00, // Seconds (0-59) (Bit 7 is the enable flag) 0x00, // Minutes (0-59) 0x00, // Hours (0-24 or 0-12 am/pm) (Bit 6 12/24 hr ctrl) 0x03, // Day of week (1-7) 0x01, // Day of month (1-31) 0x01, // Month (1-12) 0x02 // Year (0-99) }; /* ***************************** function prototypes *************************** */ void set_rtc(void); /* *********************************** main ************************************ * * Read and display the date & time each time it changes */ void main(void) { set_rtc(); // set the time } // processor to sleep /* ****************************** set_rtc ************************************** * * Copy the contents of the rtc buffer to the rtc chip * * Not used in the rtc_read program */ void set_rtc(void) { uc_wstart(0x68); // address the rtc (write) uc_write(0x00); // start at location 0 (SEC) uc_write(0x80); // stop the clock to prevent roll over uc_write(rtc[MIN]&0x7f); // write the minutes uc_write(rtc[HOUR]&0x3f); // write the hours uc_write(rtc[DOW]&0x07); // write the day of week uc_write(rtc[DAY]&0x3f); // write the month uc_write(rtc[MONTH]&0x1f); // write the month uc_write(rtc[YEAR]); // write the year uc_wstart(0x68); // start again uc_write(0x00); // start at location 0 (SEC) uc_write(rtc[SEC]&0x7f); // write seconds & enable rtc uc_stop(); // end transaction }