Real Time Clock using ATMega16

Real Time Clock using ATMega16

Description

The ATMega16 chip has a real-time counter that operates asynchronously when a 32,768hz watch crystal is connected to it, providing a real-time clock. The watch crystal needs to connect to pins TOSC1(pin 28) and TOSC2(pin 29).

 

 Hardware requirements

  • ATMega16
  • 32,768 KHz crystal
  • LCD display
>> The following software (using codevision avr compiler ) uses timer 2 CTC compare match and ther are three switches used to adjust seconds,minutes and hours.
>> Timer 2 use prescalar 1024 and the OCR value will be 32 or (20 hex).

Code Sample

#include
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1b ;PORTA
#endasm
#include
char s=0,m=0,h=0;
char t,u;
// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here
s=s+1;
    if (s==60)
    {
    s=0;
    m=m+1;
        if (m==60)
        {
        m=0;
        h=h+1;
            if (h==24)
            {
            h=0;
            }
        }
    }
   
}
// External Interrupt 1 service routine
interrupt [2] void int0(void)
    {
    s=s+1;
    if(s==60)
        {
        s=0;
        }
    }
interrupt [3] void int1(void)
    {
    m=m+1;
    if(m==60)
        {
        m=0;
        }
    }
interrupt[19] void int2(void)
    {
    h=h+1;
    if(h==24)
        {
        h=0;
        }
    }
///////////////////////////////
char tenth(char a)
{
t=a/10;
return t;
}
//////////////////////////////
//////////////////////////////
char unit(char b)
{
t=b/10;
u=b-t*10;
return u;
}
////////////////////////////////
// Declare your global variables here
void main(void)
{
// Timer/Counter 2 initialization
// Clock source: TOSC1 pin
// Clock value: PCK2/1024
// Mode: CTC top=OCR2
// OC2 output: Disconnected
ASSR=0x08;
TCCR2=0x0F;
TCNT2=0x00;
OCR2=0x20;
// LCD module initialization
lcd_init(16);
TIFR|=0x80;
// Global enable interrupts
#asm(“sei”)
while (1)
      {
      // Place your code here
      lcd_gotoxy(0,0);
      t=tenth(h);
      lcd_putchar(t+0x30);
      u=unit(h);
      lcd_putchar(u+0x30);
      lcd_putsf(“:”);
      t=tenth(m);
      lcd_putchar(t+0x30);
      u=unit(m);
      lcd_putchar(u+0x30);
      lcd_putsf(“:”);
      t=tenth(s);
      lcd_putchar(t+0x30);
      u=unit(s);
      lcd_putchar(u+0x30);
      };
}

Proteus Simulation

Real Time Clock using ATMega16

>>>> Here is a link containing the project avr and its proteus simulation
Download Here
Download this tutorial in PDF HERE