LCD Clock

Last week, one of our customers, Martin Ederveen showed us what he made with his ATMEGA168 Experimenter's Kit, an LCD clock.

Martin based the circuit on the Analogue to Digital Conversion, we did last month, then took the analog input out and added 2 push buttons. The push buttons are used to set the hours and minutes.

LCD Clock

  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <avr/wdt.h>
  4. #include <stdio.h>
  5. #include <util/delay.h>
  6. #include <stdlib.h>
  7. #include <avr/hd44780.h>
  8.  
  9. unsigned char sec=0;
  10. unsigned char min=0;
  11. unsigned char hour=0;
  12. ISR(SIG_OUTPUT_COMPARE1A)
  13. {
  14. sec=sec+4;
  15. if(sec>59){min++;sec=0;};
  16. if(min>59){min=0;hour++;};
  17. if(hour>23){hour=0;};
  18. return;
  19. }
  20. void ioinit (void)
  21. {
  22.    DDRC  = 0b11001111; //1 = output, 0 = input
  23.    PORTC = 0b00110000; //Enable pin 5 and 4 internal pullup
  24. }
  25.  
  26. #define HOU() (bit_is_clear(PINC,4))
  27. #define MIN() (bit_is_clear(PINC,5))
  28. int main(void)
  29. {
  30. TIMSK1 = _BV(OCIE1A); // Enable Interrupt Timer/Counter 1, Output Compare A
  31. TCCR1B = _BV(CS12) | _BV(WGM12); // Clock/256, 0.000256 secs per tick, Mode=CTC
  32. OCR1A = 15625; // SIG_COMPARE1A triggered every 4 seconds
  33. sei();
  34. ioinit();
  35. lcd_init();
  36. while(1)
  37. {
  38. if(MIN())
  39. {
  40. min++; // Button for minute increase has been pressed
  41. sec=0;
  42. if(min>59){min=0;}
  43. Delay_ms(100); // Not too fast on the button, increase to slow it down
  44. }
  45. if(HOU())
  46. {
  47. hour++; // Button for hour increase has been pressed
  48. sec=0;
  49. if(hour>24){hour=0;}
  50. Delay_ms(100); // Not too fast on the button, increase to slow it down
  51. }
  52. char buffer[16];
  53. sprintf(buffer,"%2d : %2d . %2d",hour,min,sec); // Just an example of a working format
  54. lcd_goto(0);
  55. lcd_puts(buffer);
  56. Delay_ms(1);
  57. }
  58. }

The microcontroller is running on the internal oscillator so the time will drift a bit, but it is a fun project and great for learning how microcontrollers work.

We love to hear from our customers, so keep sending us your projects.