timer

46
Timers and Counters Many microcontroller applications require the counting of external events, such as frequency of a pulse train, or the generation of precise internal time delays between actions. Both of these tasks can be accomplished using software techniques. The 8051 has two 16-bit registers that can be used as either timers or counters. Each counter may be programmed to count internal clock pulses, act as a timer, or programmed to count 1

Upload: sisay-deressa

Post on 25-Sep-2015

215 views

Category:

Documents


0 download

DESCRIPTION

interface

TRANSCRIPT

Slide 1

Timers and CountersMany microcontroller applications require the counting of external events, such as frequency of a pulse train, or the generation of precise internal time delays between actions.Both of these tasks can be accomplished using software techniques.The 8051 has two 16-bit registers that can be used as either timers or counters. Each counter may be programmed to count internal clock pulses, act as a timer, or programmed to count external events as a counter.

11 Timers /Counters The 8051 has 2 timers/counters: Timer/Counter 0 Timer/Counter 1 They can be used asThe Timer :used as a time delay generator.The clock source is the internal crystal frequency of the 8051.An event counter.External input from input pin to count the number of events on registers.These clock pulses represent the number of people passing through an entrance, or the number of wheel rotations, or any other event that can be converted to pulses.2Timers and CountersBoth Timer 0 and Timer 1 are 16 bits wideSince 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of low byte and high byte.Accessed as low byte and high byteThe low byte register is called TL0/TL1 AndThe high byte register is called TH0/TH1Accessed like any other register

33Contd. 4

5Contd. 8051 timers use 1/12 of XTAL frequency as the input of timers.Because the input of timer is a regular, fixed-periodic square wave, we can count the number of pulses and calculate the time delay.toLCDP18051TL0TH0SetTimer 0XTALoscillator 12TimerTimer SFRsAs mentioned before, the 8051 has twotimers which each function essentially the sameway. One timer is TIMER0 and the other isTIMER1. The two timers share two SFRs (TMODand TCON) which control the timers, and eachtimer also has two SFRs dedicated solely to itself(TH0/TL0 and TH1/TL1).Weve given SFRs names to make it easier to refer to them, but in reality an SFR has a numeric address. It is often useful to know the numeric address that corresponds to an SFR name.66Contd. 7The SFRs relating to timers are:

Timer SFRsWhen you enter the name of an SFR intoan assembler, it internally converts it to a number.For example, the command: MOV TH0,#25hmoves the value 25h into the TH0 SFR.However, since TH0 is the same as SFR address8Ch this command is equivalent to: MOV 8Ch,#25h88The TMOD(Timer Mode)SFRThe TMOD SFR is used to control the mode of operation of both timers. Each bit of the SFR gives the microcontroller specific information concerning how to run a timer.TMOD is a 8-bit registerThe lower 4 bits are for Timer 0The upper 4 bits are for Timer 1In each case,The lower 2 bits are used to set the timer modeThe upper 2 bits to specify the operation99Contd. 10

GateEvery timer has a mean of starting and stopping.GATE=0Internal controlThe start and stop of the timer are controlled by way of software.Set/clear the TR for start/stop timer. SETB TR0 CLR TR0GATE=1External controlThe hardware way of starting and stopping the timer by software and an external source.Timer/counter is enabled only while the INT pin is high and the TR control pin is set (TR).

Contd. 12

13-bit Time Mode (mode 0)Timer mode "0" is a 13-bit timer. This is a relic that was kept around in the 8051 to maintain compatibility with its predecessor.When the timer is in 13-bit mode, TLx willcount from 0 to 31. When TLx is incremented from 31, it will "reset" to 0 and increment THx. Thus, effectively, only 13 bits of the two timer bytes are being used: bits 0-4 of TLx and bits 0-7 of THx.This timer can only contain 8192 values.

