You are on page 1of 16

/* design date - 30.12.

2005 */

/* tested date - debugged using debugger and cleaned up on 10.2.2006 */

/* working date - 12.2.2006 */

/* School bell ringing, digital clock is running */

/* 5 keys, Hour set, minute set, Set Bell time, Clear Bell time, View Bell time */

#include <reg51.h>

unsigned int count,itime,x,y,digit,i;

unsigned short int


secs,mnt,hr,hrm,umnt,tmnt,uhr,thr,bellhr,bellmnt,ubellhr,tbellhr,ubellmnt,tbellmnt;

unsigned short int currenttime,ringtime,bellontime,oncount,maxentry;

unsigned char p0r,p2r,dot,pm,sec1,set,bellon,buzzeron,belltimeshow,presscount;

unsigned int ringtable[15];

unsigned char longbell[15];

/* P0.7 - dt g f e d c b a P0.0 */

/* 0 1 2 3 4 5 6 7 8 9 */

/* 7 segment formed using seg[10] array */

unsigned code char seg[10] =


{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

/* ----------------------------------------------------------------------------------------------------------*/

/* interrupt on timer/counter 0 TF0 flag */

void tmr () interrupt 1

count++;
presscount = 1;

if (count == 3600) /* actual 3600 */

/* 1 sec, for 11.0592 MHz clock (tested - 16 April, 2005) */

/* the reload count value is 0, so counter will count for 256. with 11.0592 MHz
crystal,

clock period is 90.4 ns, multiply by 12 machine cycles, again multiplied by 256
hardware

counter comes to 0.2777 ms, multiplied by 3600 by software counter, comes to


exactly 1 sec */

count = 0;

sec1 = 1;

if (itime > 0)

itime--;

x++;

if (x == 4) /* count for 1 msec */

x = 0;

y++;

if (y == 5)

y = 0;

p2r = (p2r & 0x0f); /* disable all for 1 msec */

P2 = p2r;

if (y == 1) /* keep one digit displayed for 4 msec with a gap of 1 msec */


{

digit++;

if (digit == 4)

digit = 0;

/* P2.7 6 5 4 3 2 1 0 */

/* c4 c3 c2 c1 - - - - */

switch (digit)

case 0: /* enable C1 */

p2r = (p2r | 0x10); /* enable display 1 setting C1 */

p2r = (p2r & 0x1f); /* disable others*/

P2 = p2r;

if (belltimeshow == 1 )

p0r = seg[ubellmnt];

else

p0r = seg[umnt];

if (pm == 0)

p0r = p0r | 0x80;

else

p0r = p0r & 0x7f;

P0 = p0r;

break;

case 1: /* enable C2 */

p2r = (p2r | 0x20); /* enable C2 */

p2r = (p2r & 0x2f); /* disable others*/

P2 = p2r;
if (belltimeshow == 1 )

p0r = seg[tbellmnt];

else

p0r = seg[tmnt];

P0 = p0r;

break;

case 2: /* enable C3 */

p2r = (p2r | 0x40); /* enable C3 */

p2r = (p2r & 0x4f); /* disable others*/

P2 = p2r;

if (belltimeshow == 1 )

p0r = seg[ubellhr];

else

p0r = seg[uhr];

p0r = p0r & dot; /* blinking of dot */

P0 = p0r;

break;

case 3: /* enable C4 */

p2r = (p2r | 0x80); /* enable C4 */

p2r = (p2r & 0x8f); /* disable others*/

P2 = p2r;

if (belltimeshow == 1 )

p0r = seg[tbellhr];

else

p0r = seg[thr];

if (p0r == 0xc0) /* mask leading zero */


p0r = 0xff;

P0 = p0r;

break;

/* ----------------------------------------------------------*/

/* every second check whether buzzer or ring to be controlled */

void control_buz_ring(void)

sec1 = 0;

secs++;

if (dot == 0xff)

dot = 0x7f; /* dot LED blinking */

else

dot = 0xff;

if (buzzeron == 1)

if (secs > 45)

p2r = p2r & 0xfd; /* switch on buzzer */

P2 = p2r;

if (bellon == 1)

{
buzzeron = 0;

p2r = p2r | 0x02;/* switch off buzzer */

P2 = p2r;

if (secs < bellontime)

p2r = p2r | 0x01; /* switch on relay */

P2 = p2r;

else

bellon = 0;

p2r = p2r & 0xfe; /* switch off relay */

P2 = p2r;

/* ----------------------------------------------------------*/

/* every minute update mnt counter and check whether it is ring time */

void update_mnt (void)

secs = 0;

mnt++;

umnt = mnt%10;

tmnt = mnt/10;

uhr = hr%10;
thr = hr/10;

currenttime = (hrm * 60) + mnt;

for (i = 0; i < 15; i++)

ringtime = ringtable[i];

if (currenttime == ringtime - 1)

buzzeron = 1;

if (currenttime == ringtime)

bellon = 1;

bellontime = 5;

if (longbell[i] == 1)

bellontime = 10;

/* ----------------------------------------------------------*/

/* every minute update mnt counter and check whether it is ring time */

void update_hr (void)

mnt = 0;

hr++;

hrm++;

if (hr == 13) /* actual 13 */

{
hr = 1;

if (pm == 0)

pm = 1;

hrm = (hr + 12);

else

pm = 0;

hrm = hr;

umnt = mnt%10;

tmnt = mnt/10;

uhr = hr%10;

thr = hr/10;

/* ----------------------------------------------------------*/

/* hour_set called if micro switch is pressed */

void hour_set (void)

secs = 0;

hr++;
hrm++;

if (hr == 13)

hr = 1;

if (pm == 0)

pm = 1;

P3 = 0;

hrm = (hr + 12);

else

pm = 0;

P3 = 0xff;

hrm = hr;

set = 1;

/* ----------------------------------------------------------*/

/* minute_set is called if micro switch is pressed */

void minute_set(void)

secs = 0;

mnt++;

if (mnt == 60)
mnt = 0;

set = 1;

/* ----------------------------------------------------------*/

/* Bell time setting */

/* P1.7 6 5 4 3 2 1 0 */

/* - - - View Bellset longbellset Mntset Hourset */

void longbell_set(void)

itime = 5000;

oncount = 0;

while (itime > 0)

if (presscount == 1)

presscount = 0;

if ((P1 & 0x04) == 0)

oncount++;

if (oncount > 4000)

longbell[(maxentry-1)] = 1;

p2r = p2r & 0xfd; /* switch on buzzer */

P2 = p2r;
itime = 3000; /* 1 sec */

while (itime > 0);

p2r = p2r | 0x02;/* switch off buzzer */

P2 = p2r;

/* ----------------------------------------------------------*/

void belltime_set (void)

itime = 5000;

oncount = 0;

while (itime > 0)

if (presscount == 1)

presscount = 0;

if ((P1 & 0x08) == 0)

oncount++;

if (oncount > 4000)

ringtable[maxentry] = (hrm * 60) + mnt;

maxentry++;

if (maxentry > 15)

maxentry = 0;
p2r = p2r & 0xfd; /* switch on buzzer */

P2 = p2r;

itime = 3000; /* 1 sec */

while (itime > 0);

p2r = p2r | 0x02;/* switch off buzzer */

P2 = p2r;

/* ----------------------------------------------------------*/

void belltime_view (void)

p2r = p2r & 0xfd; /* switch on buzzer */

P2 = p2r;

itime = 3000; /* ~ 0.54 secs */

while (itime > 0);

p2r = p2r | 0x02;/* switch off buzzer */

P2 = p2r;

belltimeshow = 1;

for (i = 0; i < 15; i++)

ringtime = ringtable[i] ;

if (ringtime > 0)

bellhr = ringtime/60;

if (bellhr > 12)


bellhr = bellhr - 12;

bellmnt = ringtime%60;

ubellhr = bellhr%10;

tbellhr = bellhr/10;

ubellmnt = bellmnt%10;

tbellmnt = bellmnt/10;

itime = 3600; /* ~ 1 secs */

while (itime > 0);

p2r = p2r & 0xfd; /* switch on buzzer */

P2 = p2r;

itime = 3000; /* ~ 0.54 secs */

while (itime > 0);

p2r = p2r | 0x02;/* switch off buzzer */

P2 = p2r;

belltimeshow = 0;

/* ----------------------------------------------------------*/

void main (void)

/* interrupt initialize */

TMOD = 0x02; /* mode 2 with reload option, gate = 0, c/t = 0 */

TL0 = 0; /* count 256 */


TH0 = 0; /* reload value is 256 */

TR0 = 1;

ET0 = 1;

EA = 1;

/* other variables initialize */

count = 0;

sec1 = 0;

digit = 0;

dot = 0xff;

secs = 0;

mnt = 53;

hr = 8;

hrm = hr;

pm = 0;

P3 = 0xff;

uhr = hr%10;

thr = hr/10;

umnt = mnt%10;

tmnt = mnt/10;

P1 = 0xff; /* P1 is input */

p2r = 0x02;

P2 = p2r;

buzzeron = 0;

bellon = 0;

currenttime = (hrm * 60) + mnt;


maxentry = 0;

/* clear the ring table */

for (i = 0; i < 15; i++)

ringtable[i] = 0;

/* start main endless loop */

while (1)

if (sec1 == 1)

control_buz_ring();

if (secs == 60) /* acutal 60 */

update_mnt ();

if (mnt == 60) /* actual 60 */

update_hr ();

/* every loop read the control switches to set time and bell time */

if ((P1 & 0x01) == 0) /* hour set */

hour_set();

if ((P1 & 0x02) == 0) /* minute set */

minute_set();

if (set == 1)

uhr = hr%10;

thr = hr/10;

umnt = mnt%10;

tmnt = mnt/10;
secs = 0;

itime = 1300;

while (itime > 0); /* wait for less than a second */

set = 0;

/* P1.7 6 5 4 3 2 1 0 */

/* - - - Viewbelltime Belltimeset Clear Mntset Hourset */

if ((P1 & 0x04) == 0) /* clear all entry */

longbell_set();

if ((P1 & 0x08) == 0) /* set bell time */

belltime_set();

if ((P1 & 0x10) == 0) /* view bell time */

belltime_view();