Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 3430 – Intro to Microcomputer Systems

Similar presentations


Presentation on theme: "ECE 3430 – Intro to Microcomputer Systems"— Presentation transcript:

1 ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture #17 Agenda Today Output Compare Forced Output Compare HC11 Timer Review Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

2 Timer Output Compare Function
- Sometimes it is desirable for a microcontroller to generate a precisely-timed digital waveform. Programmatically driving a waveform by repeatedly forcing a bit high and low will not yield a digital waveform with good integrity! - Some microcontrollers provide a feature called output compare which can be used to more accurately produce digital waveforms. - The HC11 (D3) provides 5 output compare channels. - Each channel has a dedicated 16-bit output compare register. - A 16-bit value is loaded into the output compare registers. When TCNT is equal to this value, an internal event is triggered. - When this internal event is triggered the output compare circuitry will alter the value on an output pin. These output pins are shared with port A (each channel has a dedicated output pin): OC1 = PA7 (bi-directional, need to set to OUTPUT if using, shared with PAI) OC2 = PA6 OC3 = PA5 OC4 = PA4 OC5 = PA3 (bi-directional, need to set to OUTPUT if using, shared with IC4) - In the HC11 (D3), OC1 operates a little differently than the others (OC2-5). OC1 can control any or all of PA7-PA3. But only OC1 has access to PA7. Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

3 Timer Output Compare Function
Output Compare Details - When the output compare generates an internal event (when OC register and TCNT match), the OCx pins in the TCTL1 register ($0020) specify what to do to the respective output pin: 1) Drive the output pin to logic 1 2) Drive the output pin to logic ) Toggle state of output pin (0  or  0) OMx OLx Timer disconnected from output pin logic Toggle OCx output drive level OCx output drive level to zero OCx output drive level to one NOTE: OC1 uses OC1D and OC1M control registers instead. OM2 OL2 OM3 OL3 OM4 OL4 OM5 OL5 Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

4 Timer Output Compare Function
Output Compare Details - The 16-bit output compare registers for each of the 5 output compare channels are located at: TOC1 = {$0016 : $0017} TOC2 = {$0018 : $0019} TOC3 = {$001A : $001B} TOC4 = {$001C : $001D} TI4O5 = {$001E : $001F} - The last register is used for either input capture or output compare—hence the weird register name. Theory of how we use output compare Ex) Generate a ‘1’ on PA6 256 cycles from NOW!!! Assume PA6 is currently ‘0’. - Set up OC channel 2 to set output pin on successful compare and enable. - Load TCNT into ACCD (LDD TCNT) - Add 256 to it (ADDD #256) - Store that value to TOC2 (STD TOC2) - When TCNT reaches that value, it will trigger an output compare internal event. Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

5 Timer Output Compare Function
Output Compare Details FLAGS - When the output compare internal event occurs, a flag is set in the TFLG1 register ($0023): OC1F, OC2F, OC3F, OC4F, I4O5F OCxF = 0 = waiting for successful compare (RESET state) OCxF = 1 = TCNT and TOCx registers successfully compared - When a successful compare occurs, the respective flag is set. To use that flag again, you must clear it by writing a ‘1’ to the proper bit. - Remember: The hardware sets the flag bit when TCNT and TOCx are equal. Software has the responsibility of clearing that flag. Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

6 Timer Output Compare Function
Output Compare Details (Optional Interrupts) - Just like virtually all of the other on-chip peripheral devices, the output compare system can generate an interrupt when TCNT and the TOCx registers are equal. - Each output compare channel has a relative priority. Output compare channel 1 has a higher priority than any of the other output compare channels. It has a global and local interrupt enable: Global mask = I-bit in CCR (another maskable interrupt) Local mask = OCxI bits in the TMSK1 register ($0022) OCxI = 0 = Disabled (RESET State) OCxI = 1 = Enabled - When a particular output compare channel is turned on, it is disconnected from the normal output pin control logic. Hence, once you turn on the output compare for a channel, the respective pin can only be changed by the hardware (writing a value to the pin by storing to a port register will have no effect)! Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

7 Timer Output Compare Function
Output Compare Applications Generate Square Waves - A square wave with a pre-defined duty cycle and frequency (period) can be generated. Useful in PWM control (motors) Generate Fixed Delay after Event When an event occurs, we can trigger a specific # of TCNT’s after the event. Could also be an accurate delay subroutine Generate a Pulse with a Certain Width Trigger two ‘toggle’ output compares a certain  TCNT between edges. Event Output Compare  TCNT Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

8 Timer Output Compare Function
Output Compare Example Generate a square wave with a pulse width equal to TCNT rollover on PA6 (OC2) TMSK1 EQU $22 OC2I EQU % ; OC2 local interrupt enable (bit 6) TFLG1 EQU $23 OC2F EQU % ; OC2 flag (bit 6) TCTL1 EQU $20 TOC2 EQU $18 ; OC2 compare register ORG $FFE6 ; initialize TOC2 Vector Table FDB OC2_ISR ORG $FFFE ; initialize RESET Vector Table FDB $E000 Roll Over Roll Over Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

9 Timer Output Compare Function
Output Compare Example Cont… ORG $E000 SEI ; disable maskable interrupts during setup LDS #$00FF ; initialize stack pointer BSET TMSK1 OC2I ; enable local IRQ mask for OC2 BCLR TFLG1 OC2F ; clear the OC2 Flag LDAA TCTL ; set the OC2 function to toggle ORAA #% ANDA #% STAA TCTL1 LDX #$5555 ; initialize TOC2 to $ STX TOC2 ; every time TCNT = $5555, OC2 generates event CLI ; enable maskable interrupts, we are now ready MAIN: BRA MAIN ; the main loop doesn’t do anything with OC2 * On each interrupt, PA6 will toggle state OC2_ISR: BCLR TFLG1 OC2F ; all we need to do is clear the OC2 flag RTI ; so the next interrupt will fire Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

10 Timer Output Compare Function
Forced Output Compare - We can “force” an output compare function at anytime—even if TCNT and the TOCx register don’t match. - By writing to the CFORC register ($000B), we can specify which output compare to trigger Writing a 1 forces a compare. Register is set to zero on reset. - We would use this if we wanted to insert aperiodic edges in addition to our periodic edges. - Also since we cannot re-initialize the state of the outputs once the output compare system is engaged, we might use a forced compare to re-initialize the output pin state without disengaging the output compare logic (which could induce glitches). FOC1 FOC2 FOC3 FOC4 FOC5 Forced Periodic Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

11 ECE 3430 – Intro to Microcomputer Systems
Timer Review HC11 Timers Main Timer - A 16-bit free-running, read-only counter that runs off of the E-clock. It can trigger an interrupt upon overflow ($FFFF-$0000). The speed of the counter can be clocked down by a factor of 4, 8, or 16. Input Capture – Used to capture external events via dedicated input pins. When the event occurs, the value of TCNT is loaded into a pre-defined register. This is then used to calculate relative timing information between events. This can also cause an interrupt. Output Compare - Compare registers are loaded with a 16-bit values. When TCNT reaches the value in the register, an output compare trigger will occur. Upon the compare event, output pins on the HC11 can be altered. This can also cause an interrupt. Forced Output Compare - At any time, software can force any of the output compare functions to trigger by writing to the CFORC register. Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009

12 ECE 3430 – Intro to Microcomputer Systems
Timer Review HC11 Timers Real Time Interrupts – A sub-system independent from the 16-bit free-running counter (TCNT) but derived from the E-clock. It can be programmed to generate periodic internal interrupts. Used to provide a semi-accurate time base for software and hardware. This is similar to the “Main Timer Overflow Interrupts” but generates quicker interrupts. Pulse Accumulator - A counter that can be incremented upon an event on a dedicated input pin or a counter that is incremented by E-clock/64 where the counter is gated by an input pin. Lecture #17 ECE 3430 – Intro to Microcomputer Systems Fall 2009


Download ppt "ECE 3430 – Intro to Microcomputer Systems"

Similar presentations


Ads by Google