Monday, 23 December 2013

PROJECT LCD DIGITAL CLOCK

              IN THIS POST I WOULD LIKE TO INTRODUCE THE NEW PROJECT WHICH IS LCD DIGITAL CLOCK USING ATMEGA. THE SOURCE CODE IS VERY INTERESTING AND READ THE COMMENTS FOR THE BETTER UNDERSTANDING....


            AVR MICROCONTROLLER IS ONE THE MOST FAMOUS AND ALSO THE ADVANCED ONE. IF YOU MORE INFORMATION ABOUT AVR MICROCONTROLLER MEANS GO THROUGH MY PREVIOUS POSTS....

           In this ATMega16 AVR project we will be designing and implementing a digital clock with the aid of a Atmel AVR ATMega16 microcontroller and an alphanumeric liquid crystal display (LCD).

Note: Although this AVR project was designed around the ATMega16 the project could have utilized another microcontroller such as an ATMega32, ATMega8515, etc.


ATMega16 LCD Digital Clock Operation

Our digital clock operates as follows. When the circuit is powered up the clock starts      at "00:00:00" for "HH:MM:SS". There are two push-button switches used to set the time. Switch SW1 is for setting the minutes, when this button is pressed the minutes in increase until it reaches 59 then reset and start counting from 0. Switch SW2 is for setting thehours.
 
Hardware Description for the ATMega16 LCD Digital Clock
   
The figure below gives the circuit diagram for the ATmega16 LCD Digital Clock.







Important Note: In order for our digital clock to work correctly the internal oscillator of the ATMega16 AVR microcontroller must be enabled and set to 4MHz.

Below is the AVR C implementation of the entire code for the ATMega16 based LCD digital clock. The code was implemented and built using AVR Studio 5. Be reminded that the internal clock of the ATMega16 microcontroller should be enabled and programmed to operate at 4MHz for the AVR C code to operate correctly. 

HERE IS THE CODING FOR THE ABOVE PROJECT AND USE IT TO DEVELOP

#define F_CPU  4000000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>
 
/*Global Variables Declarations*/
unsignedchar hours =0;
unsignedchar minutes =0;
unsignedchar seconds =0;
char time[]="00:00:00";
 
/*LCD function declarations */
voidLCD_send_command(unsignedcharcmnd);
voidLCD_send_data(unsignedchar data);
voidLCD_init();
voidLCD_goto(unsignedchar y,unsignedchar x);
voidLCD_print(char*string);
voidLCD_update_time(void);
 
/*Timer Counter 1 Compare Match A Interrupt Service Routine/Interrupt Handler*/
ISR(TIMER1_COMPA_vect);
 
#define LCD_DATA_PORT  PORTB
#define LCD_DATA_DDR   DDRB
#define LCD_DATA_PIN   PINB
 
#define LCD_CNTRL_PORT PORTC
#define LCD_CNTRL_DDR  DDRC
#define LCD_CNTRL_PIN  PINC
 
#define LCD_RS_PIN             5
#define LCD_RW_PIN             6
#define LCD_ENABLE_PIN         7
#define SET_HOUR               3
#define SET_MINUTE             4
 
int main(void)
{         
unsignedchar i; 
LCD_init();         
LCD_goto(1,2);         
LCD_print("AVR-Tutorials");        
 LCD_goto(2,4);          
LCD_print(time);        
 LCD_CNTRL_PORT =(1<<SET_HOUR |1<<SET_MINUTE);         
TCCR1B =(1<<CS12|1<<WGM12);         
OCR1A =15625-1;         
TIMSK =1<<OCIE1A;         
sei();         
while(1)
{                
if(!(LCD_CNTRL_PIN &(1<<SET_HOUR)))                
{                       
 hours++;                        
if(hours >23)                                
hours=0;                
}                
if(!(LCD_CNTRL_PIN &(1<<SET_MINUTE)))                
{                       
 minutes++;                        
if(minutes >59)                                
minutes=0;               
 }                
_delay_ms(250);       
}
}
 
/* This function sends a command 'cmnd' to the LCD module*/
voidLCD_send_command(unsignedcharcmnd)
{         
LCD_DATA_PORT =cmnd;         
LCD_CNTRL_PORT &=~(1<<LCD_RW_PIN);         
LCD_CNTRL_PORT &=~(1<<LCD_RS_PIN);         
LCD_CNTRL_PORT |=(1<<LCD_ENABLE_PIN);         
_delay_us(2);         
LCD_CNTRL_PORT &=~(1<<LCD_ENABLE_PIN);         
_delay_us(100);
}
 
/* This function sends the data 'data' to the LCD module*/
voidLCD_send_data(unsignedchar data)
{         
LCD_DATA_PORT = data;         
LCD_CNTRL_PORT &=~(1<<LCD_RW_PIN);         
LCD_CNTRL_PORT |=(1<<LCD_RS_PIN);         
LCD_CNTRL_PORT |=(1<<LCD_ENABLE_PIN);         
_delay_us(2);         
LCD_CNTRL_PORT &=~(1<<LCD_ENABLE_PIN);         
_delay_us(100);
}
 
voidLCD_init()
{         
LCD_CNTRL_DDR =0xFF;         
LCD_CNTRL_PORT =0x00;         
LCD_DATA_DDR =0xFF;        
 LCD_DATA_PORT =0x00;         
_delay_ms(10);         
LCD_send_command(0x38);         
LCD_send_command(0x0C);         
LCD_send_command(0x01);         
_delay_ms(10);       
 LCD_send_command(0x06); 
}
 
/* This function moves the cursor the line y column x on the LCD module*/
voidLCD_goto(unsignedchar y,unsignedchar x)
{         
unsignedcharfirstAddress[]={0x80,0xC0,0x94,0xD4};         
LCD_send_command(firstAddress[y-1]+ x-1);         
_delay_ms(10); 
}
 
voidLCD_print(char*string)
{         
unsignedchar i;         
while(string[i]!=0)         
{               
 LCD_send_data(string[i]);                
i++;         
}
}
voidLCD_update_time()
{         
unsignedchar temp;         
LCD_goto(2,4);        
itoa(hours/10,temp,10);         
LCD_print(temp);         
itoa(hours%10,temp,10);        
 LCD_print(temp);        
 LCD_print(":");         
itoa(minutes/10,temp,10);        
 LCD_print(temp);        
 itoa((minutes%10),temp,10);        
 LCD_print(temp);         
LCD_print(":");         
itoa(seconds/10,temp,10);         
LCD_print(temp);         
itoa(seconds%10,temp,10);        
 LCD_print(temp);
}
/*Timer Counter 1 Compare Match A Interrupt Service Routine/Interrupt Handler*/
ISR(TIMER1_COMPA_vect)
{                       
seconds++;        
if(seconds ==60)         
{                
seconds=0;                
minutes++;         
}         
if(minutes ==60)         
{                
minutes=0;                
hours++;                        
}        
 if(hours >23)                
hours=0;         
LCD_update_time();
}

No comments:

Post a Comment

share your thoughts