4
 CONSTRUCTION JUNE 2007 WWW.EFYMAG.COM  JAYARAMAN KIRUTHI VASAN MICROCONTROLLER-BASED INDUSTRIAL TIMER S  U N I L K  U M AR I ndustrial timers can be con- structed using discrete compo- nents including up/down counters and timers. However, to in- corporate various facilities like setting the count, start, stop, reset and dis- play, these circuits would require too many ICs and discrete components. A microcontrol ler-based industrial timer can be programmed and used as a timer, counter and time totaliser. Here is a simple de- sign based on 40-pin Atmel AT89S52 microcontroller that performs count-down operation up to 9999 minutes/second with four 7-segment dis- plays showing the ac- tual time left. The re- lay energises as you press the start switch and remains on till the countdown reaches ‘0000.’ Four tactile, push-to-on switches are used to start/stop, select either minutes or seconds, and set the initial value for count- down operation (us- ing ‘up’ and ‘down’ keys). Circuit description Fig. 1 shows the cir- cuit of the microcontroller-based industrial timer. The microcontroller is Atmel AT89S52 (IC1), which is a 40-pin de- is configured for segments of the 7- segment display. Port 0 is an 8-bit open-drain bidirectional I/O port. Port 0 is pulled up with 10-kilo-ohm resis- tor network RNW1. Port pins P0.0 through P0.6 are connected to pins of segments ‘a’ through ‘g’ via resistors R2 through R8, respectively. Port P0.7 is connected to decimal via resistor R9. Resistors R2 through R9 are used as current limiter for various segments of displays, respectively. vice with 8 kB of program flash memory, 256 bytes of RAM, 32 I/O lines, Watchdog timer, two data point- ers, three 16-bit timer/counters, a six- vector two-level interrupt architecture, a full-duplex serial port, on-chip oscil- lator and clock circuitry. The power- down mode saves the RAM contents but freezes the oscillator, disabling all other chip functions until the next in- terrupt or hardware reset is activated. Port P0 of microcontroller AT89S52

Industrial Timer

Embed Size (px)

DESCRIPTION

From sourced