131316-bit Time Mode (mode 1)Timer mode "1" is a 16-bit timer.This is a very commonly used mode. It functions just like 13-bit mode except that all 16 bits are used. TLx is incremented from 0 to 255. When TLx is incremented from 255, it resets to 0 and causes THx to be incremented by 1. Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. 1414Contd. 15

8-bit Time Mode (mode 2)Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask? Simple. When atimer is in mode 2, THx holds the "reload value"and TLx is the timer itself.Thus, TLx starts counting up. When TLx reaches 255 and is subsequently incremented, instead of resetting to 0 (as in the case of modes 0 and 1), it will be reset to the value stored in THx.16

16Contd. 8-bit Time Mode (mode 2)Whats the benefit of auto-reload mode?Perhaps you want the timer to always have avalue from 200 to 255. If you use mode 0 or 1, youd have to check in code to see if the timer had overflowed and, if so, reset the timer to 200. This takes precious instructions of execution time to check the value and/or to reload it. When you use mode 2 the microcontroller takes care of this for you.1717Contd. 18

Contd. 19

Contd. 20

Split Timer Mode (mode 3)Timer mode "3" is a split-timer mode.When Timer 0 is placed in mode 3, it essentiallybecomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0.2121Contd. Split Timer Mode (mode 3)While Timer 0 is in split mode, the realTimer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2 normally--however, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0. The real timer 1, in this case, will be incremented every machine cycle no matter what.2222Example: 1. Indicate which mode and which timer are selected for the following. (a) MOV TMOD, #01HSolution: We convert the value from hex to binary. We have:(a) TMOD = 00000001, mode 1 of timer 0 is selected.2323Exercise1. Indicate which mode and which timer are selected for each of the following. (a) MOV TMOD, #20H (b) MOV TMOD, #12H2424The TCON(timer control) SFRFinally, theres one more SFR that controls the two timers and provides valuable information about them.TCON (timer control) register is an 8-bit register.which is bit addressable and in which Upper nibble is for timer/counter, lower nibble is for interrupts

25

25Contd. 26

Tcon contdTR (Timer run control bit)TR0 for Timer/counter 0; TR1 for Timer/counter 1.TR is set by programmer to turn timer/counter on/off.TR=0 : off (stop)TR=1 : on (start) TF (timer flag, control flag) TF0 for timer/counter 0; TF1 for timer/counter 1.TF is like a carry. Originally, TF=0. When TH-TL roll over to 0000 from FFFFH, the TF is set to 1.TF=0 : not reach TF=1: reach

Contd. As you may notice, weve only defined 4 ofthe 8 bits. Thats because the other 4 bits of theSFR dont have anything to do with timers--theyhave to do with Interrupts .A new piece of information in this chart isthe column "bit address." This is because this SFR is "bit-addressable."2828Contd. What does this mean? It means if you want to set the bit TF1--which is the highest bit of TCON--you could execute the command: MOV TCON, #80hor, since the SFR is bit-addressable, you could just execute the command: SETB TF1This has the benefit of setting the high bit of TCON without changing the value of any of the other bits of the SFR. Usually when you start or stop a timer you dont want to modify the other values in TCON, so you take advantage of the fact that the SFR is bit-addressable.2929Contd. 30

Initializing a TimerAs youll recall, we first must decide what mode we want the timer to be in. In this case we want a 16-bit timer that runs continuously; that is to say, it is not dependent on any external pins.We must first initialize the TMOD SFR. Since we are working with timer 0 we will be using the lowest 4 bits of TMOD. The first two bits, GATE0 and C/T0 are both 0 since we want the timer to be independent of the external pins.3131Initializing a Timer16-bit mode is timer mode 1 so we must clear T0M1 and set T0M0. Effectively, the only bit we want to turn on is bit 0 of TMOD. Thus to initialize the timer we execute the instruction: MOV TMOD,#01hTimer 0 is now in 16-bit timer mode.3232 8051- SERIAL COMMUNICATIONComputers transfer data in two ways:ParallelOften 8 or more lines (wire conductors) areused to transfer data to a device that is only afew feet awaySerial To transfer to a device located many metersaway, the serial method is usedThe data is sent one bit at a time.3333Contd. 34

