The code:
/* Title : LCD 16x2 Clock with Blinking Colon Separator * Author : Yohanes Erwin (beginnerprogrammer168.blogspot.com) */ #include <mega16.h> #include <delay.h> #include <lcd.h> #include <stdio.h> void update_time_colon_on(void); void update_time_colon_off(void); unsigned char half_second = 0; unsigned char second = 0; unsigned char minute = 0; unsigned char hour = 0; // ISR execute every 500ms interrupt [TIM1_COMPA] void timer1_compa_isr(void) { // half_second == 2 means 1 second // display time with colon separator if (++half_second == 2) { half_second = 0; if (second < 59) { second++; } else { if (minute < 59) { second = 0; minute++; } else { if (hour < 23) { second = 0; minute = 0; hour++; } else { second = 0; minute = 0; hour = 0; } } } update_time_colon_on(); } // display time without colon separator else { update_time_colon_off(); } } // Main function void main(void) { // Timer 1 compare match every 500ms TCCR1B = 0x0C; OCR1AH = 0x7A; OCR1AL = 0x12; // Timer 1 compare match interrupt enable TIMSK = 0x10; #asm("sei") // LCD initialization on PORTC #asm .equ __lcd_port = 0x15 #endasm lcd_init(16); lcd_clear(); update_time_colon_on(); // Do nothing while(1); } // Display time with colon separator // Time format hh:mm:ss void update_time_colon_on() { char s_second[]; char s_minute[]; char s_hour[]; lcd_clear(); if (hour == 9) sprintf(s_hour, " 0%d", hour); else sprintf(s_hour, " %d", hour); lcd_puts(s_hour); lcd_putchar(':'); if (minute == 9) sprintf(s_minute, "0%d", minute); else sprintf(s_minute, "%d", minute); lcd_puts(s_minute); lcd_putchar(':'); if (second == 9) sprintf(s_second, "0%d", second); else sprintf(s_second, "%d", second); lcd_puts(s_second); } // Display time without colon separator // Time format hh mm ss void update_time_colon_off() { char s_second[]; char s_minute[]; char s_hour[]; lcd_clear(); if (hour == 9) sprintf(s_hour, " 0%d", hour); else sprintf(s_hour, " %d", hour); lcd_puts(s_hour); lcd_putchar(' '); if (minute == 9) sprintf(s_minute, "0%d", minute); else sprintf(s_minute, "%d", minute); lcd_puts(s_minute); lcd_putchar(' '); if (second == 9) sprintf(s_second, "0%d", second); else sprintf(s_second, "%d", second); lcd_puts(s_second); }
No comments :
Post a Comment