Digital Alarm With 8051
Digital Alarm With 8051
Digital Alarm With 8051
Comprehensive in the form that its code is very lengthy. Programming is not an easy
stuff for every one , and when it comes to program a microcontroller it becomes
more complex to write a code for desired output if you are not familiar with the
basics of programming. Digital alarm with 8051(89c51,89c52) microcontroller is an
extension of digital clock with 8051(89c51,89c52) microcontroller project. The alarm
works in the same way as your digital watches and mobile phone alarms works. Well
All the above tutorials play a vital role in 8051 digital alarm clock project. I will say
the code of all the above tutorials is part of this project. I just arranged the code in
sequence with some minor modifications and made a stream line code flow to
achieve digital alarm clock functionality.
Then single bits of ports are declared for connecting our buzzer and 4×3 keypad.
Next character arrays are declared they are used as messages to be displayed on
16×2 lcd on suitable conditions. delay() function is used to generate some delay for
suitable purposes. lcdcmd() function is sending commands to lcd. lcddata() function
is sending and printing data on lcd screen. checkconditions() function is checking
conditions like if seconds reached to 60 increment the minute, if minutes reached to
60 increment hour, check if clock reached the alarm time ring the buzzer.
#include<reg52.h>
int keypad();
void tochar(unsigned int value);
sbit buzzer=P0^0;
sbit r0=P2^0; sbit r1=P2^1; sbit r2=P2^2; sbit r3=P2^3; sbit c0=P2^5; sbit c1=P2^6;
sbit c2=P2^7; sbit en=P3^6; sbit rs=P3^5; sbit rw=P3^7;
char hour[]="HOURS-> 01 - 12",mint[]="MINTS-> 01 - 60";
char alarm[]=" DIGITAL ALARM";
int count=0,H,M,S=1,hour1[1],mint1[1],H1,M1,hour2[1],mint2[1];
void checkconditions(){
unsigned int i;
i=scankey();
while(i==2)
{
count=0;
lcdcmd(0x01); lcdcmd(0x80);
while(hour[count]!='\0')
{
lcddata(hour[count]);count++;
}
count=0; lcdcmd(0xC8);
while(count!=2)
{
hour2[count]=keypad();count++;
}
H1=(hour2[0]*10)+hour2[1];
count=0; lcdcmd(0x01); lcdcmd(0x80);
while(mint[count]!='\0')
{
lcddata(mint[count]);count++;
}
count=0; lcdcmd(0xC8);
while(count!=2)
{
mint2[count]=keypad();count++;
}
M1=(mint2[0]*10)+mint2[1];
count=0; delay(1000); lcdcmd(0x01); lcdcmd(0x80); count=0;
while(alarm[count]!='\0')
{
lcddata(alarm[count]);count++;
}
count=0;
lcdcmd(0xC4);tochar(H);lcddata(':');tochar(M);lcdcmd(0xCB);tochar(S);
i=1;
}
if(S>=60) { M++; }
if(M>=60) { H++; }
void clockdelay()
{
int count=0,sec;
for(sec=0;sec<60;sec++){
count=0;
while(count!=495)
{
TMOD=0x01; //16-bit timer0 selected
TH0=0xF8; // Loading high byte in TH
TL0=0xCC; // Loaded low byte in TL
TR0=1; // Running the timer
while(!TF0); //Checking the timer flag register if it is not equal to 1
TR0 = 0; // If TF0=1 stop the timer
TF0 = 0; // Clear the Timer Flag bit for next calculation
count++;
}
S++;lcdcmd(0xCB);tochar(S);
checkconditions();
if(H==H1)
{
if(M==M1)
buzzer=0;
}}}
void lcdint()
{
lcdcmd(0x30); delay(3000); lcdcmd(0x30); delay(3000); lcdcmd(0x30); delay(3000);
lcdcmd(0x30); delay(3000); lcdcmd(0x30); delay(3000); lcdcmd(0x38); delay(3000);
lcdcmd(0x01); delay(3000); lcdcmd(0x0C); delay(3000); lcdcmd(0x80); delay(3000);
}
int keypad()
{
char c='a';
while(c!='s'){
r0=0;r1=1;r2=1;r3=1;
if(c0==0){lcddata('1');delay(20000);c='s';return 1;}
if(c1==0){lcddata('2');delay(20000);c='s';return 2;}
if(c2==0){lcddata('3');delay(20000);c='s';return 3;}
r0=1;r1=0;r2=1;r3=1;
if(c0==0){lcddata('4');delay(20000);c='s';return 4;}
if(c1==0){lcddata('5');delay(20000);c='s';return 5;}
if(c2==0){lcddata('6');delay(20000);c='s';return 6;}
r0=1;r1=1;r2=0;r3=1;
if(c0==0){lcddata('7');delay(20000);c='s';return 7;}
if(c1==0){lcddata('8');delay(20000);c='s';return 8;}
if(c2==0){lcddata('9');delay(20000);c='s';return 9;}
r0=1;r1=1;r2=1;r3=0;
if(c1==0){lcddata('0');delay(20000);c='s';return 0;}
c='a';
}
}
void start(){
count=0; lcdcmd(0x01); lcdcmd(0x80);
while(alarm[count]!='\0')
{
lcddata(alarm[count]);count++;
}
count=0;
lcdcmd(0xC4);
tochar(H);
lcddata(':');
tochar(M);
lcdcmd(0xCB);
tochar(S);
}
void main()
{
unsigned int i=0;
P0=0x00; P1=0x00; P2=0xF0; P3=0x0F;
lcdint(); buzzer=1;
settime(); start();
while(1)
{
clockdelay();
buzzer=1;
}
}
The alarm is 12 hours alarm which means you can put-on alarm for maximum next 12
hours period. Do not enter hours greater than 12 and do not enter minutes greater
than 60. If you want to make the alarm more intelligent just insert more functions in
it like set alarm for am or pm time. Do some other work when ever clock time
reaches the alarm time like switch on a bulb, start your water motor to fill the tank or
switch off the oven. If you are really willing to do some extra stuff than go a head
and give me your feedback.
Download the project code and hex file compiled in keil Uvision-2. If you
have any questions regarding code or any thing just leave a comment
below.Your comments are highly apreciated.