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

New Year, New Product Post

Welcome to 2014, I think it's going to be a good one. Let's start the...

Female Headers and Rubber Feet

2.54mm connector pitch is pretty ubiquitous in electronics. This is the pitch used by DIP...

Pogo Pins and Photographing LCDs

This post is a new product post, a tutorial and a behind the scenes look,...

Leave a reply

Shopping Cart