Professional Documents
Culture Documents
MICROCONTROLLER-BASED
INDUSTRIAL TIMER
JAYARAMAN KIRUTHI VASAN
UMAR
SUNIL K
Circuit
description
Fig. 1 shows the circuit
of
the
microcontroller-based
industrial timer. The
microcontroller is
Atmel AT89S52 (IC1),
which is a 40-pin deWWW.EFYMAG.COM
JUNE 2007
CONSTRUCTION
and
relay
RL1
energises. Diode D1
serves as a free-wheeling diode. Any appliance can be connected
with contacts of relay
RL1.
Power-on-reset is
achieved by connecting resistor R1 and capacitor C1 to pin 9 of
the microcontroller.
Other ends of the capacitor and resistor are connected to Vcc and ground,
respectively. Switch S5 is used for
manual reset. The microcontroller is
operated with the clock derived from
a 20MHz crystal oscillator.
Power supply. Fig. 2 shows the circuit of the power supply. The AC
mains is stepped down by transformer
X1 to deliver a secondary output of
7.5V at 350 mA. The transformer output is rectified by a full-wave bridge
rectifier BR1, filtered by capacitor C5
and regulated by IC2. Capacitor C6 bypasses any ripple present in the regulated output. Unregulated power supply is used for relay RL1.
An actual-size, single-side PCB layout for the microcontroller-based in-
PARTS LIST
Semiconductor:
IC1
IC2
T1-T4
T5
BR1
D1
DIS1-DIS4
AT89S52 micrcontroller
7805 5V regulator
BC557 pnp transistor
BC547 npn transistor
1A bridge rectifier
1N4007 rectifier diode
LTS542 common-anode
display
10F,16V electrolytic
33pF ceramic disk
0.1F ceramic disk
1000F, 25V electrolytic
Operation
Switch on the circuit using using ON/
OFF switch S6. The
microcontroller is reset
by power-on-reset and
then timer is in seconds
mode. The select key selects the mode between
seconds and minutes.
This is displayed as 0
for seconds and 1 for
minutes
on
the
hundreds digit display
(DIS3), respectively.
Up key increments
the time setting in seconds and minutes.
Down key decrements the time setting in
seconds and minutes.
After setting the desired time with the help
of up and down keys,
press start key. This
energises the relay. The
timer counts down for
the set time and once the
display becomes zero,
WWW.EFYMAG.COM
CONSTRUCTION
the relay de-energises.
The timer will stop before preset time by pressing start key again.
Software
The source program is
written in C language
and compiled with Keil
Microvision 3 IDE. It is
well commented and
easy to understand.
D o w n l o a d
C51V808A.EXE from
www.keil.com/demo/
eval/c51.htm. This file is
a freely available and
self-extracting setup program
for
Keil
Microvision 3 IDE.
Normally, when there
is no interrupt, the
microcontroller executes
while loop in the main
function. Here it scans
the keys and acts according to the key
pressed.
Two interrupts are enabled in the
software, namely, timer 0 and timer 2.
Timer 0 counts milliseconds, which are
then accumulated to seconds or minutes according to the user selection.
Timer 2 drives the displays in multiplex mode.
For time counting, timer 0 is
initialised
by
the
void
Timer0_init(unsigned
char
Timer0h,unsigned char Timer0l) function. Timer 0 interrupts the
microcontroller every millisecond.
When interrupted by timer 0, the
microcontroller executes the void
isr_t0(void) function wherein it increments two counter variables, namely,
Timer0Counter and LedCounter.
SOURCE PROGRAM
/*** Include Files ***/
#include <At89x52.h>
/*** RENAMING OF PORTS ****/
#define SegPort P0
#define DigPort P2
/* CODE FOR LIGHTING EACH SEGMENT OF
THE SEVEN SEGMENT LED DISPLAY */
#define seg_a 0xfe
#define seg_b 0xfd
#define seg_c 0xfb
#define seg_d 0xf7
#define seg_e 0xef
#define seg_f 0xdf
#define seg_g 0xbf
#define seg_dot 0x7f
/* SEVEN SEGMENT CODE FOR EACH NUMBER FROM 0 TO 9 ,DOT AND SPACE */
#define NUM_0 (seg_a & seg_b & seg_c & seg_d
& seg_e & seg_f)
#define NUM_1 (seg_b & seg_c)
#define NUM_2 (seg_a & seg_b & seg_d & seg_e
& seg_g)
#define NUM_3 (seg_a & seg_b & seg_c & seg_d
& seg_g)
#define NUM_4 (seg_b & seg_c & seg_f & seg_g)
#define NUM_5 (seg_a & seg_c & seg_d & seg_f
WWW.EFYMAG.COM
& seg_g)
#define NUM_6 (seg_a & seg_c & seg_d & seg_e
& seg_f & seg_g)
#define NUM_7 (seg_a & seg_b & seg_c)
#define NUM_8 (seg_a & seg_b & seg_c & seg_d
& seg_e & seg_f & seg_g)
#define NUM_9 (seg_a & seg_b & seg_c & seg_d
& seg_f & seg_g)
#define NUM_DOT (seg_dot)
#define NUM_SPACE 0Xff;
const unsigned char hex_table[] =
{
NUM_0,NUM_1,NUM_2,NUM_3,NUM_4,
NUM_5,NUM_6,NUM_7,
NUM_8,NUM_9,NUM_DOT
};
/* ADDRESS FOR SELECTING THE COMMON
PIN OF THE DISPLAY FOR EACH DIGIT */
#define UNITS 0xdf
#define TENS 0xef
#define HUNDS 0xf7
#define THS 0xfb
/* RELOAD VALUE FOR TIMER2 FOR INTERRUPT DURATION OF 2 MILLISECONDS */
#define TIMER2H_2MS 0xf2
#define TIMER2L_2MS 0xfb
/* RELOAD VALUE FOR TIMER0 FOR INTER-
JUNE 2007
CONSTRUCTION
void
Timer2_init(unsigned
char
Timer2h,unsigned char Timer2l);
void Display(unsigned char Digit);
void IntToSevSeg(unsigned int TimeTemp);
void KeyDebounce(unsigned char dly);
void Keyscan(void);
/* RENAMING PORT PINS FOR EASY USAGE
*/
sbit RELAY = P3^7;
sbit SEL = P1^4;
sbit UP = P1^5;
sbit DN = P1^6;
sbit STRT = P1^7;
/* MAIN FUNCTION */
void main(void)
{
RELAY = 0;
Timer2_init(TIMER2H_2MS,TIMER2L_2MS);
Timer0_init(TIMER0H_1MS,TIMER0L_1MS);
TR0=0;
EA=1;
while(1)
{
if(TR0==0)IntToSevSeg(SetSec);
else if( (TR0==1)&& (Timer0Counter==0) )
IntToSevSeg(OneSecCount);
Keyscan();
switch(key)
{
case 4:
if(SetSec>0)
{
if(TR0==0)
{
OneSecCount
= SetSec;
RELAY = 1;
TR0=1;
}
else
{
TR0=0;
}
}
key=0;
break;
case 3:
SetSec;
if(SetSec>9999)
SetSec=9999;
key=0;
break;
case 2:
SetSec++;
if(SetSec>9999)
SetSec=0;
key=0;
break;
case 1:
Mode++;
if(Mode>1)Mode=0;
Hunds=hex_table[Mode];
Tens = NUM_SPACE;
Units = NUM_SPACE;
key=0;
KeyDebounce(10);
break;
}
}
}
/* TIMER INITIALISATION FUNCTIONS */
void
Timer0_init(unsigned
char
Timer0h,unsigned char Timer0l)
{
TMOD &= 0xf0;
TMOD |= 0x01;
TH0 = Timer0h;
TL0 = Timer0l;
ET0=1;
TR0=1;
}
void
Timer2_init(unsigned
JUNE 2007
char
TimeTemp -=(TimeTemp/100)*100;
if((Ths==0xff)&&(TimeDig==0))
{
Hunds=NUM_SPACE;
}
else
{
Hunds = hex_table[TimeDig];
}
TimeDig = TimeTemp/10;
TimeTemp -=(TimeTemp/10)*10;
if((Hunds==0xff)&&(TimeDig==0))
{
Tens=NUM_SPACE;
}
else
{
Tens = hex_table[TimeDig];
}
TimeDig = TimeTemp%10;
Units = hex_table[TimeDig];
}
/* Interrupt Routines */
/* Drives LED Displays */
void isr_t2(void) interrupt TIMER2VECTOR
{
DisplayCounter++;//Increments every 50
ms.
if(DisplayCounter>3)DisplayCounter=0;
Display(DisplayCounter);
TF2 = 0;
}
/* Counts Seconds */
void isr_t0(void) interrupt TIMER0VECTOR
{
TH0 = TIMER0H_1MS;
TL0 = TIMER0L_1MS;
TF0=0;
Timer0Counter++;//Counts every 1msec.
LedCounter++;
if(LedCounter<200)
Units= Units & NUM_DOT;
else if( (LedCounter>=200) && (LedCounter
<999) )
Units = Units | 0x80;
else
LedCounter=0;
if(Mode==0)
{
if(Timer0Counter>SEC)
{
Timer0Counter=0;
OneSecCount;
if(OneSecCount==0)
{
//OneSecCount=0;
TR0=0;
RELAY = 0;
}
}
}
else if(Mode==1)
{
if(Timer0Counter>MIN)
{
Timer0Counter=0;
OneSecCount;
if(OneSecCount>MIN)
{
OneSecCount=0;
TR0=0;
RELAY = 0;
}
}
}
WWW.EFYMAG.COM