#include #include #include #include #include #include "ks0108.h" #include "allfonts.h" #include "twi.h" #include "ds3231.h" //TWI frequency is 347khz.DS3231 supports a highest of 400khz volatile unsigned char Sync; void print_bcd(uint8_t data) { ks0108_putchar('0'+(data>>4)); ks0108_putchar('0'+(data & 0x0F)); } void print_dec(uint8_t data1) { ks0108_putchar('0'+(data1/10)); ks0108_putchar('0'+(data1%10)); } uint8_t BCDtoDEC(uint8_t bcd_val) { return (((bcd_val >> 4) * 10) + (bcd_val & 0x0F)); } void INT1_enable(void) { MCUCR |=(1<>5); hr&=0b00011111; //Convert BCD to Decimal hr_dec=BCDtoDEC (hr); min_dec=BCDtoDEC (min); sec_dec=BCDtoDEC (sec); //Print AM/PM ks0108_select_font(System5x7,ks0108_read_font_data,BLACK); ks0108_cursorto(19,4); if(i==0) { ks0108_puts("AM"); } else if(i==1) { ks0108_puts("PM"); } //Print date,month,year ks0108_select_font(fixednums8x16,ks0108_read_font_data,BLACK); ks0108_cursorto(0,2); print_bcd(dat); ks0108_cursorto(3,2); print_bcd(mnt); ks0108_cursorto(6,2); print_bcd(yer); //Print Day ks0108_select_font(System5x7,ks0108_read_font_data,BLACK); ks0108_cursorto(14,4); switch(dy) { case 1: ks0108_puts("Sun"); break; case 2: ks0108_puts("Mon"); break; case 3: ks0108_puts("Tue"); break; case 4: ks0108_puts("Wed"); break; case 5: ks0108_puts("Thu"); break; case 6: ks0108_puts("Fri"); break; case 7: ks0108_puts("Sat"); break; } Read_rtc=0; Sync=0;//Now current data already fetched,No need to wait for another second pulse.Just Print it Display=1;//Enable the display flag in order to print the current data INT1_enable();//enable the Interrupt to detect seconds further } //*******************************************************The per-second sync loop starts here ****************************************************** if(Sync) //If a per-second sync detected { PORTC ^=(1<<4);//Toggle the state of LED Display=1;//Enable the display update Sync=0;//Sync=0,wait for resync with per-second pulse //Now update whats necessary if(sec_dec==59)//if it was 60th sec(counting from 0) { sec_dec=0x00;//now reset it to 0 if(min_dec==59)//now if it was 60th min(counting from 0) { min_dec=0x00;//reset it to 00 if(hr_dec==12)//if it was 12th hour { //it is either a new day or am-pm conversion,read the RTC. Read_rtc=1; Display=0;//Disable the display to flag to quit printing INT1_disable();//Disable the per-second sync pulse detection,Now we will read the RTC } else { hr_dec++; } } else { min_dec++; } } else { sec_dec++; } }//end of per-second sync loop //***************************Display every second loop*************************** if(Display==1)//Display only if the flag is Set { ks0108_select_font(fixednums15x31,ks0108_read_font_data,BLACK); ks0108_cursorto(0,0); print_dec(hr_dec); ks0108_cursorto(3,0); print_dec(min_dec); ks0108_cursorto(6,0); print_dec(sec_dec); Display=0; } } }