Timers are used in many different applications for example in Industrial Applications, to switch ON or switch OFF any device or a machine load for a specific period of time. In the same way the timers are used in Domestic Appliances like in Air Conditioners, Microwave Ovens, Washing Machines, Food Processors etc.
The applications for the timers could also be seen in Military Applications and Space Applications, to trigger any Bomb or a Missile or to ignite booster rockets for lift offs at a specific time.
Here the given project demonstrates the fully featured and customized timer built using AVR Microcontroller ATMega32.
Fig. 1: Prototype of AVR ATMega32 based Timer for switching AC appliances ON or OFF
Features of the On/Off Timer
· Simple user interface using only 4 push buttons
· LCD for message display
· LEDs for indications
· Buzzer for audio notification
· Can count from 0 to 9999
· User selectable two counting speeds – 1 count/sec and 10 count/sec
· User selectable count up or count down
· Potential free (relay) contacts for device/machine/load on/off
User can connect any device or machine with the timer relay contacts. Set the desire count from 0 to 9999. Set counting direction either up or down and set counting speed either 1 count/sec or 10count/sec. Then when he press start switch to start timer operation, the device/machine will remain ON till the count finishes. As the count finishes, the device will shift to OFF mode and a beep sound is produced for audio notification. The whole counting process is displayed on LCD as count increments of(or) decrements. Two LEDs – one green and one red, are also given for indication of counting process.
Circuit Description
The complete circuit is built around AVR Microcontroller ATMega32. It consists of push buttons for user setting, LCD display, LEDs for indication, buzzer for audio notification and relay to switch on/off load or device. For complete Circuit Diagram click on the Circuit Diagram Tab.
· Four push buttons are connected to PORTA pins PA0 – PA3 such that when button is pressed that pin will get momentarily high logic input
· PORTC is connected to data pins D0 – D7 of LCD that sends command or data to LCD
· Two control pins En and Rs of LCD are connected to PORTD pins PD0 and PD1 respectively. Third control pin RW is connected to ground to make LCD write always enable
· PB0 pin drives 1 C/O (change over) type relay through NPN transistor connected in switch configuration
· Similarly PB3 pin drives DC buzzer through another NPN transistor connected in switch configuration
· One green and one red LEDs are connected to PB1 and PB2 pins to indicate counting process is going on or finished
· A 16 MHz crystal is connected to crystal input pins to provide internal clock for operation
· Complete circuit works on 5 V supply
Circuit Operation & Software Program
Circuit Operation
· As the circuit is switched on, the LCD shows message as “set count”. Both LEDs, buzzer and relay all are off
· User can set desire count by incrementing or decrementing it by pressing up arrow / down arrow key. The count can be set from 0 to 9999
· After count is set, user has to press enter key. When enter key is pressed LCD shows message as “count is set to xxxx” for 2 second and then next message is displayed as “count up or down?”. By default count up is selected
· To change counting direction again press down arrow key to set count down or press up arrow key to set count up. After selecting again press enter key
· When enter key is pressed, LCD shows message as “selected count up” or “selected countdown”. After 2 second LCD shows next message as “set counting rate”
· Default counting rate is slow – means counting is done at 1 count/sec (1 sec). User can change rate to fast – means 10 count/sec (0.1 sec) by pressing up arrow key or slow (1 sec) by pressing down arrow key
· After setting counting rate again press enter key. The message displayed as “counting rate is 1 sec” or “counting rate is 0.1 sec” for 2 second. After 2 second final message is displayed as “press start”
· When start button is pressed, the LCD shows counting process as count is incrementing or decrementing as per selected rate. The green LED lits ON to indicated counting is in progress. The relay is also switched ON to switch on the device or load for the time period
· When the desired count is reached, counting process stops. The message is displayed as “counting finish”. Green LED lits off and red LED lits ON to indicate counting (has stopped). A beep sound is produced through buzzer for audio notification
Software Program
The circuit operation is due to the software program loaded in to internal memory of AVR Microcontroller. The program implements all the required functionalities like
· To handle user inputs through push buttons
· To display messages and set values on LCD
· Switch on and off load connected through relay
· Gives indications on LEDs and on buzzer
For Program Code click Code Tab. The program is written in C language. All the above functionalities are implemented using different functions written in C language. AVR studio software tool is used to compile and simulate the program.
Circuit:
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#define lcd_databus PORTC
#define lcd_cntr_port PORTD
#define rs PD1
#define en PD0
#define output_port PORTB
unsigned int up_flag=1,dwn_flag=0,up_dwn_flag=0,count=0;
unsigned int count_set_flag=1,speed_flag=0,slow=1,fast=0,counting_rate;
void senddata(unsigned char data)
{
_delay_ms(2);
lcd_cntr_port=(1<<rs);
lcd_databus=data;
lcd_cntr_port=(1<<rs)|(1<<en);
lcd_cntr_port =(1<<rs)|(0<<en);
}
void sendcmd(unsigned char cmd)
{
_delay_ms(2);
lcd_cntr_port = (0<<rs);
lcd_databus=cmd;
lcd_cntr_port = (1<<en);
lcd_cntr_port = (0<<en);
}
void printstr(char *s)
{
uint8_t l,i;
l = strlen(s); // get the length of string
for(i=0;i<l;i++)
{
senddata(*s); // write every char one by one
s++;
}
}
void display_data(unsigned int value)
{
unsigned char ascii_value[4];
unsigned int tmp,t;
if(value>1000)
{
t=3;
while (value>1)
{
tmp = value%10;
ascii_value[t] = tmp+0x30;
value = value/10;
t--;
}
ascii_value[0] = value+0x30;
}
else if(value>100)
{
tmp = value%10;
ascii_value[3] = tmp+0x30;
value = value/10;
tmp = value%10;
ascii_value[2] = tmp+0x30;
value = value/10;
ascii_value[1] = value+0x30;
ascii_value[0] = 0x30;
}
else
{
tmp = value%10;
ascii_value[3] = tmp+0x30;
value = value/10;
ascii_value[2] = value+0x30;
ascii_value[1] = 0x30;
ascii_value[0] = 0x30;
}
for(t=0;t<4;t++) senddata(ascii_value[t]);
}
void up_arrow_key()
{
if(count_set_flag==1)
{
count++;
sendcmd(0xC0);
display_data(count);
}
else if(up_dwn_flag==1)
{
sendcmd(0xC0);
printstr("up ");
up_flag=1;
dwn_flag=0;
}
else if(speed_flag==1)
{
sendcmd(0xC0);
printstr("fast(0.1 sec)");
fast=1;
slow=0;
}
}
void down_arrow_key()
{
if(count_set_flag==1)
{
count--;
sendcmd(0xC0);
display_data(count);
}
else if(up_dwn_flag==1)
{
sendcmd(0xC0);
printstr("down");
up_flag=0;
dwn_flag=1;
}
else if(speed_flag==1)
{
sendcmd(0xC0);
printstr("slow(1 sec) ");
fast=0;
slow=1;
}
}
void enter_key()
{
if(count_set_flag==1)
{
sendcmd(0x80);
printstr("count is set to ");
sendcmd(0xC0);
display_data(count);
count_set_flag=0;
_delay_ms(2000);
sendcmd(0x01);
printstr("count up or dwn?");
sendcmd(0xC0);
printstr("up");
up_dwn_flag=1;
}
else if(up_dwn_flag==1)
{
sendcmd(0x01);
printstr("selected");
sendcmd(0xC0);
if(up_flag==1) printstr("count up");
else if(dwn_flag==1) printstr("count down");
up_dwn_flag=0;
_delay_ms(2000);
sendcmd(0x01);
printstr("set countng rate");
speed_flag=1;
sendcmd(0xC0);
printstr("slow(1 sec)");
}
else if(speed_flag==1)
{
sendcmd(0x01);
printstr("counting rate is");
sendcmd(0xC0);
if(slow==1)
{
counting_rate=9;
printstr("1 sec");
}
else if(fast==1)
{
counting_rate=1;
printstr("0.1 sec");
}
_delay_ms(2000);
sendcmd(0x01);
printstr("press start");
}
}
void delay(int d)
{
int j;
for(j=0;j<d;j++) _delay_ms(100);
}
void start_key()
{
unsigned int i;
sendcmd(0x01);
printstr("counting");
output_port |= 0x03;
if(up_flag==1)
{
for(i=0;i<=count;i++)
{
sendcmd(0xC0);
display_data(i);
delay(counting_rate);
}
}
else if(dwn_flag==1)
{
for(i=count;i>0;i--)
{
sendcmd(0xC0);
display_data(i);
delay(counting_rate);
}
}
output_port &= 0xFC;
output_port |= 0x0C;
sendcmd(0x01);
printstr("counting finish");
sendcmd(0xC0);
printstr("press reset");
_delay_ms(1000);
output_port &= 0xF7;
}
void main()
{
DDRD=0x03;
PORTD=0x00;
DDRC=0xFF;
PORTC=0x00;
DDRB = 0x0F;
PORTD = 0x00;
sendcmd(0x3E);
sendcmd(0x0E);
sendcmd(0x01);
printstr("set count");
sendcmd(0xC0);
display_data(count);
while(1)
{
while(PINA==0xF0);
switch(PINA)
{
case 0xF1:
up_arrow_key();
break;
case 0xF2:
down_arrow_key();
break;
case 0xF4:
enter_key();
break;
case 0xF8:
start_key();
break;
}
_delay_ms(150);
}
}
Không có nhận xét nào:
Đăng nhận xét