Citation preview

  • CONSTRUCTION

    J U N E 2 0 0 7 W W W . E F Y M A G . C O M

    JAYARAMAN KIRUTHI VASAN

    MICROCONTROLLER-BASEDINDUSTRIAL TIMER

    SUNIL KUMAR

    I ndustrial timers can be con-structed using discrete compo-nents including up/downcounters and timers. However, to in-corporate various facilities like settingthe count, start, stop, reset and dis-play, these circuits would require toomany ICs and discrete components.

    A microcontroller-based industrialtimer can be programmed and usedas a timer, counterand time totaliser.Here is a simple de-sign based on 40-pinAtmel AT89S52microcontroller thatperforms count-downoperation up to 9999minutes/second withfour 7-segment dis-plays showing the ac-tual time left. The re-lay energises as youpress the start switchand remains on till thecountdown reaches0000. Four tactile,push-to-on switchesare used to start/stop,select either minutesor seconds, and set theinitial value for count-down operation (us-ing up and downkeys).

    CircuitdescriptionFig. 1 shows the cir-cuit of themicrocontroller-basedindustrial timer. Themicrocontroller isAtmel AT89S52 (IC1),which is a 40-pin de-

    is configured for segments of the 7-segment display. Port 0 is an 8-bitopen-drain bidirectional I/O port. Port0 is pulled up with 10-kilo-ohm resis-tor network RNW1. Port pins P0.0through P0.6 are connected to pins ofsegments a through g via resistorsR2 through R8, respectively. Port P0.7is connected to decimal via resistor R9.Resistors R2 through R9 are used ascurrent limiter for various segments ofdisplays, respectively.

    vice with 8 kB of program flashmemory, 256 bytes of RAM, 32 I/Olines, Watchdog timer, two data point-ers, three 16-bit timer/counters, a six-vector two-level interrupt architecture,a full-duplex serial port, on-chip oscil-lator and clock circuitry. The power-down mode saves the RAM contentsbut freezes the oscillator, disabling allother chip functions until the next in-terrupt or hardware reset is activated.

    Port P0 of microcontroller AT89S52

  • CONSTRUCTION

    J U N E 2 0 0 7 W W W . E F Y M A G . C O M

    Port 2 is used to control DIS1through DIS4. Port 2 is an 8-bit bidi-rectional I/O port with internal pull-ups. When port-2 pin is low, the tran-sistor conducts and provides supplyto the common pin of 7-segment dis-play. Port pins P2.5 through P2.2 con-trol DIS1 through DIS4 with the helpof transistors T1 through T4, respec-tively.

    The microcontroller drives the 7-segment displays in multiplex mode.This helps in reducing current con-sumption while maintaining thebrightness of the display. For drivingthe displays, timer 2 inside themicrocontroller is used. It enables dis-play of each digit every two millisec-onds.

    For driving the dis-plays, the microcontrolleruses port-0 to send thesegment outputs. It se-lects the correspondingunits, tens, hundredsand thousands displaysthrough P2.5, P2.4, P2.3and P2.2, respectively.

    Four pins of port 1 areused for various switcheslike select, up, down andstart/stop. Port 1 is an 8-bit bidirectional I/O portwith internal pull-ups.Switches S1 through S4are connected to pins 5through 8 of themicrocontroller and usedfor select, up, down andstart/stop functions, re-spectively.

    Pin P3.7 controls re-lay RL1. When pin P3.7goes high, transistor T5 isdriven into saturation

    and relay RL1energises. Diode D1serves as a free-wheel-ing diode. Any appli-ance can be connectedwith contacts of relayRL1.

    Power-on-reset isachieved by connect-ing resistor R1 and ca-pacitor C1 to pin 9 ofthe microcontroller.

    Other ends of the capacitor and resis-tor are connected to Vcc and ground,respectively. Switch S5 is used formanual reset. The microcontroller isoperated with the clock derived froma 20MHz crystal oscillator.

    Power supply. Fig. 2 shows the cir-cuit of the power supply. The ACmains is stepped down by transformerX1 to deliver a secondary output of7.5V at 350 mA. The transformer out-put is rectified by a full-wave bridgerectifier BR1, filtered by capacitor C5and regulated by IC2. Capacitor C6 by-passes any ripple present in the regu-lated output. Unregulated power sup-ply is used for relay RL1.

    An actual-size, single-side PCB lay-out for the microcontroller-based in-

    dustrial timer (Fig. 1) including powersupply (Fig. 2) is shown in Fig. 3 andits components layout in Fig.4.

    OperationSwitch on the circuit using using ON/

    OFF switch S6. Themicrocontroller is resetby power-on-reset andthen timer is in secondsmode. The select key se-lects the mode betweenseconds and minutes.This is displayed as 0for seconds and 1 forminutes on thehundreds digit display(DIS3), respectively.

    Up key incrementsthe time setting in sec-onds and minutes.

    Down key decre-ments the time setting inseconds and minutes.

    After setting the de-sired time with the helpof up and down keys,press start key. Thisenergises the relay. Thetimer counts down forthe set time and once thedisplay becomes zero,

    PARTS LISTSemiconductor:IC1 - AT89S52 micrcontrollerIC2 - 7805 5V regulatorT1-T4 - BC557 pnp transistorT5 - BC547 npn transistorBR1 - 1A bridge rectifierD1 - 1N4007 rectifier diodeDIS1-DIS4 - LTS542 common-anode

    display

    Resistors (all -watt, 5% carbon):R1, R14-R18 - 8.2-kilo-ohmR2-R9 - 270-ohmR10-R13 - 470-ohmRNW1 - 10-kilo-ohm resistor

    network

    Capacitors:C1 - 10F,16V electrolyticC2, C3 - 33pF ceramic diskC4, C6 - 0.1F ceramic diskC5 - 1000F, 25V electrolytic

    Miscellaneous:X1 - 230V AC primary to 6V,

    350mA secondarytransformer

    XTAL - 20MHz crystalRL1 - 6V, 1C/O relayS1-S5 - Push-to-on switchS6 - On/off switch

  • CONSTRUCTION

    J U N E 2 0 0 7 W W W . E F Y M A G . C O M

    the relay de-energises.The timer will stop be-fore preset time by press-ing start key again.

    SoftwareThe source program iswritten in C languageand compiled with KeilMicrovision 3 IDE. It iswell commented andeasy to understand.D o w n l o a dC51V808A.EXE fromwww.keil.com/demo/eval/c51.htm. This file isa freely available andself-extracting setup pro-gram for KeilMicrovision 3 IDE.

    Normally, when thereis no interrupt, themicrocontroller executeswhile loop in the mainfunction. Here it scansthe keys and acts according to the keypressed.

    Two interrupts are enabled in thesoftware, namely, timer 0 and timer 2.Timer 0 counts milliseconds, which arethen accumulated to seconds or min-utes according to the user selection.Timer 2 drives the displays in multi-plex mode.

    For time counting, timer 0 is

    initialised by the voidTimer0_init(unsigned charTimer0h,unsigned char Timer0l) func-tion. Timer 0 interrupts themicrocontroller every millisecond.

    When interrupted by timer 0, themicrocontroller executes the voidisr_t0(void) function wherein it incre-ments two counter variables, namely,Timer0Counter and LedCounter.

    Timer0Counter is re-sponsible for countingthe number of millisec-onds elapsed and incre-ments the minutes/sec-onds counter accordingto the mode selected(seconds or minutescount). Once the setvalue is reached, thetimer-0 interrupt is dis-abled and time countingstops.

    The LED countermakes the dot LED of theunits digit flash everysecond once.

    Display-driving pro-cess is taken care of bythe built-in timer 2. Timer2 is initialised by the voidTimer2_init(unsignedchar Timer2h,unsignedchar Timer2l) function.

    Timer 2 gives an in-terrupt to the microcontroller toswitch on the common pin of each 7-segment display for every two milli-seconds. When an interrupt occurs, thevoid isr_t2(void) function is executedand the microcontroller returns towhile loop in the main function.

    EFY note. The software and otherrelevant files of this article have beenincluded in this months EFY-CD.

    SOURCE PROGRAM/*** Include Files ***/#include

    /*** RENAMING OF PORTS ****/#define SegPort P0#define DigPort P2

    /* CODE FOR LIGHTING EACH SEGMENT OFTHE 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 NUM-BER 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

    & 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 COMMONPIN OF THE DISPLAY FOR EACH DIGIT */#define UNITS 0xdf#define TENS 0xef#define HUNDS 0xf7#define THS 0xfb/* RELOAD VALUE FOR TIMER2 FOR INTER-RUPT DURATION OF 2 MILLISECONDS */#define TIMER2H_2MS 0xf2#define TIMER2L_2MS 0xfb/* RELOAD VALUE FOR TIMER0 FOR INTER-

    RUPT DURATION OF 1 MILLISECOND */#define TIMER0H_1MS 0xf9#define TIMER0L_1MS 0x7e/* VECTOR VALUE FOR TIMER INTERRUPT*/#define TIMER0VECTOR 1#define TIMER2VECTOR 5/* MINUTES AND SECONDS CONSTANTS */#define SEC 999#define MIN (60 * SEC)/* VARIABLES DEFINITION */unsigned char Units;unsigned char Tens;unsigned char Hunds;unsigned char Ths;unsigned int Timer0Counter=0;unsigned char DisplayCounter=0;unsigned int LedCounter=0;unsigned char TimeDig;unsigned int OneSecCount=0;unsigned int SetSec=0;unsigned char OneMinCount;unsigned char key;unsigned char KeyCount;unsigned char Mode;/* FUNCTION PROTOTYPES */void Timer0_init(unsigned charTimer0h,unsigned char Timer0l);

  • CONSTRUCTION

    J U N E 2 0 0 7 W W W . E F Y M A G . C O M

    void Timer2_init(unsigned charTimer2h,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 charTimer0h,unsigned char Timer0l){TMOD &= 0xf0;TMOD |= 0x01;TH0 = Timer0h;TL0 = Timer0l;ET0=1;TR0=1;}

    void Timer2_init(unsigned char

    Timer2h,unsigned char Timer2l){T2CON = 0x04;T2MOD = 0x00;TH2 = Timer2h;RCAP2H=Timer2h;TL2 = Timer2l;RCAP2L=Timer2l;ET2=1;TR2=1;}

    /* KEYSCAN FUNCTIONS */void Keyscan(void){while( (SEL==0) || (STRT==0) )

    {KeyDebounce(1);if(SEL==0)key=1;else if(STRT==0)key=4;

    }if ((UP==0) || (DN==0) )

    {KeyCount;if(KeyCount0)

    {dly;for(z=0;z3)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) && (LedCounterSEC)

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

    }}

    }

    /****************** END *****************/ z