1 Chapter 4 Timer Operation (I. Scott MacKenzie).

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

Dr A Sahu Dept of Comp Sc & Engg. IIT Guwahati I/O + Timer 8155 I/O + Timer 8255 I/O 8255 I/O 8253/54 Timer 8253/54 Timer 2 Port (A,B), No Bidirectional.
4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
8051 Core Specification.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
5-1 Timer/Counters In many microprocessor systems we need to:  count logic pulses  measure the frequency of signals  measure the period of pulses 
Chapter 6 Interrupts (I. Scott Mackenzie).
Embedded Systems UNIT 3. Pin Details of 8051 Pins 1-8: Port 1 Each of these pins can be configured as an input or an output. Pin 9: The positive voltage.
1.  8051 Timers “count up,” incrementing the Timer’s respective “count register” each time there is a triggering clock pulse. 2  When the “count register”
The 8051 Microcontroller and Embedded Systems
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
The 8051 Microcontroller architecture
NATIONAL TAIWAN OCEAN UNIVERSITY 國立台灣海洋大學 2002/4/8 Microcomputers and Microprocessors Chapter 9 COUNTER/TIMER PROGRAMMING IN THE 8051.
CHAPTER TIMER PROGRAMMING Timers The 8051 has two timers/counters, they can be used as ◦ Timers to generate a time delay ◦ Event counters.
8051 timer/counter.
8051 timer/counter.
8254 Programmable Interval Timer
ENEE 440 Chapter Timer 8254 Register Select The 8254 timer is actually 3 timers in one. It is an upgraded version of the 8253 timer which was.
Chapter 9 Counter/Timer Programming in the 8051
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
8253 TIMER. Engr 4862 Microprocessors 8253 / 8254 Timer A.k.a. PIT (programmable Interval Timer), used to bring down the frequency to the desired level.
PIT: Programmable Interval Timer
8254 Counter/Timer Counter Each of the three counter has 3 pins associated CLK: input clock frequency- 8 MHz OUT GATE: Enable (high) or disable.
Chapter 4 TIMER OPERATION
CoE3DJ4 Digital Systems Design Chapter 4: Timer operation.
Lecture Set 9 MCS-51 Serial Port.
Timers /Counters Programming  The 8051 has 2 timers/counters: ○ timer/counter 0 ○ timer/counter 1 They can be used as 1. The timer is used as a time.
1. Registers Used in Timer/Counter  TH0, TL0, TH1, TL1  TMOD (Timer mode register)  TCON (Timer control register) 2.
Advanced Microprocessor1 I/O Interface Programmable Interval Timer: 8254 Three independent 16-bit programmable counters (timers). Each capable in counting.
8051 Timer/Counter Lec note 7.
8051 Micro controller. Architecture of 8051 Features of 8051.
MCS51 - lecture 3. Lecture 3 2/30 Timers/counters MCS51.
8254 Timer.
Intel 8051 Another family of microcontroller is the Intel 8051 family. The basic 8051 microcontroller has four parallel input/output ports, port 0, 1,
Jump, Loop, and Call Instructions
The Silicon Laboratories C8051F020
8051 Aula 02 Prof Afonso Ferreira Miguel. Microcontrolador 8051 (MCS-51)  Timers do MCS51 Two 16-bit Counter/Timers: Two 16-bit Counter/Timers: Up counters,
Lecture 41 CSE 341 – Microprocessors Lecture 4 Md. Omar Faruqe UB 1228
kashanu.ac.ir Microprocessors Interrupts Lec note 8.
Timer Programming in Assembly and C Prepared By:
HJD Institute of Technical Education & Research- Kera(Kutch) The 8051 Microcontroller architecture PREPARED BY: RAYMA SOHIL( )
80C51 Block Diagram 1. 80C51 Memory Memory The data width is 8 bits Registers are 8 bits Addresses are 8 bits – i.e. addresses for only 256.
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
Assembly Language * * * * * * * 8051 Timer
Source: Serial Port Source:
8051 Timers Timers are the peripherals of the 8051 Microcontroller.
Timer Source: under
Programmable Interval Timer
Programmable Interval Timer
Interrupt Source: under
Introduction to Micro Controllers & Embedded System Design Interrupt
UNIT 5 TIMRERS/COUNTERS
Introduction to Micro Controllers & Embedded System Design I/O Processing and Serial Port Operation Department of Electrical & Computer Engineering Missouri.
Timer Operations and Programming
EMT 348: Microcontroller Timer/counter
8051 Timers Prof. Rajiv R Bhandari.
Introduction to Micro Controllers & Embedded System Design Timer Operation Department of Electrical & Computer Engineering Missouri University of Science.
8254 Timer and Counter (8254 IC).
Timer.
8051 Timers / Counters It has two timers Timer 0 and Timer 1.
Programmable Interval timer 8253 / 8254
Timer Source: under
Interrupt Source: under
Programmable Interval timer 8253 / 8254
Interrupt Source: under
Source: Serial Port Source:
Timer Source: under
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
Programmable Interval Timer
Presentation transcript:

1 Chapter 4 Timer Operation (I. Scott MacKenzie)

