DOC PREVIEW
NMT EE 308 - Ways to implement delays

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 Spring 2011 • The 9S12 Output Compare Function • Huang Section 8.6 • ECT_16B8C Block User Guide o Interrupts on the MC9S12 o The MC9S12 Output Compare Function o Registers used to enable the Output Compare Function o Using the MC9S12 Output Compare Function o A program to use the MC9S12 Output Compare to generate a square wave o Setting and clearing bits in the Timer Subsystem Ways to implement delays • Software Delay void delay (unsigned int ms) { unsigned int i; while (ms > 0) { i = D_1MS; while (i > 0) { i = i - 1; } ms = ms - 1; } } Cannot do anything while waiting • Timer Overflow or Real Time Interrupt Only fixed delaysEE 308 Spring 2011 The MC9S12 Output Compare Function Want event to happen at a certain time Want to produce pulse with width T Wait until TCNT == 0x0000, then bring PA0 high Wait until TCNT == T, then bring PA0 low while (TCNT != 0x0000) ; PORTA = PORTA | 0x01; while (TCNT != T) ; PORTA = PORTA & ~0x01; Problems: 1) May miss TCNT == 0x0000 or TCNT == T 2) Time not exact −− software delays 3) Cannot do anything else while waitingEE 308 Spring 2011 Want event to happen at a certain time Want to produce pulse with width T Wait until TCNT == 0x0000, then bring PA0 high Wait until TCNT == T, then bring PA0 low Now pulse is exactly T cycles longEE 308 Spring 2011 OUTPUT COMPARE PORT T 0−7 To use Output Compare, you must set IOSx to 1 in TIOSEE 308 Spring 2011 The HCS12 Output Compare Function • The MC9S12 allows you to force an event to happen on any of the eight PORTT pins • An external event is a rising edge, a falling edge, or a toggle • To use the Output Compare Function: – Enable the timer subsystem (set TEN bit of TSCR1) – Set the prescaler – Tell the HCS12 that you want to use Bit x of PORTT for output compare – Tell the HCS12 what you want to do on Bit x of PORTT (generate rising edge, falling edge, or toggle) – Tell the HCS12 what time you want the event to occur – Tell the HCS12 if you want an interrupt to be generated when the event is forced to occur • There are some more complicated features of the output compare subsystem which are activated using registers CFORC, OC7M, OC7D and TTOV. – Writing a 1 to the corresponding bit of CFORC forces an output compare event to occur, the same as if a successful comparison has taken place (Section 8.6.5 of Huang). – Using OC7M and OC7D allow Timer Channel 7 to control multiple output compare functions (Section 8.6.4 of Huang). – Using TTOV allows you to toggle an output compare pin when TCNT overflows. This allows you to use the output compare system to generate pulse width modulated signals. – We will not discuss these advanced features in this class.EE 308 Spring 2011 Write a 1 to Bit 7 of TSCR1 to turn on timer To turn on the timer subsystem: TSCR1 = 0x80; Set the prescaler in TSCR2 Make sure the overflow time is greater than the width of the pulse you want to generate To have overflow rate of 21.84 ms: TSCR2 = 0x03;EE 308 Spring 2011 Write a 1 to the bits of TIOS to make those pins output compare To make Pin 4 an output compare pin: TIOS = TIOS | 0X10; Write to TCTL1 and TCTL2 to choose action to take To have Pin 4 toggle on compare: TCTL1 = (TCTL1 | BIT0) & ~BIT1; Write time you want event to occur to TCn register. To have event occur on Pin 4 when TCNT == 0x0000: TC4 = 0x0000; To have next event occur T cycles after last event, add T to TCn. To have next event occur on Pin 4 500 cycles later: TC4 = TC4 + 500;EE 308 Spring 2011 When TCNT == TCn, the specified action will occur, and flag CFn will be set. To clear the flag, write a 1 to the bit you want to clear (0 to all others) To wait until TCNT == TC4: while ((TFLG1 & BIT4) == 0) ; To clear flag bit for Pin 4: TFLG1 = BIT4; To enable interrupt when compare occurs, set corresponding bit in TIE register To enable interrupt when TCNT == TC4: TIE = TIE | BIT4;EE 308 Spring 2011 Using Output Compare on the MC9S12 1. In the main program: (a) Turn on timer subsystem (TSCR1 reg) (b) Set prescaler (TSCR2 reg) (c) Set up PTx as OC (TIOS reg) (d) Set action on compare (TCTL 1-2 regs, OMx OLx bits) (e) Clear Flag (TFLG1 reg) (f) Enable int (TIE reg) 2. In interrupt service routine (a) Set time for next action to occur (write TCx reg) • For periodic events add time to TCx register (b) Clear flag (TFLG1 reg)EE 308 Spring 2011 #include <hidef.h> /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ #include "vectors12.h" #define PERIOD 3000 #define HALF_PERIOD (PERIOD/2) #define disable() __asm(sei) #define enable() __asm(cli) interrupt void toc2_isr(void); void main(void) { disable(); TSCR1 = 0x80; /* Turn on timer subsystem */ TSCR2 = 0x04; /* Set prescaler to 16 (0.666 us) */ TIOS = TIOS | 0x04; /* Configure PT2 as Output Compare */ TCTL2 = (TCTL2 | 0x10) & ~0x20; /* Set up PT2 to toggle on compare */ TFLG1 = 0x04; /* Clear Channel 2 flag */ /* Set interrupt vector for Timer Channel 2 */ UserTimerCh2 = (unsigned short) &toc2_isr; TIE = TIE | 0x04; /* Enable interrupt on Channel 2 */ enable(); while (1) { __asm(wai); } } interrupt void toc2_isr(void) { TC2 = TC2 + HALF_PERIOD; TFLG1 = 0x04; }EE 308 Spring 2011 Capturing the Time of an External Event • One way to determine the time of an external event is to wait for the event to occur, the read the TCNT register: • For example, to determine the time a signal on Bit 0 of PORTB changes from a high to a low: while ((PORTB & BIT0) != 0) ; /* Wait while Bit 0 high */ time = TCNT; /* Read time after goes low */ • Two problems with this: 1. Cannot do anything else while waiting 2. Do not get exact time because of delays in software • To solve problems use hardware which latches TCNT when event occurs, and generates an interrupt. • Such hardware is built into the MC9S12 — called the Input Capture SystemEE 308 Spring 2011 Measure the time between two events How to measure ∆t? Wait until signal goes low, then measure TCNT while ((PORTB & BIT0) == BIT0) ; start = TCNT; while ((PORTB & BIT0) == BIT1) ; end = TCNT; dt = end - start; Problems: 1) May not get very accurate time 2) Can’t do anything while waiting for signal level to change.EE 308 Spring 2011 Measure the time between two


View Full Document

NMT EE 308 - Ways to implement delays

Documents in this Course
Load more
Download Ways to implement delays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Ways to implement delays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Ways to implement delays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?