The 8051 Microcontroller - Chapter 4: Timer Operation - Lê Chí Thông

Timer/Counter
• 3 Functions
1. Timer is used as time delay generator (interval
timing)
– Internal clock source
2. An event counter (event counting)
– External clock source
– For example :
• number of people passing through an entrance
• number of wheel rotations
• any other event that can be converted to pulses
3. Baud rate generation for serial port 
pdf 24 trang thamphan 28/12/2022 960
Bạn đang xem 20 trang mẫu của tài liệu "The 8051 Microcontroller - Chapter 4: Timer Operation - Lê Chí Thông", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdfthe_8051_microcontroller_chapter_4_timer_operation_le_chi_th.pdf

Nội dung text: The 8051 Microcontroller - Chapter 4: Timer Operation - Lê Chí Thông

  1. ĐH Bách Khoa TP.HCM Lê Chí Thông The 8051 Microcontroller Chapter 4 Timer Operation Lê Chí Thông chithong@hcmut.edu.vn sites.google.com/site/chithong Ho Chi Minh City University of Technology Lê Chí Thông 1 Review of 3-bit Up Counter 000 001 111 000 LêOverflow Chí Thông 2 sites.google.com/site/chithong 1
  2. ĐH Bách Khoa TP.HCM Lê Chí Thông Clock Source Internal clock fCLK = fCrystal / 12 to Timer External clock C/T Clock Function 0 Internal Timer (interval timing, delay) 1 External Counter (event counting) Lê Chí Thông 5 Timer 1 mode 1 (16 bit) Lê Chí Thông 6 sites.google.com/site/chithong 3
  3. ĐH Bách Khoa TP.HCM Lê Chí Thông GATE bit: More details • GATE=0 • Internal control • The start and stop of the timer are controlled by the software Ex: SETB TR1 ; Run Timer 1 CLR TR1 ; Stop Timer 1 • GATE=1 • External control • The hardware way of starting and stopping Timer Timer the timer by software stops runs and an external source . • When GATE is set and TRx is set (SETB TRx), Timer runs only while the INTx pin is high. Lê Chí Thông 9 Applications of Timer Gate C/T Application 0 Delay 0 1 Event counting; Frequency meter 1 0 Pulse width meter Lê Chí Thông 10 sites.google.com/site/chithong 5
  4. ĐH Bách Khoa TP.HCM Lê Chí Thông TMOD Register (MSB) TMOD: Timer Mode Register (LSB) GATE C/T M1 M0 GATE C/T M1 M0 Timer 1 Timer 0 GATE 0 : Timer/counter counts only while TRx bit is set. 1 : Timer/counter counts only while TRx bit is set and INTx pin is high C/T 0 : Timer operation (clock : Machine cycle) 1 : Counter operation (clock : Tx input pin) Lê Chí Thông 13 M1, M0: mode setting bits M1 M0 Mode Operating mode 0 0 0 13-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1) 0 1 1 16-bit timer mode 8-bit THx + 8-bit TLx 1 0 2 8-bit auto-reload mode 8-bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows. 1 1 3 Split timer mode Lê Chí Thông 14 sites.google.com/site/chithong 7
  5. ĐH Bách Khoa TP.HCM Lê Chí Thông 4 Timer Modes Mode 0 : 13-bit counter Mode 1 : 16-bit counter (4048 mode) Mode 2 : 8-bit auto reload counter Mode 3 : two 8-bit counter the other counter will not Lê Chí Thông output overflow (interrupt)17 TCON Register (1) • Timer control register: TCON – Upper nibble : timer/counter – Lower nibble : interrupts • TR (run control bit) – TR0 : Timer/counter 0 – TR1: Timer/counter 1. – Turn timer/counter on/off. • TR=0: off (stop) • TR=1: on (start) (MSB) (LSB) TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Timer 1 Timer0 for Interrupt Lê Chí Thông 18 sites.google.com/site/chithong 9
  6. ĐH Bách Khoa TP.HCM Lê Chí Thông Delay 100 µs using Timer 1 (12 MHz crystal) MOV TMOD, #00010000B; Timer 1, mode1 MOV TL1, #9CH ; Initial count MOV TH1,#0FFH ; -100 = FF9CH TMOD  10H SETB TR1 ; start Timer 1 TH1:TL1  -100 WAIT: JNB TF1, WAIT ; wait for overflow CLR TF1 ; clear overflow flag Run Timer 1 CLR TR1 ; stop Timer 1 Note: N Overflow? MOV TL1,#9CH = MOV TL1,# LOW (-100) (TF1=1?) MOV TH1,0FFH = MOV TH1,# HIGH (-100) Y WAIT: JNB TF1, WAIT = JNB TF1, $ Clear overflow flag Lê Chí Thông Stop Timer 1 21 Delay 25 µs using Timer 0 (24 MHz crystal) Your Turn! Lê Chí Thông 22 sites.google.com/site/chithong 11
  7. ĐH Bách Khoa TP.HCM Lê Chí Thông 10-Hz Square Wave Write a program using Timer 0 to create a 10 Hz square wave on P1.0 MOV TMOD, #01H ; Timer 0, mode 1 (16-bit timer mode) LOOP: MOV TH0, #HIGH(-50000); high byte of -50,000 MOV TL0, #LOW(-50000) ; low byte of -50,000 SETB TR0 ; start timer WAIT: JNB TF0, WAIT ; wait for overflow CLR TR0 ; stop timer CLR TF0 ; clear timer overflow flag CPL P1.0 ; toggle port bit SJMP LOOP ; repeat Delay 50,000 μs (source ) P1.0  NOT (P1.0) Lê Chí Thông 25 10-kHz Square Wave Write a program using Timer 0 to create a 10 kHz square wave on P1.0 Lê Chí Thông 26 sites.google.com/site/chithong 13
  8. ĐH Bách Khoa TP.HCM Lê Chí Thông 10-kHz Square Wave using Mode 2 Write a program using Timer 0 to create a 10 kHz square wave on P1.0 MOV TMOD, #02H ; Timer 0, mode 2 (8-bit auto-reload) MOV TH0, #-50 ; reload value SETB TR0 ; start timer WAIT: JNB TF0, WAIT ; wait for overflow CLR TF0 ; clear timer overflow flag CPL P1.0 ; toggle port bit SJMP WAIT ; repeat Mode 2: 8-bit auto-reload timer Lê Chí Thông 29 Buzzer Interface A buzzer is connected to P1.7 and a debounced switch is connected to P1.6. Write a program that reads the logic level provided by the switch and sounds the buzzer for 1 second for each 1-to-0 transition detected. Lê Chí Thông 30 sites.google.com/site/chithong 15
  9. ĐH Bách Khoa TP.HCM Lê Chí Thông Techniques for Programming Timed Intervals • 12 MHz operation Maximum Technique interval [μs] ≈10 Software tuning 256 8-bit timer with auto-reload 65536 16-bit timer No limit 16-bit timer plus software loops Lê Chí Thông 33 Very Short Intervals Very short intervals (i.e. high frequencies) can be programmed without using timers. LOOP: SETB P1.0 CLR P1.0 SJMP LOOP Lê Chí Thông 34 sites.google.com/site/chithong 17
  10. ĐH Bách Khoa TP.HCM Lê Chí Thông Counter 0 Mode 1 • C/T = 1 • 16-bit counter (TH0 and TL0) • TH0-TL0 is incremented when TR0 is set to 1 and an external pulse (in T0) occurs. Timer 0 Overflow external clock flag input (P3.4/T0) TH0 TL0 TF0 TF0 goes high when FFFF 0 C/T = 1 TR0 Lê Chí Thông 37 Counter_BarLED A push button is connected to P3.4 (T0). Assume that there is no contact bounce. Write a program that counts the pulses created by push button and display on the LED-Bargraph connected to Port 1. (schematic ) Lê Chí Thông 38 sites.google.com/site/chithong 19
  11. ĐH Bách Khoa TP.HCM Lê Chí Thông Counter_7segLED ORG 0000H MAIN: MOV TMOD,#00000101B BCDTO7SEG: MOV TH0,#0 MOV DPTR,#TABLE MOV TL0,#0 MOVC A,@A+DPTR SETB TR0 RET LOOP: MOV A,TL0 CJNE A,#10,NEXT TABLE: DB 40h,79h,24h,30h,19h CLR A DB 12h,02h,78h,00h,10h MOV TL0,#0 NEXT: ACALL DISPLAY DONE: NOP SJMP LOOP END DISPLAY: ACALL BCDTO7SEG MOV P1,A RET (source ) Lê Chí Thông 41 Frequency Meter_7segLED A clock source is connected to P3.4 (T0). Write a program that displays the frequency in KHz on the common-anode 7-segment LED connected to Port 1. ( schematic ) Lê Chí Thông 42 sites.google.com/site/chithong 21
  12. ĐH Bách Khoa TP.HCM Lê Chí Thông Pulse_width_7segLED A pulse source is connected to P3.2 (/INT0). Write a program that displays the pulse width in ms (approximate) on the common- anode 7-segment LED connected to Port 1. ( schematic ) Lê Chí Thông 45 Pulse_width_7segLED ORG 0000H MOV TMOD,#00001001B DISPLAY: ;Timer 0,16 bit,internal clock,GATE=1 ACALL BCDTO7SEG MOV TH0,#0 MOV P1,A MOV TL0,#0 RET SETB TR0 AGAIN: MOV A,TH0 BCDTO7SEG: CJNE A,TH0,AGAIN MOV DPTR,#TABLE MOV B,#4 MOVC A,@A+DPTR DIV AB RET ;A=Pulse width in us/256/4 ;approximate /1000 TABLE: DB 40h,79h,24h,30h,19h ACALL DISPLAY DB 12h,02h,78h,00h,10h SJMP AGAIN DONE: NOP (source ) END Lê Chí Thông 46 sites.google.com/site/chithong 23