5atimers counters

60
TIMERS / COUNTERS - Two 16 bit timer/counters - Can be programmed independently as – timer or event counter. - Four-SFR’s connected with TIMER/COUNTER operation - TMOD – Timer Mode Register - TCON – Timer Control Register - TH0, TL0 – Timer/Counter - 0 - TH1, TL1 – Timer/Counter - 1 - Two pins of 8051 connected with Timer/counter. T0 – Timer 0 external input – P3.4 T1 – Timer 1 external input – P3.5 - INT0 and INT1 are also used for controlling the timer/counters.

Upload: sahilyadav

Post on 09-Nov-2015

230 views

Category:

Documents


1 download

DESCRIPTION

vdfc

TRANSCRIPT

TIMERS / COUNTERS

TIMERS / COUNTERS Two 16 bit timer/counters Can be programmed independently as timer or event counter. Four-SFRs connected with TIMER/COUNTER operation TMOD Timer Mode Register TCON Timer Control Register TH0, TL0 Timer/Counter - 0 TH1, TL1 Timer/Counter - 1 Two pins of 8051 connected with Timer/counter.T0 Timer 0 external input P3.4T1 Timer 1 external input P3.5- INT0 and INT1 are also used for controlling the timer/counters.Timer OperationTimer Register (TH0, TL0 or TH1, TL1) incremented every m/c cycle. Thus working at increment frequency of 1/12 of oscillator frequency ( for 12 oscillator machine cycle ). Any preset value i.e. initial count can be loaded to TH0, TL0 or TH1, TL1.For Example Clock frequency = 12 MHZClock period = 1/12 secMachine cycle time = 1 secThus timer register will be incremented every microsecond.- If timer is initialized to 0000HMax. count = FFFFHmax. time measured = 216 sec = 26 x 210 sec 26 millisecond 64 millisecond = 65.5 millisecond

Counts pulses occurring at T0 pin (Timer/Counter 0) and/or T1 pin (Timer/counter 1).May correspond to event likePassing of railway coach from a point axle counter Rotation of speedometer cable speedometer of vehicle No. of persons visiting exhibition.T0, T1 scanned every m/c cyclenth m/c cycle T1 or T0 = High(n+1)th m/c T1 or T0 = LowTimer 0 or timer 1 incremented in (n+1)th m/c cycleCount frequency = min 2 m/c cycle per countT0- P3.4, T1- P3.5Counter OperationIn 12 MHz 8051 m/c cycle = 1 sec8051 can count at the rate of 2 sec per count or higherAny event when takes less than 2 sec may go unnoticedC/T bit of TMOD selects Timer or counter operation for Timer 0 or Timer 1.Timer/Counter operations are controlled byGate bit of TMODTR0 bit of TCONWhen Gate = 0 thenTR0, TR1 act as Timer run control bits.2 m/c cycleThus make Gate = 0 in TMOD By making TR0 (TCON. 4) or TR1 (TCON. 6) = 1 . through instruction, Timer/Counter 0 or Timer/Counter 1 may be started.For starting and stopping the Timer/Counter from outside through hardware.Make Gate = 1, TR0 = 1 through software.By making INT0 or INT1 pin High will start Timer/Counter 0 or Timer/Counter 1Make INT0 or INT1 Low to stop the Timer/Counter. INT0 - P3.2INT1 - P3.3

As value in Timer register rolls from all ones (i.e. FFFFH) to all zeros (i.e. 0000H) interrupt flag (TF0 or TF1) will be set.TF0 (for Timer 0) and TF1 (for Timer 1) are bits of TCON SFR.IF Timer 0 or Timer 1 interrupt is enabled then program control will branch to interrupt servicing routine.Timer modes-4 modesMode 0-13 bit counterMode 1-16 bit counterMode 2-8 bit counter + auto reloadMode 3- Split operation Timer 0TimerRegisterTF0InterruptProgramTimer ISRRETIModes are set by M1 M0 bits of TMOD register. Mode 0 - 13 bit counter operation- TH0, TL0 (for Timer 0) or TH1, TL1 (for Timer 1) used as 13 bit counter.- All 8 bits of TH0 or TH1- 5 lower bits of TL0 or TL1are used, for counting.- When count rolls over from all 1s to all 0s, - interrupt flag TF0 or TF1 is set.OSC 12TH0(8 bits)TF0TL0(5 bits)C/T = 0C/T = 1T0 pinControlInterruptTR0

GateINT0 pinFigureTimer 0, mode 0 - 13-bit counterIn above figure when C/T = 0 - timer operation count incremented every m/c cycle.providedTR0 (TCON. 4) or TR1 (TCON. 6) = 1and Gate (TMOD. 3) or (TMOD. 7) = 0Other way is-- TR0 or TR1 =1- Gate = 1 and INT0 or INT1 = 1- Thus by sending Logic High signal on INT0 (or INT1) pins Timer 0 or Timer 1 can be started.

- This can be used for finding pulse width in the following way. C/T = 0 Timer operation TR0 or TR1 = 1Gate = 1Source of pulse connected to INT0 or INT1 pinWhen pulse goes high timer starts counting at the rate 1/12 clock frequencyWhich pulse goes low Timer stops.INT0 or INT1 = Low - causes interrupt.

ISR can read the timer value.ISR can store the timer value and process it as required by the application.

TimerStartsTimerStops+InterruptGeneratedMode 1 16 bit counterFigure Timer 0, mode 1 - 16-bit counter.OSC 12TH0(8 bits)TF0TL0(8 bits)C/T = 0C/T = 1T0 pinControlInterruptTR0

GateINT0 pin- Operation same as mode 0 except that all bits of TH0, TL0 or TH1, TL1 are used.When count rolls over from all 1s to all 0s TF0 or TF1 interrupt flag is set.- Causes interrupt if enabled.Mode 2 8 bit operation with auto reloadFigure Timer 0, mode 2 - autoreload.OSC 12TL0(8 bits)TF0C/T = 0C/T = 1T0 pinControlInterruptTR0

GateINT0 pinTH0(8 bits)Only TL0 or TL1 are used i.e. 8 bit counting. Initial preset value is loaded to TH0 or TH1 by software.The value is loaded to TL0 or TL1 by hardware automatically before starts of counting.When count rolls from all 1s (i.e. FFH) to all 0s (i.e. 00H)TF0 or TF1 flag is setPreset value in TH0 or TH1 is reloaded to TL0 or TL1Operation i.e. Counting starts automatically.

Mode 3 Split operation Timer 0Figure Timer 0, mode 3-split to two 8-bit counters.OSC 12TL0(8 bits)TF0C/T = 0C/T = 1T0 pinControlInterruptTR0

GateINT0 pin(1/12) fosc(1/12) foscTH0(8 bits)TF1ControlInterrupt(1/12) foscTR1When Timer 0 is put in mode 3 Acts as two 8 bit counters i.e. TL0 and TH0 become two separate counter.TL0 8 bit operation in mode 0 or mode 1 (Timer or Counter) controlled by C/T, TR0, Gate, INT0 Sets TF0 when count rolls to all 0s from all 1s.TH0 Timer function only. Controlled by TR1 i.e. starts when TR1 = 1.When count rolls to all 0s from all 1s TF1 flag is set.

Note TR1 and TF1 are used in Timer 0 (TH0) even though they are bits for Timer 1. When Timer 1 is put in mode 3 It just holds the preset count same as TR0 = 0 i.e. opening the switch.[Modes 0, 1 and 2 are mostly used] Timer Mode Control Register - TMOD76543210GateC/TM1M0GateC/TM1M0Timer 1 Timer 0Bit no.M1 and M0 specify the mode as follows:M1M0ModeDescription in brief00013-bit counter01116-bit counter1028-bit counter with autoreload113Split Timer 0 into two 8-bit counters or to stop Timer 1SymbolIf C/T = 1, the timers function as counters to count the negative transitions at T0 or T1 pins.If C/T = 0, the timers function as timers, that is, they basically count the number of machine cycles.Gate = 0 means that the timer is controlled by TR1 or TR0 only, irrespective of INT0 or INT1.Gate = 1 means that the timer control will depend on INT0 or INT1 and also on TR0 or TR1 bitsWhen data is written it gets latched.TMOD is used for setting mode bits M1, M0, Gate bit and C/T for Timer 0 and Timer 1.Bit 0 to 3 for Timer 0.Bit 4 to 7 for Timer 1.Timer Control Register - TCONBit 0 to 3 used for interrupt functionsBit 4 to 7 used for setting TR0, TR1 by softwareSetting TF0, TF1 by counter i.e. hardware When count rolls from all 1s to all 0s.

76543210TF1TR1TF0TR0IE1IT1IE0IT0Bit no.SymbolTF1:Timer 1 overflow flag. Set by hardware when the timer/counter overflows.Cleared by hardware when the processor vectors to the interrupt routine.TR1:Timer 1 run control bit. Set/cleared by software to turn the timer/counter on/off.TF0:Timer 0 overflow flag. Set by hardware when the timer/counter overflows.Cleared by hardware when the processor vectors to the interrupt routine.TR0:Timer 0 run control bit. Set/cleared by software to turn the timer/counter on/off.Example a. Configuring Timer/Counter using TMOD 76543210GateC/TM1M0GateC/TM1M0Timer 1 Timer 0C/T = 0M1 M0 = 00Gate = 0C/T = 1M1 M0 = 01Gate = 0TR1TR0TMOD = 0 0 0 0 0 1 0 1 = 0 5 H MOV TMOD, #05HTIMER 1 - TIMERMode = 00(13 bit operation)

TIMER 0 - CounterMode = 01(16 bit operation)

Work out the preset value = ABCDH Timer 0Load the preset value = 0000H - Timer 1MOVTL0, #CDHMOVTH0, #ABHMOVTL1, #00HMOVTH1, #00Hb. To load initial count as preset value

Timer 0Timer 1Make TR0 = 1, TR1 = 1TCON = 0 1 0 1 0 0 0 0 = 5 0 H MOV TCON, #50H or SETB TCON.4 SETB TCON.6c. Start Timer/Counter through TR0, TR1

TCON = 76543210TF1TR1TF0TR0IE1IT1IE0IT0Timer InterruptFollowing tasks need to be done.Preset value to be loaded to Timer RegisterTimer interrupt flag (TF0 or TF1) to be clearedd. When count value in Timer Register transits from all 1s to all 0sFor continuous operation of Timer/CounterTime clockPulse train generation etc.- Can be achieved in 2 ways:1. - Check Timer interrupt flag in loop. JNB TCON.5, $ or JNB TCON.7, $When interrupt flag is set then clear the flag. CLR TCON.5 or CLR TCON.7Load the preset count and restart SJMP to b

2. Write ISR for Timer 0 or Timer 1 and store at location 000BH (for Timer 0) or 001BH (for Timer 1)- Enable Timer 0 or Timer 1 interrupt by making bits ET0 (IE.1) or ET1(IE.3) = 1. SETB IE.1 or SETB IE.3- When TF0 or TF1 is set- Interrupt will occur and program will branch to ISR location (000BH for Timer 0) or (001BH for Timer 1).

ISRclear flag TF0 or TF1load preset valueRestart timer/counter RETIStep d will be different for different applications.Example -1- Generate a square wave of 50% duty cycle at pin p1.7. Use Timer 1 to generate time delay. Clock frequency = 12 MHz, 12 oscillator clock. Pulse width = 50 millisecond.- Let us work out the initial preset value.

1 m/c cycle = 1 microsecond50 millisecond = 50 x 103 microsecond = 50, 000 m/c cycleFFFF = 65535Difference = 65535 - 50000 = 15535 m/c cycle