Types of Serial communications.

8051- SERIAL COMMUNICATIONOne of the 8051s many powerful featuresis its integrated UART, otherwise known as aserial port. The fact that the 8051 has an integrated serial port means that you may very easily read and write values to the serial port. If it were not for the integrated serial port, writing a byte to a serial line would be a rather tedious process requiring turning on and off one of the I/O lines in rapid succession to properly "clock out each individual bit, including start bits, stop bits, and parity bits.3636RxD and TxD pins in the 8051The 8051 has two pins for transferring and receiving data by serial communication. These two pins are part of the Port3(P3.0 &P3.1)These pins are TTL compatible and hence they require a line driver to make them RS232 compatible Max232 chip is one such line driver in use. Serial communication is controlled by an 8-bit register called SCON register,it is a bit addressable register.8051 InterruptsAn interrupt is an external or internal event that disturbs the microcontroller to inform it that a device needs its service.

A Microcontroller can serve various devices. There are two ways to do that:interrupts &Polling-is the process where the computer or controlling device waits for an external device to check for its readiness.The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.

Steps in executing an interrupt

Upon receiving the interrupt signal the Microcontroller , finish current instruction and saves the PC on stack.Jumps to a fixed location in memory depending on type of interruptStarts to execute the interrupt service routine until RETI (return from interrupt)Upon executing the RETI the microcontroller returns to the place where it was interrupted. Get pop PC from stack Interrupt SourcesOriginal 8051 has 6 sources of interrupts Reset Timer 0 overflow Timer 1 overflow External Interrupt 0 External Interrupt 1 Serial Port events (buffer full, buffer empty, etc)

Interrupt Enable Register Upon reset all Interrupts are disabled & do not respond to the MicrocontrollerThese interrupts must be enabled by software in order for the Microcontroller to respond to them.This is done by an 8-bit register called Interrupt Enable Register (IE).EA : Global enable/disable.--- : Undefined.ET2 : Enable Timer 2 interrupt.ES : Enable Serial port interrupt.ET1 : Enable Timer 1 interrupt.EX1 :Enable External 1 interrupt.ET0 : Enable Timer 0 interrupt. EX0 : Enable External 0 interrupt.

Enabling and disabling an interruptBy bit operation

Recommended in the middle of programSETB EA ;Enable All SETB ET0 ;Enable Timer0 ovrf SETB ET1 ;Enable Timer1 ovrfSETB EX0 ;Enable INT0SETB EX1 ;Enable INT1SETB ES ;Enable Serial port By Mov instruction

Recommended in the first of programMOV IE, #10010110BSETB IE.7SETB IE.1SETB IE.3SETB IE.0SETB IE.2 SETB IE.4What if two interrupt sources interrupt at the same time?The interrupt with the highest PRIORITY gets serviced first.All interrupts have a power on default priority order. External interrupt 0 (INT0)Timer interrupt0 (TF0)External interrupt 1 (INT1)Timer interrupt1 (TF1)Serial communication (RI+TI)Priority can also be set to high or low by IP reg. Interrupt PrioritiesInterrupt Priorities (IP) RegisterIP.7: reservedIP.6: reservedIP.5: Timer 2 interrupt priority bit (8052 only)IP.4: Serial port interrupt priority bitIP.3: Timer 1 interrupt priority bitIP.2: External interrupt 1 priority bitIP.1: Timer 0 interrupt priority bitIP.0: External interrupt 0 priority bit

---PX0PT0PX1PT1PSPT2---Interrupt Priorities ExampleMOV IP , #00000100B or SETB IP.2 gives priority orderInt1Int0Timer0Timer1SerialMOV IP , #00001100B gives priority orderInt1Timer1Int0Timer0Serial

---PX0PT0PX1PT1PSPT2---