chapter 5 timer functions
play

Chapter 5 Timer Functions ECE 3120 Dr. Mohamed Mahmoud - PowerPoint PPT Presentation

Chapter 5 Timer Functions ECE 3120 Dr. Mohamed Mahmoud http://iweb.tntech.edu/mmahmoud/ mmahmoud@tntech.edu Outline 5 .1 The Tim er System 5.2 Programming the Timer System 5.3 Examples and Applications The HCS12 Timer System Port T


  1. Chapter 5 Timer Functions ECE 3120 Dr. Mohamed Mahmoud http://iweb.tntech.edu/mmahmoud/ mmahmoud@tntech.edu

  2. Outline 5 .1 The Tim er System 5.2 Programming the Timer System 5.3 Examples and Applications

  3. The HCS12 Timer System Port T Com ponents 1) 1 6 -bit tim er counter 2) Eight channels programmed for input capture or output compare. 3) Each channel has a pin at port T. 4) 11 interrupt sources 1 for timer overflow 8 for channels 5) A group of registers to program the system’s functions 5 - 1

  4. Outline 5 .1 The Tim er System 5 .1 .1 The Tim er 5.1.2 Input Capture Function 5.1.3 Output Compare Function 5.2 Programming the Timer System 5.3 Examples and Applications

  5. - Timer Counter Register (TCNT): 16-bit counter timer - It counts from 0000 to FFFF . After FFFF it rolls to 0000 and continue in counting. - Each time it rolls from FFFF to 0000, it generates interrupt called tim er overflow . - The counter needs a clock to count. - This clock (F) is the CPU clock (E-clock = 24 MHz) divided by a prescale. - One clock is needed to increment the counter by 1. - 2 16 is the number of counts from 0000 to FFFF  The tim er can generate interrupt every 2 1 6 counts ( or clocks) . 2 16 Sec Interrupt every: F E-Clock = 24 MHz TCNT Timer 5 - 2

  6. 0000 0000 0000 0000 1/ F 0000 0000 0000 0001 1/ F 1/ F 2 16 counts 0000 0000 0000 0010 … … … … … … … … … … … … … … … … … … … … … … Timer clock 1111 1111 1111 1111 Timer overflow interrupt 0000 0000 0000 0000 0000 0000 0000 0001 One interrupt every 2 16 0000 0000 0000 0010 sec F … … … … … … … … … … … … … … … … … … … … … … 1111 1111 1111 1111 Timer overflow interrupt 0000 0000 0000 0000 0000 0000 0000 0001 5 - 3

  7. 2 16 2 16 Timeout = = prescale µ second F 24 The timeout can be determined by programming the prescale Min. Timeout = 2.73 ms when prescale value = 1 Max. Timeout = 349.53 ms when prescale value = 128 To do an action every longer time, take action every n interrupts Ex. when prescale = 128 and an action is taken every 4 interrupts --  the action is taken every 1.4 second. Similar to slide 4-29 . The timer usages 1) Similar to RTI, to generate interrupts after certain time, e.g., turn on and off a LED every fixed time. 2) It is used by input capture and output compare functions. 5 - 4

  8. Outline 5 .1 The Tim er System 5.1.1 The Timer 5 .1 .2 I nput Capture Function 5.1.3 Output Compare Function 5.2 Programming the Timer System 5.3 Examples and Applications

  9. - Each channel has a 16-bit capture register (TCx and x = { 0, 1, .. ,7} ) and a pin on port T. - The occurrence of an event is represented by a signal edge (rising or falling edge). - When an event occurs at port T pin number X, the event time (the content of the timer (TCNT)) is recorded in the register TCx and interrupt is requested. Timer (TCNT) TC3 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 … … … … … … … … … … … 1 … … … … … … … … … … … At this moment an event comes to Copy 0000 0001 0000 0000 port T pin 3 … … … … … … … … … … … Interrupt request from IC3 2 5 - 5

  10. Uses of input capture function Event 1 occurred at pin 0 of Port T at count number 5 TC0 TCNT Copy 0000 0000 0000 0101 0000 0000 0000 0101 Event 2 occurred at pin 1 of Port T at count number 9 TCNT TC1 Copy 0000 0000 0000 1001 0000 0000 0000 1001 1- Arrival time comparison  Event 1 occurred before event 2 2- The tim e difference betw een the tw o events = (0009 – 0005) * (1/ F) where (0009 – 0005) is the number of counts (or clocks) between the two events  can be used to measure the phase difference between two signals. 5 - 6

  11. Phase difference m easurem ent t 0 Signal 1 connected to PT0 Signal 2 connected to PT1 t 1 Period m easurem ent - The period = the time difference between two consecutive events t 1 t 2 5 - 7

  12. Outline 5 .1 The Tim er System 5.1.1 The Timer 5.1.2 Input Capture Function 5 .1 .3 Output Com pare Function 5.2 Programming the Timer System 5.3 Examples and Applications

  13. 1- Write a number in a compare register (TCx and x = { 0, 1, .. ,7} ) 2- Once the number of the timer (TCNT) = the number in TCx: 1- Interrupt is generated. 2- An action is taken. The action can be outputting 0, 1 or toggle the PTx pin. Timer (TCNT) TC3 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 … … … … … … … … … … … … … … … … … … … … … … 1 Interrupt request = 0000 0001 0000 0000 from IOC3 … … … … … … … … … … … Action is taken: PT3 = 0 or PT3 = 1 or Toggle PT3 2 5 - 8

  14. Uses of output capture function - Trigger an action at a specific time in the future. Generating a waveform 2100 counts 900 counts 1- Count for TC0 = TCNT + 900, pull OC0 low after counting 2- TC0 = TCNT + 2100, pull OC0 high after counting 3- Go to step 1 5 - 9

  15. Using both input capture and output com pare - A sensor interrupts the microcontroller when the containers are in the proper place - Then, the microcontroller open a valve for a certain time to fill up containers The timer system applications can be done by the timer’s circuits without the direct involvement of the CPU big applications 5 - 10

  16. Outline 5.1 The Timer System 5 .2 Program m ing the Tim er System 5 .2 .1 Program m ing The Tim er 5.2.2 Programming Input Capture and Output Compare 5.3 Examples and Applications

  17. 1- Main program 1.1 Initializing the interrupt vector 1- Interrupt vector: $FFDE org $FFDE dc.w Timer_ISR ;load timer interrupt routine vector Priority 5 - 11

  18. 2- Interrupt enable bit and setting prescale Timer System Control Register 2 (TSCR2) - Bit 7 is the timer overflow interrupt enable bit. - Bits 0, 1, and 2 are used to set the prescale. TOI enables/ disables interrupts, but it does not start/ stop counting When interrupt is disabled, the timer counts but an interrupt is not generated when it rolls from FFFF to 0000 5 - 12

  19. Start/ stop counting Timer System Control Register 1 (TSCR1) - Setting and clearing TEN (bit 7 of TSCR1) will start and stop the counting of the TCNT. TEN starts/ stops the timer counting. When the timer stops counting, the interrupts stop automatically because it will not roll from FFFF to 0000 Timer Interrupt Flag Bit - Timer Interrupt Flag 2 Register (TFLG2) - In the interrupt subroutine, write one in Bit 7 of TFLG2 to clear the interrupt flag bit. movb #%10000000,TFLG2 5 - 13

  20. Summary 1- Programming the timer interrupt org $FFDE dc.w timer_isr ; set up TCNT overflow interrupt vector movb #$80,TSCR1 ;enable timer counter #$86,TSCR2 ; enable TCNT overflow interrupt, set prescaler to 64 movb timer_isr: movb #%10000000,TFLG2 ; clear Timer interrupt flag ; code is here rti 2- Programming the timer without using interrupt movb #$80,TSCR1 ;enable timer counter #$06,TSCR2 ; disable TCNT overflow interrupt, set prescaler to 64 movb 5 - 14

  21. Outline 5.1 The Timer System 5 .2 Program m ing the Tim er System 5.2.1 Programming The Timer 5 .2 .2 Program m ing I nput Capture and Output Com pare 5.3 Examples and Applications

  22. 1- Main program 1.1 Initializing the interrupt vectors of timer channels org $FFE0 dc.w Timer_Ch7 ;load Channel 7 ISR vector Priority 5 - 15

  23. Select either Input-Capture or Output-Compare Function - The same pins can be used for Input-Capture and Output-Compare Functions. - Only one function can be selected at a time. - Timer input capture/ output compare (TIOS) register is programmed to select either input-capture or output-compare for each channel movb #$00,TIOS ; all channels are input capture movb #$FF,TIOS ; all channels are output capture movb #$F0,TIOS ; the first 4 channels are input capture and the last 4 channels are output capture 5 - 16

  24. 2- Local interrupt enable bits - The enabling of the interrupt is controlled by the Timer Interrupt Enable Register (TIE) - Channels can generate interrupts if it is enabled or its bit in TIE is 1 movb #$1,TIE ; enable interrupt of channel 1 movb #$FF,TIE ; enable the interrupts of all channels 5 - 17

  25. 3- Interrupt Flags - Whenever an interrupt occurs, the associated timer interrupt flag in Timer Interrupt Flag 1 (TFLG1) register will be set to 1. Flag CxF is cleared by writing a “1 ” to bit x of this register Example: movb # $01,TFLG1 will clear the C0F flag. 5 - 18

  26. The actions that can be activated on an output compare - The actions that can be activated on an output compare pin include 1. pull up to high 2. pull down to low 3. toggle - The action is determined by the Timer Control Register 1 & 2 (TCTL1 & TCTL2): 5 - 19

  27. Input capture respond to rising or falling edge? - The signal edge to be captured is selected by Timer Control Register 3 and 4 (TCTL3 and TCTL4). - The edge to be captured is selected by two bits. You can choose to capture the rising edge, falling edge, or both edges. 5 - 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend