1 / 58

TIMER

TIMER. Registers Used in Timer/Counter. TH0, TL0, TH1, TL1 TMOD (Timer mode register) TCON (Timer control register). Timer register. D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0. TL0/TL1. TH0 /TH1. TMOD Register :.

eldora
Télécharger la présentation

TIMER

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TIMER

  2. Registers Used in Timer/Counter • TH0, TL0, TH1, TL1 • TMOD (Timer mode register) • TCON (Timer control register)

  3. Timer register D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 TL0/TL1 TH0 /TH1

  4. TMOD Register: • Gate : When set, timer only runs while INT(0,1) is high. • C/T : Counter/Timer select bit. • M1 : Mode bit 1. • M0 : Mode bit 0.

  5. TF1: Timer 1 overflow flag. TR1: Timer 1 run control bit. TF0: Timer 0 overflag. TR0: Timer 0 run control bit. IE1: External interrupt 1 edge flag. IT1: External interrupt 1 type flag. IE0: External interrupt 0 edge flag. IT0: External interrupt 0 type flag. TCON Register:

  6. Timer modes • Timer Mode 0 : • Emulates 8048 counter/timer (13-bits). • 8-bit counter (TL0 or TL1). • 5-bit prescaler (TH0 or TH1).

  7. Timer Mode 1 : • Simple 16-bit counter. • -Timer Mode 2 : • 8-bit auto-reload. • Counter in TL0 or TL1. • Reload value in TH0 or TH1. • Provides a periodic flag or interrupt

  8. Timer Mode 3 • Splits timer 0 into two 8-bit counter/timers. • First counter (TLO) acts like mode 0, without prescaler. • Second counter (TH0): • Counts CPU cycles. • Uses TR1 (timer 1 run bit) as enable. • Uses TF1 (timer 1 overflow bit) as flag. • Uses Timer 1 interrupt.

  9. TIMING MODE DIAGRAM

  10. ANIMATED TIMER MODE: SFR TMOD TH1 TL1 FF FFFB FFFC FFFD FFFE FFFF FB FFFF+01 TR1 TF1 1 0 1 0

  11. ANIMATED TIMER MODE: SFR TMOD TH1 TL1 FF FFFB FFFC FFFD FFFE FFFF FB FFFF+01 TR1 TF1 1 0 1 0

  12. SAMPLE PGM FOR TIMER: • ORG 0H • START:SETB P1.0 • ACALL DELAY • CLR P1.0 • ACALL DELAY • SJMP START

  13. SAMPLE PROGRAM • DELAY:MOV TMOD,#0001 0000B ;10H MOV TL1,#00 MOV TH1,#00 SETB TR1 JNB TF1,LOOP CLR TR1 CLR TF1 RET

  14. SERIAL PORT

  15. SCON REGISTER

  16. SM0,SM1 • - SM0, SM1 = Serial Mode: • 00 = Mode 0 : Shift register I/O expansion. • 01 = Mode 1 : 8-bit UART with variable baud rate. • 10 = Mode 2 : 9-bit UART with fixed baud rate. • 11 = Mode 3 : 9-bit UART with variable baud rate.

  17. SM2 • - SM2 : • Mode 0 : Not used. • Mode 1 : 1 = Ignore bytes with no stop bit. • Mode 2,3 : 0 = Set receive interrupt (RI) on all bytes. • : 1 = Set RI on bytes where bit 9 = 1.

  18. REN = Enables receiver. • - TB8 = Ninth bit transmitted (in modes 2 and 3). • - RB8 = Ninth bit received: • Mode 0 : Not used. • Mode 1 : Stop bit. • Mode 2,3 : Ninth data bit. • - TI = Transmit interrupt flag. • - RI = Receive interrupt flag.

  19. BAUD RATE

  20. Mode 1:TX

  21. MODE 1:TX TI = 0 TI=1 STOP BIT 8 BIT DATA PARALLEL TO SERIAL TX 8 BIT DATA DATA SBUF SERIAL DATA TRANSFER START BIT START STOP

  22. MODE 1:TX TI = 0 TI=1 STOP BIT 8 BIT DATA PARALLEL TO SERIAL TX 8 BIT DATA DATA SBUF SERIAL DATA TRANSFER START BIT START STOP

  23. DATA TX SERIALLY: • Load TMOD Register ;timer1 • Load TH1 or TL1 values ; for Baud rate • Load SCON register; serial mode 0,1,2,3 • SET TR1 ;To start the timer • Character byte is written to SBUF; • Check if TI=1; • CLR TI • Reload the value again.

  24. SAMPLE PROGRAM:TX • MOV TMOD,#20H • MOV TH1,#-3 • MOV SCON,#50H • SETB TR1 • AGAIN:MOV SBUF,#’Y’ • JNB TI ,Here • CLR TI • SJMP AGAIN

  25. SERIAL DATA TRANSFER Y 8051 DISPLAY N

  26. SAMPLE PROGRAM: • MOV TMOD,#20H • MOV TH1,#-3 • MOV SCON,#50H • SETB TR1 • AGAIN:MOV SBUF,#’Y’ • JNB TI,Here • CLR TI • MOV P1,#’N’ • SJMP AGAIN

  27. Mode 1:RX

  28. MODE 1:RX RI=1 RI=0 START BIT 8 BIT DATA PARALLEL TO SERIAL TX DATA RX 8 BIT DATA SBUF SERIAL DATA RECEIVE STOP BIT START STOP

  29. SAMPLE PROGRAM:RX • Load TMOD Value. • Load TH1 value ;baud rate • Load SCON Register • Set TR1 To start timer. • Clear RI • Check the RI each time. • When RI =1,Load the value from SBUF.

  30. SAMPLE PROGRAM:RX • MOV TMOD,#20H • MOV TH1,#-3 • MOV SCON,#50H • SETB TR1 • CLR RI • LOOP:JNB RI,LOOP • MOV A,SBUF • END

  31. SERIAL PORT TRANSMITTER

  32. SERIAL MODE 2 TI = 0 TI=1 STOP BIT TB8=0 TB8=1 8 BIT DATA PARALLEL TO SERIAL TX 8 BIT DATA DATA SBUF SERIAL DATA TRANSFER START BIT START STOP

  33. RECEIVE DATA

  34. SERIAL MODE 2 :RX RI=1 RI=0 START BIT 8 BIT DATA PARALLEL TO SERIAL TX DATA RX 8 BIT DATA SBUF SERIAL DATA RECEIVE RB8=0 STOP BIT START STOP

  35. Interrupt :

  36. Interrupts Programming • An interrupt is an external or internal event that interrupts the microcontroller to inform it that a device needs its service. Interrupts vs. Polling • A single microcontroller can serve several devices. • There are two ways to do that: • interrupts • polling. • The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.

  37. Steps in executing an interrupt • Finish current instruction and saves the PC on stack. • Jumps to a fixed location in memory depend on type of interrupt • Starts 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. Getpop PC from stack

  38. PROGRAM COUNTER • It saves the address next insturction going to be executed.

  39. MAIN PGM INTERRUPT PROGRAM ISR END

  40. Example • A 10khz square wave with 50% duty cycle ORG 0;Reset entry poit LJMP MAIN;Jump above interrupt ORG 000BH;Timer 0 interrupt vector T0ISR:CPL P1.0;Toggle port bit RETI;Return from ISR to Main program ORG 0030H;Main Program entry point 0030:MAIN: MOV TMOD,#02H;Timer 0, mode 2 0032:MOV TH0,#-50;50 us delay 0034:SETB TR0;Start timer 0036:MOV IE,#82H;Enable timer 0 interrupt 0038:SJMP $;Do nothing just wait END

  41. INTERRUPT PGM STACK POINTER MAIN PGM 0B 0A 09 08 INTERRUPT ISR 00 PCH 38 PCL

  42. Interrupt Sources • Original 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) • Enhanced version has 22 sources • More timers, programmable counter array, ADC, more external interrupts, another serial port (UART)

  43. Interrupt Vectors Each interrupt has a specific place in code memory where program execution (interrupt service routine) begins. External Interrupt 0: 0003h Timer 0 overflow: 000Bh External Interrupt 1: 0013h Timer 1 overflow: 001Bh Serial : 0023h Timer 2 overflow(8052+) 002bh Note: that there are only 8 memory locations between vectors.

  44. ISRs and Main Program in 8051 SJMP main ORG 03H ljmp int0sr ORG 0BH ljmp t0sr ORG 13H ljmp int1sr ORG 1BH ljmp t1sr ORG 23H ljmp serialsr ORG 30H main: … END

  45. Interrupt Enable Register : • 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.

  46. Enabling and disabling an interrupt • by bit operation • Recommended in the middle of program SETB EA ;Enable All SETB ET0 ;Enable Timer0 ovrf SETB ET1 ;Enable Timer1 ovrf SETB EX0 ;Enable INT0 SETB EX1 ;Enable INT1 SETB ES ;Enable Serial port • by mov instruction • Recommended in the first of program MOV IE, #10010110B SETB IE.7 SETB IE.1 SETB IE.3 SETB IE.0 SETB IE.2SETB IE.4

More Related