2 Counter / Timers (T0 & T1) Timer is a series of divide-by-two flip-flops that receive an input signal as a clocking source. Clock is applied to the first flip-flop, which gives output divided by 2. That output of first flip-flop clocks the second flip- flop,which also divides it by 2 and so on. The output of the last stage clocks a timer overflow flip-flop, or flag, which is tested by the software. It is like a counter. A 16-bit timer would count from 0000H to FFFFH. The overflow flag is set on the FFFFH-to-0000H count. There are two timers in 8051 i.e., T0 and T1. Four modes of timer operations.

3 ÷2 16 Divide-by-Two Flip-Flops CLK FF1 Q1 FF2 Q2 FF3 Q3 FF16 Q16 FF4 Q4 Overflow Flag (TFx) CLK Q1 Q2 Q3 ÷2 1 ÷2 2 ÷2 3 Q16

4 TMOD : Counter/Timer MODe Register GATEC/TM1M0GATEC/TM1M0 Timer 1Timer 0 - C/T : Set for counter operation, reset for timer operation. - M1, M0 : To Select the Mode 00 : Mode bit timer mode (Emulates 8048). 01 : Mode bit timer mode. 10 : Mode bit auto-reload mode. 11 : Mode 3 - Split timer mode (Timer 0 = two 8-bit timers). TMOD is not bit addressable. It is loaded, generally, by the software at the beginning of a program to initialize the timer mode. - GATE : Permits INTx pin to enable/disable the counter.

5 TCON : Counter/Timer CONtrol Register - TF1, TF0 : Overflow flags for Timer 1 and Timer 0. - TR1, TR0 : Run control bits for Timer 1 and Timer 0. Set to run, reset to hold. - IE1, IE0 : Edge flag for external interrupts 1 and 0. * Set by interrupt edge, cleared when interrupt is processed. - IT1, IT0 : Type bit for external interrupts. * Set for falling edge interrupts, reset for 0 level interrupts. * = not related to counter/timer operation but used to detect and initiate external interrupts. TF1TR1TF0TR0IE1IT1IE0IT0 TimersInterrupts

6 Timer Modes Timer Mode 0 (13-bit Timer): –Timer high-byte (THx) is cascaded with the 5 least- significant bits of the timer low-byte (TLx) to form a 13- bit timer, where x = 0 or 1. –Upper 3-bits of TLx are not used. –Overflow occurs on the 1FFFH-to-0000H and sets the timer overflow flag. –MSB is THx bit 7, and LSB is TLx bit 0. –MOV TMOD, #00H; setting both timers to mode 0 Timer clock Overflow flag TLx 5-bits THx 8-bits TFxInterrupt 7040

