/* ***************************************************************************** * * File Name - rtc_read.c * Function - Demonstrates reading from the uFlash RTC over the I2C connection * * Note that you should run the rtc_set program to set and enable the RTC * before using this program * * You will need: * (1) uFlash & CCS PIC C compiler (PCM/PCW) * (2) PC Terminal emulator (such as Hyperterm) * * Instructions: * (1) Compile & downloading to the uFlash * (2) Setup Hyprterm for 57K6 Baud,8 bits, no parity, no echo, no handshaking * (3) Remove the Pgrm Jumper from the uFlash (this stops Hyperterm from * holding the uFlash in reset). * (4) Run Hyperterm * * 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 // use the uConnect support code /* ******************************* 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 * * The initial value shown here is 00:00:00 Saturday 01/01/2000 */ 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) 0x07, // Day of week (1-7) 0x01, // Day of month (1-31) 0x01, // Month (1-12) 0x00 // Year (0-99) }; /* ***************************** function prototypes *************************** */ void read_rtc(void); void print_rtc(void); /* *********************************** main ************************************ * * Read and display the date & time each time it changes */ void main(void) { BYTE last_sec; // the last second read from the rtc printf("RTC Read - Reads and displays the contents of the uFlash RTC\r"); printf("If the the data displayed is wrong or invalid please use the\r"); printf("rtc_set program to set the uFlash RTC to a valid time\r\r"); last_sec=0xff; // force print_rtc to run next time while(true) // loop forever { delay_ms(100); // every 100 mS read_rtc(); // read the rtc if(last_sec!=rtc[0]) // if the time has changed { last_sec=rtc[0]; // update last_sec print_rtc(); // print the date & time } } } /* ****************************** read_rtc ************************************* * * Copy the contents of the rtc chip to the rtc buffer * * Not used in the rtc_set program */ void read_rtc(void) { uc_wstart(0x68); // address the RTC (write) uc_write(0); // start at location 0 (SEC) uc_rstart(0x68); // address the RTC (read) rtc[SEC]=uc_read(1); // read seconds rtc[MIN]=uc_read(1); // read minutes rtc[HOUR]=uc_read(1); // read hours rtc[DOW]=uc_read(1); // read day of week rtc[DAY]=uc_read(1); // read day of month rtc[MONTH]=uc_read(1); // read month rtc[YEAR]=uc_read(0); // read year (since 2000) uc_stop(); // finished } /* **************************** print_rtc ************************************** * * Print the contents of the rtc buffer * Note that because RTC values are stored in BCD format printing them in * hex gives the required result (think about it!) * * Display format is: * * hh:mm:ss dayofweek dd/mm/yyyy */ void print_rtc(void) { printf("%02x:%02x:%02x ",rtc[HOUR],rtc[MIN],rtc[SEC]); switch(rtc[DOW]) { case 1: printf("Sunday "); break; case 2: printf("Monday "); break; case 3: printf("Tuesday "); break; case 4: printf("Wednesday "); break; case 5: printf("Thursday "); break; case 6: printf("Friday "); break; case 7: printf("Saturday "); break; } printf("%02x/%02x/%04lx\r",rtc[DAY],rtc[MONTH],rtc[YEAR]+0x2000); }