Module5_Timers_Counters.pdf
Document Details
Uploaded by Ameera
University of Sharjah
Related
- PCSII Depression/Anxiety/Strong Emotions 2024 Document
- A Concise History of the World: A New World of Connections (1500-1800)
- Human Bio Test PDF
- University of Santo Tomas Pre-Laboratory Discussion of LA No. 1 PDF
- Vertebrate Pest Management PDF
- Lg 5 International Environmental Laws, Treaties, Protocols, and Conventions
Full Transcript
Embedded Systems Design Timers and Counters Dr. Bilal Arain Based on slides from Dr. Bassel Soudan Design Problem Design Problem – What do we need? A means for measuring 1 second exactly, A means for counting the pulses coming from the infrared sensor, A prog...
Embedded Systems Design Timers and Counters Dr. Bilal Arain Based on slides from Dr. Bassel Soudan Design Problem Design Problem – What do we need? A means for measuring 1 second exactly, A means for counting the pulses coming from the infrared sensor, A program to send the result to the 7-segment displays at the end of the second, We also need to do all of this at the same time. The best way to do this is to use one of the microcontroller’s most important features, the on-chip independent timers and counters. Timers/Counters Timer/Counters Timers/Counters - Clock Source Timers/Counters - Clock Source Timers/Counters – Clock Source External Crystal (Asynchronous Mode) The timer gets its clock from an external independent oscillator. The counter is incremented according to the external signal and operates asynchronously to the rest of the controller. This mode is typically used for timing very slow signals. The PIC operates on MHz frequency. Timing a KHz frequency signal will generate very large counts that may not fit in the limited bits of the timers. A separate KHz range oscillator can be used to generate the clock signal for the timer. Timers and Prescaler Timers and Prescaler Postscaling the count Postscaling allows counting multiple loops of the counter before asserting the completion flag. Typically, a “flag” bit is set whenever the counter completes one cycle. With postscaling, the flag will only be set after the counter has completed “x” cycles. “x” is programmable. Allows measuring longer periods of time using the same counter. Postscaling the count The PIC16F877 Timers PIC16F877 Timers PIC16F877 Timers The PIC16F877 has three timers/counter These are referred to as Timer0, Timer1, and Timer2 Special Function Registers Some “high use” SFRs from bank0 are mirrored in the other banks (e.g., INDF, PCL, STATUS, FSR, PCLATH, INTCON) Timer-related Special Function Registers Timer-related Special Function Registers The three timers of the PIC16F877 exist in the SFR area. Timer0 is located at address 0x01 Timer1 is located at addresses 0x0E and 0x0F Timer2 is located at address 0x11 These locations are available for read and write We can read the current count value We can set the starting count value Other SFRs are required to configure and control these timers INTCON Interrupt Control Register OPTION_REG Options Register T1CON Timer 1 Control Register T2CON Timer 2 Control Register INTCON Timer 0 Block Diagram T0CKI Pin can be used for the External Clock In timer mode, it is incremented on every instruction clock pulse unless a prescaler is used to reduce the speed. OPTION Register Timer 0 Features 8-bit timer/counter Readable and writeable – TMR0 register in SFR 3-bit software programmable prescaler Internal or External clock select Interrupt on overflow Programmable external clock edge select (rising or falling) Timer 0 Operation Control the Timer 0 operation using the OPTION Register Timer 0 is a free running timer. It does not have a start/stop signal It starts counting the instant the microcontroller is powered. Timer 0 Block Diagram T0CKI Pin can be used for the External Clock In timer mode, it is incremented on every instruction clock pulse unless a prescaler is used to reduce the speed. Timer 0 Block Diagram Steps to Configure Timer 0 Following are the step-by-step guide to use the Timer 0: 1. Timer mode is selected by the T0CS bit of the OPTION_REG register, (T0CS: 0=timer, 1=counter). 2. Configuring the Prescaler Assignment and Prescaler Rate Select bits of the same register 3. The flag bit TMR0IF of the INTCON register is automatically set every time the TMR0 register overflows. Timer 0 Configuration: OPTION Register Timer mode is selected by clearing the TMR0CS bit of the OPTION_REG register. Timer 0 Configuration: OPTION Register The rising or falling transition of the incrementing edge for either input source is determined by the TMR0SE bit in the OPTION_REG register. (T0CKI) Timer 0 Configuration: OPTION Register The prescaler is enabled by clearing the PSA bit of the OPTION_REG register. There are eight prescaler options for the Timer0 module ranging from 1:2 to 1:256. The prescale values are selectable via the PS bits of the OPTION_REG register. Timer 0 Configuration: INTCON Register The TMR0IF interrupt flag bit of the INTCON register is set every time the TMR0 register overflows from 0xFF to 0x00. This allows the software to poll the TMR0IF bit asynchronously. The Timer0 interrupt enable bit (TMR0IE) of the INTCON register will be used later. Timer 0 Example Code: Initialization in C Timer 0 Calculations (Internal Clock) Timer 0 Calculations (External Clock) If using the External Clock Source, the division is performed as follows: In this case there is no division by 4 of the original clock. We use the external frequency as is. Timer 0 Example Code: Implementation in C