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.
Circuit Diagram:
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
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);
}
}
No comments:
Post a Comment