7 Timer Mode 1 (16-bit Timer): –Same as mode 0 except that it is 16-bit. Timer high-byte (THx) is cascaded the timer low-byte (TLx) to form a 16- bit timer, where x = 0 or 1. –Clock is applied to the combined high and low-byte timer registers. –Overflow occurs on the FFFFH-to-0000H and sets the timer overflow flag. –MSB is THx bit 7, and LSB is TLx bit 0. –LSB toggles at clock frequency/2 1 and MSB at clock frequency/2 16 Timer Modes (cont'd) Timer clock Overflow flag THx 8-bits TFxInterrupt TLx 8-bits 7070

8 Timer Modes (cont'd) Timer clock Overflow flag THx 8-bits TFxInterrupt TLx 8-bits Trigger Reload Timer Mode 2 (Auto-Reload): –Timer low -byte (TLx) operates as an 8-bit timer while the timer high -byte (THx) holds a reload value. –When the count overflows from FFH-to-00H, not only the timer flag set, but also the value in THx is loaded into TLx, and counting continues from this value up to next FFH-to-00H, so on.

9 Timer Modes (cont'd) Timer Mode 3: –Timer 0 Splits into two 8-bit counter/timers. TL0 and TH0 act as two separate timers with overflows setting the TF0 and TF1 respectively. –Timer 1 (when timer 0 is in mode 3 ):  Counter stopped if in mode 3  Can be used in mode 0, 1, or 2  Has gate (INT1) and external input (T1), but no flag or interrupt.  May be used as a baud rate generator. Timer clock TH1 8-bits TL1 8-bits Overflow flag Timer clock TF0Interrupt TL0 8-bits Overflow flag ÷12 F osc. TF1Interrupt TH0 8-bits

10 Clocking Sources 1.Interval Timing 2.Event Counting

11 Clocking Sources (contd.) 1.Interval Timing: If C/T = 0 (in TMOD), timer operation is selected and timer is clocked from on-chip oscillator. A divide-by-12 clock frequency is applied. Timer registers (TLx/THx) increment at a rate of 1/12 th the frequency of on-chip oscillator. 12 MHz crystal would yield a clock rate of 1 MHz. Timer overflows occur after a fixed number of clocks, depending on the initial value loaded into the timer registers.

12 Clocking Sources (contd.) 2.Event Counting: If C/T = 1 (in TMOD), counter operation is selected and timer is clocked from external source. Usually, external source supplies the timer with a pulse upon the occurrence of an event. Timer counts those events. External clock source comes through P3.4 (for Timer 0) and P3.5 (for Timer 1). Timer registers are incremented in response to a 1-to- 0 transition at the external input. Number of external events is determined in software by reading the timer registers TLx/THx.

13 Start, Stop, and Control of Timers TRx bit in bit addressable register TCON is responsible for starting and stopping the counters –TRx = 0 stops/disables the timers (e.g., CLR TR1) –TRx = 1 starts/enables the timers (e.g., SETB TR0) System reset clears TRx, so timers are disabled by default.

14 Timer 1 Operating in 16-bit (Mode 1) Osc.÷12Osc. TL1 8-bits TF1 TH1 8-bits Interrupt Control T1 (P3.5) TR1 Gate INT1 (P3.3) C/T MHz 0=Up, 1=Down Gate Disable/Enable INTx 0=Disable, 1=Enable 0=Open 1=Close

15 Initializing and Accessing Timer Registers Timers are usually initialized once at the beginning of the program to set the correct operating mode. Then, within the body of a program, the timers are started, stopped, flag bits are tested and cleared, timer registers read or updated, and so on, as required in the application. First register to be initialized is TMOD to set the mode of operation e.g., MOV TMOD, # B ; sets Timer 1 in mode 1, leave C/T = 0 and GATE = 0 for internal clocking, and clears the Timer 0 bits.

16 Initializing and Accessing Timer Registers (Contd.) Secondly, registers to be initialized are TLx/THx. E.g., For 100μs interval, the following instruction will do the job MOV TL1, #9CH ; (-100) 10 = FF9CH MOV TH1, #FFH ; load Timer 1 registers by FF9CH

17 Initializing and Accessing Timer Registers (Contd.) The timer is then started by setting the run control bit i.e., SETBTR1 Overflow flag is automatically set 100μs later. Following instruction will check that WAIT: JNB TF1, WAIT ; wait until overflow flag is set. When the timer overflows, it is necessary to stop the timer and clear the overflow flag in software by the following instructions: CLR TR1; stop Timer 1 CLR TF1; clear overflow flag of Timer 1

18 Short and Long Intervals Shortest possible interval is one machine cycle i.e., 1μs (using 12 MHz crystal frequency). An 8-bit counter can give maximum delay of 256μs because 2 8 =256. A 13-bit counter can give maximum delay of 8192μs because 2 13 =8192. A 16-bit counter can give maximum delay of 65536μs because 2 16 = μs = sec = sec (approx.)

19 Short and Long Intervals (Contd.) For more than sec delay, there are two methods: 1.Cascade Timer 0 and Timer 1 but in this way both timers will be tied up. 2.Use one timer in 16-bit mode with a software loop counting overflows.

20 Ex4-1: Pulse Wave Generation Write a program that creates a periodic waveform on P1.0 with as high a frequency as possible. What are the frequency and duty cycle of the waveforms? ORG 0000H LOOP:SETB P1.0; one m/c cycle CLR P1.0; one m/c cycle SJMP LOOP; two m/c cycle END Frequency = 1/4µs = 250kHz Duty Cycle = 25%

21 Ex4-2: 10 kHz Square Wave Write a program using Timer 0 to create a 10 kHz square wave on P1.0. ORG 0000H MOV TMOD, #02H ;auto-reload mode MOV TH0, # -50 ;T= 1/10kHz=100µs SETB TR0 ;start timer LOOP:JNB TF0, LOOP ;wait for overflow CLRTF0 ;clear overflow flag CPL P1.0 ;toggle port bit SJMP LOOP ;repeat END

22 Ex4-3: 1 kHz Square Wave Write a program using Timer 0 to create a 1 kHz square wave on P1.0. ORG 0000H MOV TMOD, #01H ;16-bit mode LOOP:MOV TH0, # FEH ;T= 1/1kHz=1000µs MOV TL0, # 0CH ;-500=FE0CH SETB TR0 ;start timer WAIT:JNB TF0, WAIT ;wait for overflow CLRTR0 ;stop timer CLRTF0 ;clear overflow flag CPL P1.0 ;toggle port bit SJMP LOOP ;repeat END

23 Ex4-4: Buzzer Interface Write a program that sounds the buzzer connected at P1.7 for 1 sec for each 1-to-0 transition detected at P1.6. ORG 0000H MOV TMOD, #01H ;16-bit mode LOOP:JNB P1.6, LOOP ;wait for 1 input LOOP1:JB P1.6, LOOP1 ;wait for 0 input SETB P1.7 ;turn buzzer ON CALL DELAY ;wait for 1 sec CLRP1.7 ;turn buzzer OFF SJMP LOOP ;repeat

24 Ex4-4: Buzzer Interface (contd.) DELAY:MOV R7, #100 ;for 100 loops AGAIN:MOV TH0, #D8H ;10000µsX100=1sec MOV TL0, #F0H ;-10000=D8F0H SETB TR0 WAIT:JNB TF0, WAIT CLR TF0 CLR TR0 DJNZ R7, AGAIN RET END