In this project we have implemented a digital clock with the help of 16 bit timer. Thus this project illustrates the basic use of timer and its interrupts. The timer will interrupt Microcontroller after every 1s and perform the given ISR (Interrupt Service Routine).
16-bit TIMER / COUNTER1
Getting Started
The basic block diagram of the 16 bit timer is shown below :
Here the 16 bit value of the timer/counter is continuously compared with output compare register. If the value matches then a corresponding interrupt is generated and ISR is executed.
Thus our primary concern herewill be the registers of the timer. The registers of timer which played vital role are:
1. TCCR1B ( Timer/Counter1 Control Register B )
Here the bit 4 and bit 3 (i.e.WGM13 and WGM12) describes about the mode of operation .There are different modes in which Timer can be operated, here we will operate the timer in Clear Timer on Compare Match (CTC ) mode. For that purpose we will enable the WGM12 bit
Of the above register by writing
TCCR1B = (1<<WGM12);
Bit2:0 – CSn2:0 is a Clock Select bit.The three Clock Select bits select the clock source to be used by the Timer/Counter.
Clock select bit description:
2. TCNT1 (TIMER /COUNTER 1)
The two Timer/Counter I/O locations (TCNT1H and TCNT1L, combined TCNT1) allows directaccess, for both read and write operations to the Timer/Counter.
3. OCR1A (Output Compare Register 1 A)
The Output Compare Registers contain a 16-bit value that is continuously compared with the counter value (TCNT1). A match can be used to generate an Output Compare interrupt, or togenerate a waveform output on the OCnx pin.
4. TIMSK (Timer/Counter1 Interrupt Mask Register)
When the OCIE1A bit (i.e. bit 1)in TIMSK register is written to one, and the I-flag in the Status Register is set (interruptsglobally enabled), the Timer/Counter1 Output Compare A Match interrupt is enabled. The correspondingInterrupt Vector is executed.
TIMSK=(1<<OCIE1A);
NOTE : Other information related to timer/counter could be obtained from data sheet.
SOFTWARE IMPLEMENTATION
INTRODUCTION
Implementation of digital clock in timer and alarm mode.The user will be asked to enter at what mode he want to operate DIGITAL CLOCK whether ALARM or CLOCK Mode. The alarm timing is specified within program in seconds.
Required Materials
The following materials are required:
1. MIKROC Software
2. Proteus AVR Software
Procedure
Initially we will decide the value to be given in OCR1A register so as to generate an interrupt of 1s.
By visiting the above site and providing the appropriate values like System clock frequency=8MHZ (in our case), Timer resolution=16bit, Prescaler=256
and Real Time(sec)=1s .we will get Total Timer Ticks =7A12(in hex).The above value will be written to OCR1A register. The above task could also be accomplished manually.
Then a circuit diagram was implemented on Proteus AVR software. Find the Diagram as Circuit Diagram 1 in the Circuit Diagram Tab1.
WSN DEVELOPMENT KIT BY PERVCOM
The code was when burned in the above kit, following output was observed:
As the clock was used in Alarm mode directly thus the buzzer was connected to the SPI port of the WSN Development kit.
CONCLUDING REMARK
This entire project is divided in to 2 parts, the first part demonstrates the software implementation of the digital clockon Atmega16 and the second part demonstrates the hardware implementation of digital clock on WSN-EK development kit (ATMEGA324PA).
Project Source Code
###
*****************Code 1******************==============================================================================#include "digitalclock.h"//////////////////////////////////////////////////////////////////////sbit LCD_RS at PORTB2_bit; //The sbit type defines a bit within a specialsbit LCD_EN at PORTB3_bit; //function register (SFR).sbit LCD_D4 at PORTD4_bit;sbit LCD_D5 at PORTD5_bit;sbit LCD_D6 at PORTD6_bit;sbit LCD_D7 at PORTD7_bit;sbitLCD_RS_Direction at DDB2_bit;sbitLCD_EN_Direction at DDB3_bit;sbit LCD_D4_Direction at DDD4_bit;sbit LCD_D5_Direction at DDD5_bit;sbit LCD_D6_Direction at DDD6_bit;sbit LCD_D7_Direction at DDD7_bit;unsigned char digit[6],second,minute,hour;unsignedint adcount3,adcount1, adcount2,temp,temp1;void main(){second=0;minute=0;hour=0;// Initialize_switch_led_relay();Lcd_Init();SREG_I_bit = 1;LCD_Cmd(_LCD_CURSOR_OFF); // send command to LCD (cursor off)LCD_Cmd(_LCD_CLEAR); // clears lcdDelay_ms(100);LCD_Out(1,1," DIGITAL CLOCK ");Delay_ms(200);LCD_Out(2,1,"1.ALARM 2.CLOCK");temp1=2; //alarm value is provided here in secondsDelay_ms(200) ;LCD_Cmd(_LCD_CLEAR);Start_timer(); //timer is startedwhile(1){if(~(S1_pressed)) //Switch S1 is made active low{LCD_Out(1,1," ALARM MODE ");Delay_ms(200);temp=(hour*60*60)+(minute*60)+second; /*timer value iscalculated in second and stored in temp variable.*/if(temp==temp1){buzzeron;Delay_ms(5000);buzzeroff;Stop_timer();}}if(~(S2_pressed)){LCD_Out(1,1," CLOCK MODE ");Delay_ms(200);}}}voidStart_timer(void){TCCR1B = (1<<WGM12); //timer initialized in ctc modeTCNT1H=0;TCNT1L=0;OCR1AH=0x7A;OCR1AL=0x12; //value assigned to compare registerTIMSK=(1<<OCIE1A); //interrupt enabledTCCR1B |=(1<<CS12); // Start timer with PRESCALER 256}voidStop_timer(void){TCCR1B =(0<<CS10)|(0<<CS11)|(0<<CS12);}Timer1COMPA_ISR() org IVT_ADDR_TIMER1_COMPA{second++;adcount1=second;digit[0]=adcount1%10;adcount1=adcount1/10;digit[1]=adcount1%10;adcount2=minute;digit[2]=adcount2%10;adcount2=adcount2/10;digit[3]=adcount2%10;adcount3=hour;digit[4]=adcount3%10;adcount3=adcount3/10;digit[5]=adcount3%10;LCD_chr(2,11,0x30+digit[0]); /*digit[0] converted to asciibefore display to LCD */Lcd_chr(2,10,0x30+digit[1]);LCD_Out(2,9,":");LCD_chr(2,8,0x30+digit[2]);Lcd_chr(2,7,0x30+digit[3]);LCD_Out(2,6,":");LCD_chr(2,5,0x30+digit[4]);Lcd_chr(2,4,0x30+digit[5]);if(second==60){minute++;second=0;if(minute==60){hour++;second=0;}if(hour==24){hour=0;minute=0;second=0;temp=0;}}}It could be clearly seen above that the code includes digitalclock.h============================================#define LED1_dir_output DDRC.DDC4=1/* LED1 is connected to bit4 of PORTC and the pin direction is definedasouptput */#define LED2_dir_output DDRC.DDC5=1/* LED2 is connected to bit5 of PORTC and the pin direction is definedasouptput */#define LED1_on PORTC.PORTC4=1/* LED1 may be switched ON by sending '1' to the associated pin */#define LED2_on PORTC.PORTC5=1/* LED2 may be switched ON by sending '1' to the associated pin */#define LED1_off PORTC.PORTC4=0/* LED1 may be switched OFF by sending '0' to the associated pin */#define LED2_off PORTC.PORTC5=0/* LED2 may be switched OFF by sending '0' to the associated pin */#define Relay_dir_output DDRC.DDC3=1/* Relay driver circuit is connected to bit3 of PORTC and the pin isdefined as ouptput pin */#define Relay_on PORTC.PORTC3=1/* Relay may be switched ON by sending '1' to the associated pin */#define Relay_off PORTC.PORTC3=0/* Relay may be switched OFF by sending '0' to the associated pin */#define S1_dir_input DDRC.DDC1=0/* Switch S1 is connected with bit 1 of PORTC and the pin directiondefined as input */#define S2_dir_input DDRC.DDC0=0/* Switch S2 is connected with bit 0 of PORTC and the pin directiondefined as input */#define S1_pullup_on PORTC.PORTC1=1/* The internal pull up resistance with S1 pin is turned ON *//* Pull-up resistors are used in electronic logic circuits to ensurethat inputs to logic systems settle *//* are maintained at expected logic levels if external devices aredisconnected or high-impedance */#define S2_pullup_on PORTC.PORTC0=1/* The internal pull up resistance with S2 pin is turned ON *//* Pull-up resistors are used in electronic logic circuits to ensurethat inputs to logic systems settle *//* at expected logic levels if external devices are disconnected orhigh-impedance */#define S1 PINC.PINC1 /* Read Status of S1 pin */#define S2 PINC.PINC0 /* Read Status of S2 pin */#define S1_pressed S1==0/* Checking whether S1 is pressed. If S1 is pressed the associated pinis pulled down to '0' state */#define S1_unpressed S1==1/* Checking whether S1 is unpressed. If S1 is unpressed the associatedpin is pulled up to '1' state */#define S2_pressed S2==0/* Checking whether S1 is pressed. When S2 is pressed the associatedpin is pulled down to '0' state */#define S2_unpressed S2==1/* Checking whether S2 is unpressed. When S2 is unpressed the associatedpin is pulled up to '1' state */#define RS PORTB.F2 /* RS pin of LCD connected to bit2 of PORTB */#define RS_Dir_output DDRB.F2=1 /* RS direction declared as output */#define EN PORTB.F3 /* EN pin of LCD connected to bit3 of PORTB */#define EN_Dir_output DDRB.F3=1 /* EN direction declared as output */#define D4 PORTD.F4 /* D4 pin of LCD connected to bit2 of PORTB */#define D4_Dir_output DDRD.F4=1 /* D4 direction declared as output */#define D5 PORTD.F5 /* D5 pin of LCD connected to bit2 of PORTB */#define D5_Dir_output DDRD.F5=1 /* D5 direction declared as output */#define D6 PORTD.F6 /* D6 pin of LCD connected to bit2 of PORTB */#define D6_Dir_output DDRD.F6=1 /* D6 direction declared as output */#define D7 PORTD.F7 /* D7 pin of LCD connected to bit2 of PORTB */#define D7_Dir_output DDRD.F7=1 /* D7 direction declared as output */#define ADC_VREF_TYPE 0xC0 //Internal 2.56V Voltage Reference with external//capacitor at AREF pin */#define Enter_4bit_mode {Lcd_command(0x33);Lcd_command(0x32);}/* To enter into 4 bit mode of operation *//* these two commands must be sent sequentially */#define Set_2line_5x7_dots Lcd_command(0x28) /*4 bit mode 2 line 5x7 dots*/#define Display_on_cursor_blinkingLcd_command(0x0F)/* display on cursor on blink on */#define Display_on_cursor_offLcd_command(0x0C)/* display on cursor on blink on */#define Clear_screenLcd_command(0x01) /* clear display */voidInitialize_switch_led_relay(void);/* This function configure switches, LEDs and relay pin directions *//* and other required configuration */voidInitialize_adc(void); /* This function initializes ADC module */unsignedintRead_adc(unsigned char);/* This function reads the digital value of the analog voltage *//*present at a particular channel passed as parameter and returns the*/voidStart_timer(void);voidStop_timer(void);#define buzzer_dir DDRB.DDB1=1#define buzzeron PORTB.PORTB1=1#define buzzeroff PORTB.PORTB1=0============================================***************************Code 2*******************************MIKROC PROGRAM:-#include "digitalclock.h"
///////////////////////////////////////////////////////////////////////////////
sbit LCD_RS at PORTB2_bit; //The sbit type defines a bit within a special
sbit LCD_EN at PORTB3_bit; //function register (SFR).
sbit LCD_D4 at PORTD4_bit;
sbit LCD_D5 at PORTD5_bit;
sbit LCD_D6 at PORTD6_bit;
sbit LCD_D7 at PORTD7_bit;sbitLCD_RS_Direction at DDB2_bit;
sbitLCD_EN_Direction at DDB3_bit;
sbit LCD_D4_Direction at DDD4_bit;
sbit LCD_D5_Direction at DDD5_bit;
sbit LCD_D6_Direction at DDD6_bit;
sbit LCD_D7_Direction at DDD7_bit;unsigned char digit0,digit1,digit2,digit3,digit4,digit5,second,minute,hour;
unsignedint adcount3,adcount1, adcount2,temp,temp1;
void main()
{
second=0;
minute=0;
hour=0;
// Initialize_switch_led_relay();
Lcd_Init();
SREG_I_bit = 1;LCD_Cmd(_LCD_CURSOR_OFF); // send command to LCD (cursor off)
LCD_Cmd(_LCD_CLEAR);
Delay_ms(100);
LCD_Out(1,1," DIGITAL CLOCK ");
LCD_Cmd(_LCD_CURSOR_OFF); // send command to LCD (cursor off)temp1=60;
Start_timer();
while(1)
{
temp=(hour*60*60)+(minute*60)+second;
if(temp==temp1)
{
buzzeron;
Delay_ms(5000);
buzzeroff;}
}}
voidStart_timer(void)
{
TCCR1B = (1<<WGM12);
TCNT1H=0;TCNT1L=0;
OCR1AH=0x7A;OCR1AL=0x12;
TIMSK=(1<<OCIE1A);
TCCR1B |=(1<<CS12); // Start timer with PRESCALER 256
}Timer1COMPA_ISR() org IVT_ADDR_TIMER1_COMPA
{second++;
adcount1=second;
digit0=adcount1%10;
adcount1=adcount1/10;
digit1=adcount1%10;adcount2=minute;
digit2=adcount2%10;
adcount2=adcount2/10;
digit3=adcount2%10;adcount3=hour;
digit4=adcount3%10;
adcount3=adcount3/10;
digit5=adcount3%10;
LCD_chr(2,11,0x30+digit0);
Lcd_chr(2,10,0x30+digit1);
LCD_Out(2,9,":");
LCD_chr(2,8,0x30+digit2);
Lcd_chr(2,7,0x30+digit3);
LCD_Out(2,6,":");
LCD_chr(2,5,0x30+digit4);
Lcd_chr(2,4,0x30+digit5);
if(second==60)
{
minute++;
second=0;if(minute==60)
{
hour++;
second=0;
}
if(hour==24)
{
hour=0;minute=0;second=0;
temp=0;}
}
}
______________________________________________________________________________
###
Circuit Diagrams
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.