Digital Alarm With 8051

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Digital alarm with 8051(89c51,89c52) microcontroller is a comprehensive project.

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 alarms works same   


8051 microcontroller digital alarm – Project
requirements
 8051(89c51 or 89c52) microcontroller
 alarm(I used buzzer) 
 16×2 lcd
 Potentiometer/variable resistor (For setting lcd contrast)
 4×3 keypad(You can also use 4×4. 4×4 contains 4×3 in it :D)
 Crystal(11.0592 MHz)
 Bread board(Designing circuit. You can also design circuit on PCB(Printed
circuit board))

Tutorials regarding 8051 digital alarm clock project


Its not possible to explain each and every statement of code. For example 16×2 lcd
interfacing with 8051 microcontroller or 4×4 numeric keypad interface with 8051
microcontroller or how to generate proper time delay using internal timers of 89c51
microcontroller can not be explained in a single tutorial. So i made multiple tutorials
on each component which is part of this project.   

 How to generate one second delay with 8051 microcontroller.


 How to generate one minute delay with 8051 microcontroller. 
 How 16×2 lcd works.
 Digital clock with 8051 microcontroller.

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.   

Digital alarm clock – Code flow


when you first run the program you have to set the clock time same like the digital
clock project(Link given above). Once you set the clock time it will start running for
ever unless you reset the microcontroller. Now when ever you want to set an alarm
press the button located in first coulomb and at fourth row of 4×3 keypad. Pressing
the button will lead to an event which displays messages on 16×2 lcd to set hours
and minutes. Now set alarm time.
Note: No external RTC(real time clock) is interfaced with 8051 microcontroller for
time calculation. Rather internal timers of 89c51 mirocontroller are utilized for
producing clock time(Visit above digital clock with 89c51 tutorial). 

89c51 microcontroller digital alarm – Circuit diagram


The circuit for the project is very simple. Connect 16×2 lcd with Port 1 of 8051 (89c51
or 89c52) microcontroller. Rd(read) Wr(write) and En(enable) signals are provided to
16×2 lcd using port 3 pins 5 , 6 & 7 of 8051 (89c51 or 89c52) microcontroller. Port 0
pin 0 is used to operate buzzer when ever alarm time reached to its matched clock
time. Port 2 is dedicated for our 4×3 keypad. Rows are connected to pin 0, 1 ,2 & 3
of port 2. Coulombs are connected to pins 5 , 6 & 7 of port 2. Rest of the
connections are simple apply 5v to pin 40 and pin 31. Connect crystal 11.0592 MHz
to pins 18 and 19. Ground pin 20. Connect reset button to pin 9. 
8051 microcontroller digital alarm clock
89c51 microcontroller digital alarm clock – Project
code
Coming to the code. First i included the necessary header file reg52.h. This header
file should be included when ever you are writing and compiling your 8051
microcontroller program in keil ide. It includes all the necessary linker and debuggers
for 8051 microcontroller configuration.
Then keypad() function is declared. This function scans the keys pressed on the 4×3
keypad. Next comes tochar() function. It is used to deal with integer type numbers
to be displayed on the 16×2 lcd. Integer numbers must be converted to 8-bit
character format for displaying on 16×2 lcd.

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.

clockdelay() function is producing seconds, minutes and hours for


us. lcdinit() initializing the lcd. settime() function is setting time of clock on every
reset of the program. User have to input the time using 4×3 keypad. start() function
is printing the time on 16×2 lcd at start of the program execution.
        
The above tutorials are very important if you want to understand the functions used
in the code. If you don’t care about code than just burn the hex code make the

circuit and enjoy your creation      

#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 delay(unsigned int no)


{
unsigned int i,j;
for(j=0;j<=no;j++)
for(i=0;i<=2;i++);
}

void lcdcmd(unsigned int command){


P1=command; rw=0; rs=0; en=0; delay(300); en=1; delay(300); en=0;
}

void lcddata(char data1)


{
P1=data1; rw=0; rs=1; en=0; delay(300); en=1; delay(300); en=0;
}
int scankey(){
r0=1;r1=1;r2=1;r3=0;
if(c0==0) { return 2; }
}

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++; }

if((H==13) && (M>=60) && (S>=60))


{
S=1; H=1; M=1;
lcdcmd(0x01); count=0;
while(alarm[count]!='\0')
{
lcddata(alarm[count]);count++;
}
count=0;
lcdcmd(0xC4);tochar(H);lcddata(' ');lcddata(':');tochar(M);lcddata('
');lcdcmd(0xCB);tochar(S);lcddata(' ');
}
if(S>=60)
{ S=1;
lcdcmd(0xC4);tochar(H);lcddata(':');tochar(M);lcdcmd(0xCB);tochar(S);lcddata(' ');
}
if(M>=60)
{ M=1;
lcdcmd(0xC4);tochar(H);lcddata(':');tochar(M);lcddata('
');lcdcmd(0xCB);tochar(S);lcddata(' ');
}}

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 settime() //Setting time


{
lcdcmd(0x01); lcdcmd(0x80);
while(hour[count]!='\0')
{
lcddata(hour[count]);count++;
}
count=0; lcdcmd(0xC8);
while(count!=2)
{
hour1[count]=keypad();count++;
}
H=(hour1[0]*10)+hour1[1];
count=0; lcdcmd(0x01); lcdcmd(0x80);
while(mint[count]!='\0')
{
lcddata(mint[count]);count++;
}
count=0; lcdcmd(0xC8);
while(count!=2)
{
mint1[count]=keypad();count++;
}
M=(mint1[0]*10)+mint1[1];
count=0; delay(1000); lcdcmd(0x01); lcdcmd(0x80);
}

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 tochar(unsigned int value)


{
char tendigit,unitdigit;
tendigit=value/10;
if((tendigit+48)>='1')
lcddata(tendigit+48);
unitdigit=value%10;
lcddata(unitdigit+48);
}

void main()
{
unsigned int i=0;
P0=0x00; P1=0x00; P2=0xF0; P3=0x0F;
lcdint(); buzzer=1;
settime(); start();
while(1)
{
clockdelay();
buzzer=1;
}
}

view rawdigital-alarm-with-8051-microcontroller.c hosted with   by GitHub

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.

You might also like