- Since count will roll from FFFF to 0000 additional m/c cycle will be required to set TF0 or TF1 .50 ms50 ms Thus initial count must be 15536 i.e. = 3CB0H By putting initial preset count of 3CB0H (or 15536 decimal), the register will reach FFFF in 49999 m/c cycle and roll over to 0000 in 50,000th m/c cycle accounting for 50 millisecond a. Configure Timer 1 76543210 00010000 = 1 0 HGate = 0, C/T = 0, Mode = 01(16 bit operator)MOV TMOD , # 1 0 HMake P1.7 = Low initially CLR P1.7b. Load Preset Value KK : MOVTL1, #B0H MOVTH1, #3CH

c. Complement P1.7CPLP1.7d. Start Timer 1 (TR1 = 1) SETB TCON.6e. Check for TF1=1 in loop JNB TCON.7, $f. TF1=1, Make TF1=0 CLR TCON.7g. Stop Timer 1 Make TR1=0 CLR TCON.6h. SJMP KK To reload preset value Complement P1.7 Start Timer 1.Steps d to g can be written as subroutine.Modified ProgramMOVTMOD, #10HCLRP1.7 MOVTL1, #B0HMOVTH1, #3CHCPLP1.7ACALLTDELYSJMPKK

TDELY:SETBTCON.6JNBTCON.7, $CLR TCON.7CLRTCON.6RETExample -2Generate a square wave of ON time of 3 ms and OFF time of 2 ms on P1.0. Clock frequency = 16 MHz, 12 clock m/c cycle 16 MHz Frequency using Timer 1. 1 clock period = 1/16 sec1 m/c cycle = 12/16 sec = sec 1 ms = 1000 sec = 4000/3 m/c cycle 1333 m/c cycle2 ms 2666 m/c cycle. Accounting for additional m/c cycle 26653 ms 4000 m/c cycle. Accounting for additional m/c cycle 3999

Count can be very well represented in 16 bits. Thus for 2 ms delay 65535 2665 = 62870 =F596H for 3 ms delay 65535 3999 = 61536 = F060H

; Configure Timer 1MOVTMOD, #10H00010000= 10HTMOD =;Load preset value for 3 msKK :MOVTL1, #60HMOVTH1, #F0H;Make P1.0 = HighSETBP1.0;Start Timer 1SETBTCON.6;Check for TF1 in loopJNBTCON.7, $;Make P1.0 = LowCLRP1.0 ;TF1 = 1, Make TF1 = 0CLRTCON.7;Stop Timer 1, Take TR1 = 0CLRTCON.6;Repeat for 2 ms......SJMPKK.

MOVTL1, #96HMOVTH1, #F5HSETBTCON.6JNBTCON.7, $Example 8051 with clock frequency = 18 MHzGenerate a square wave of frequency 2 KHz on pin P1.0 using mode 2.Calculate the smallest frequency possible without using software counter. Clock frequency = 18 MHz Clock period = 1/18 sec. 1 m/c cycle = 12/18 sec = 2/3 sec. 2 KHz square wave clock period = 10-3 sec. = 0.5 millisecond

Up time = 0.25 msDn time = 0.25 msUp time = 0.25 ms = 0.25 x 103 secNo. of m/c cycles in up time = ( x 103)/(2/3)= x 103 x 3/2 = 3/8 x 103= 3000/8 = (30 x 25)/2= 15 x 25 = 375

Delay of 375 m/c cycle can be achieved in many ways.375/3 = 125 Generate delay of 125 m/c cycle 3 times375/5 = 75 Generate delay of 75 m/c cycle 5 times375/15 = 25 Generate delay of 25 m/c cycle 15 times: ::We can take any of the options. Let us take 1st one.To generate delay of 125 m/c cyclePreset = (FFH) 255 125 = 130Accounting for additional m/c cyclePreset = 131 = 83H; Configure TMOD.Let us use Timer 0

MOVTMOD, #02H00000010= 02HTMOD =;Set P1.0 = HighSETBP1.0;Load preset countMOVTH0, #83H;Declare software counter;Loop:MOV R3, 03H;Start TimerKK:SETBTCON.4;Check TF0 in loopJNBTCON.5, $;Stop timer by making TR0=0CLRTCON.4;Clear TF0 FlagCLRTCON.5;Decrement and Branch to start timer.DJNZR3, KK.;Delay of 375 m/c cycle completed.CPLP1.0SJMPLoop.

b. Frequency is smallest when clock period = maximumi.e. Up time and Down time = maximumi.e. Delay is maximum.Delay is maximum when preset value =0i.e. No. of m/c cycles in Up time = FF+1No. of m/c cycles in Dn time = FF+1Up time = 256 x 2/3 sec = 512/3 170 secClock period = 341 sec.Frequency = 1/341 MHz = 1000/341 KHz= 2.92 KHzExample Counter OperationDesign a counter to count pulses input at P3.4.Determine the no. of pulses received in 1 minute.8051 is 12 MHz, 12 Clock m/c cycle.-> P3.4 pin is T0 i.e. -> external input to Timer 000000101= 05HTMOD =C/T = 1 Counter operationGate = 0, M1 M0 = 01 - 16 bit operation;MOVTMOD, #05H;InitializeTimer value to 0000HREPT:MOVTH0, #00HMOVTL0, #00H;StartCounterby making TR0 = 1 SETBTCON.4ACALLDELIM;Read timer value and output on P2, P1MOV A, TL0MOVP1, AMOVA, TH0MOVP2, ASJMPREPT

DELIM :MOVR2,-MOVR3,-MOVR4,-DJNZR4,-DJNZR3,-DJNZR2,-Let us calculate the maximum no. of pulses that can be counted.In 16 bit operation i.e. Mode 01 (FFFF+1) = 65535 + 1 = 65536In 13 bit operation i.e. Mode 00 1FFF+1 = 8191 = 8192In 8 bit operation i.e. Mode 02 & Mode 03 FF+1 = 255+1 = 256

Let us assume that 1 pulse corresponds to one rotation of is wheel circumference of wheel = 1 meter Max. distance travelled in 1 minute can be measured as 8 bit operation 256 meter.13 bit operation 8192 meter16 bit operation 65536 meter.Considering that overflow takes place in 1 minute durationMax. distance travelled in 1 hour that can be measured with be 8 bit operation 256 x 60 = 14760 meter = 14.76 KMPH13 bit operation 8192 x 60 = 491520 = 491.52 KMPH16 bit operation 65536 x 60 = 3932160 meter. = 3932.16 KMPH

For measuring automobile speed 13 bit or 16 bit operation will be o.k.Delay operation can also be managed using hardware timer so that micro controller is free for carrying out other tasks.Let us assume that Timer 1 is used for incorporating 1 minute delay12 MHz clock1 m/c cycle = 1 sec1 second = 106 sec = 106 m/c cycle = 220 m/c cycle = 24 x 216 m/c cycle.Thus 0000 to FFFF+1, counter has to repeat 16 times for delay of 1 sec.For 1 Minute delay 1 minute = 60 x 16 x216 1 second delay must be repeated in loop by 60 timesNow, TMOD will become

C/T = 0 for Timer 1 Timer operationMOVTMOD, #05H will get modified to MOV TMOD, #15H

00010101= 15 HDELIM SubroutineDELIM:MOVR3, #3CH ; for 60 secondsLOOP1:MOVR4, #10H ; for 16 times repeat for 1 secondLOOP2:MOVTH1, #00HMOVTL1, #00H; StartTimer 1SETBTCON.6JNBTCON.7, $CLRTCON.6CLRTCON.7DJNZR4, LOOP2DJNZR3, LOOP1RET We could also use interrupt servicing routine of timer interrupt for this purpose.THE END60