Skip to main content

Customer Project – LCD Clock

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

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <stdio.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/hd44780.h>
 
unsigned char sec=0;
unsigned char min=0;
unsigned char hour=0;
ISR(SIG_OUTPUT_COMPARE1A)
{
sec=sec+4;
if(sec>59){min++;sec=0;};
if(min>59){min=0;hour++;};
if(hour>23){hour=0;};
return; 
}
void ioinit (void)
{
   DDRC  = 0b11001111; //1 = output, 0 = input
   PORTC = 0b00110000; //Enable pin 5 and 4 internal pullup
}
 
#define HOU() (bit_is_clear(PINC,4))
#define MIN() (bit_is_clear(PINC,5))
int main(void)
{
TIMSK1 = _BV(OCIE1A); // Enable Interrupt Timer/Counter 1, Output Compare A
TCCR1B = _BV(CS12) | _BV(WGM12); // Clock/256, 0.000256 secs per tick, Mode=CTC
OCR1A = 15625; // SIG_COMPARE1A triggered every 4 seconds
sei();
ioinit();
lcd_init();
while(1)
{
if(MIN())
{
min++; // Button for minute increase has been pressed
sec=0;
if(min>59){min=0;}
Delay_ms(100); // Not too fast on the button, increase to slow it down
}
if(HOU())
{
hour++; // Button for hour increase has been pressed
sec=0;
if(hour>24){hour=0;}
Delay_ms(100); // Not too fast on the button, increase to slow it down
}
char buffer[16];
sprintf(buffer,"%2d : %2d . %2d",hour,min,sec); // Just an example of a working format
lcd_goto(0);
lcd_puts(buffer);
Delay_ms(1);
}
}

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.

Related News

ATMega1284 Development Kit

Introducing the 40 pin AVR Development Board

We are please to announce our newest product, the 40 pin AVR Development Board. This...

Small, medium and large prototyping boards

Small, Medium and Large

Ever since we started, we've stocked full size and half size prototyping boards. The standard...

ATMega1284 Development Kit

Announcing the ATMEGA1284 Development Kit

The ATMega1284 is an impressive microcontroller with plenty of memory, plenty of pins and loaded...

Leave a reply

Shopping Cart