DOC PREVIEW
NMT EE 308 - The MC9S12 Output Compare Function

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 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 12 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 12 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 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

EE 308 Spring 2009The MC9S12 Output Compare Function;PORTA = PORTA & ~BIT0;while (TCNT != T) ;PORTA = PORTA | BIT0;Want event to happen at a certain timeWant to produce pulse pulse with width TPA0Twhile (TCNT != 0x0000) ;Wait until TCNT == 0x0000, then bring PA0 highWait until TCNT == T, then bring PA0 low1EE 308 Spring 2009Want to produce pulse pulse with width TPA0Twhile (TCNT != 0x0000) ;while (TCNT != T) ;PORTA = PORTA | BIT0;PORTA = PORTA & ~BIT0;Want event to happen at a certain time3) Cannot do anything else while waitingWait until TCNT == 0x0000, then bring PA0 highWait until TCNT == T, then bring PA0 low2) Time not exact −− software delaysProblems: 1) May miss TCNT == 0x0000 or TCNT == T2EE 308 Spring 2009Want event to happen at a certain timeWant to produce pulse pulse with width TTTCNT0x0000CMPCMPTPT0PT0CLKWhen TCNT == 0x0000, the output goes highWhen TCNT == T, the output goes low==S QRNow pulse is exaclty T cycles long3EE 308 Spring 2009D QVCCWriteReadTFLG1TFLG1RegisterTCNT16 Bit CounterCOMPARATOR16 BitD QOUTPUT COMPARE PORT T 0−7To use Output Compare, you must set IOSx to 1 in TIOS11 => VCC10 => GND01 => Q00 => Not UsedOMx OLx (TCTL 1:0)TCxInterruptI BitCCRCxICxFPTx PinWrite time you want eventto happen to TCx RegisterTell HC12 what typeof event you wantTIECxFTime ClockSet rate with prescalerEnable with TENPTT4EE 308 Spring 2009The MC9S12 Output Compare Function• The MC9S12 allows you to force an event to happen on any of the eight PTT 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 MC9S12 that you want to use Bit x of PTT for output compare– Tell the MC9S12 what you want to do on Bit x of PTT (generate rising edge, fallingedge, or toggle)– Tell the MC9S12 what time you want the event to occur– Tell the MC9S12 if you want an interrupt to be generated when the event is forcedto occur• There are some more complicated features of the output compare subsystem which areactivated using registers CFORC, OC7M, OC7D and• Writing a 1 to the corresponding bit of CFORC forces an output compare event to occur,the s ame 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 func-tions (Section 8.6.4 of Huang).• Using TTOV allows you to toggle an output compare pin when TCNT overflows. Thisallows you to use the output compare system to generate pulse width modulated signals.• We will not discuss these advanced features in this class.5EE 308 Spring 2009TSWAI TSBCK TFFCATOI TCRE PR2 PR1 PR0PR2 PR1 PR000 11 01 1000 11 01 1000001111Period Overflow( s) (ms)µTEN0Write a 1 to Bit 7 of TSCR1 to turn on timerTo turn on the timer subsystem: TSCR1 = 0x80;0x0046 TSCR1Set the prescaler in TSCR20x004D TSCR2 0 00.04160.08330.16670.33330.66671.33332.66675.33332.73 5.4610.9221.84 43.69 86.38174.76349.53To have overflow rate of 21.84 ms:TSCR2 = 0x03;Make sure the overflow time is greater than the width of the pulse you want to generate6EE 308 Spring 2009TCTL1 = (TCTL1 | BIT0) & ~BIT1;IOS7 IOS6 IOS5 IOS4 IOS3 IOS2 IOS1 IOS0 0x0080 TIOS0 00 11 01 1Write a 1 to the bits of TIOS to make those pins output compareTo make Pin 4 an output compare pin: TIOS = TIOS | 0X10;Write to TCTL1 and TCTL2 to choose action to takeOM7 OL7 OM6 OL6 OM5 OL5 OM4 OL4OM3 OL3 OM2 OL2 OM1 OL1 OM0 OL0OMn OLn To have Pin 4 toggle on compare:DisconnectedToggleClearSetConfiguration0x0048 TCTL10x0049 TCTL2To enable interrupt when TCNT == TC4: TIE = TIE | BIT4;C4I C2IC5IC7I C6I C3I C1I C0ITo clear the flag, write a 1 to the bit you want to clear (0 to all others)To enable interrupt when compare occurs, set correspondingWrite 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;When TCNT == TCn, the specified action will occur, and flag CFn will be set.CF7 CF6 CF4 CF3 CF2 CF0CF5 CF1 0x004E TFLG1bit in TIE register0x004C TIETo wait until TCNT == TC4: while ((TFLG1 & BIT4) == 0) ;To clear flag bit for Pin 4: TFLG1 = BIT4;7EE 308 Spring 2009USING OUTPUT COMPARE ON THE MC9S1 21. In the main program:(a) Turn on timer subsystem (TSCR1 reg)(b) Set presc aler (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)8EE 308 Spring 2009Setting and Clearing Bits in the Timer Subsystem• Registers in the timer subsystem control multiple timer channels.– Usually, you want to use ANDS and ORS to change only that channel you areworking on.– For example, to make Channel 2 an output compare, and set it to toggle oncompare, do this:TIOS = TIOS | BIT2; /* Configure PT2 as Output Compare */TCTL2 = (TCTL2 | BIT4) & ~BIT5; /* Set up PT2 to toggle on compare */– Do not do this:TIOS = BIT2; /* Configure PT2 as Output Compare */TCTL2 = BIT4); /* Set up PT2 to toggle on compare */This would set up Channel 2 as an output compare, toggle on successful compare.However, it will force all the other channels to input capture – this may not bewhat you want to do.• To clear a flag bit, do not use ORs!– To clear Timer Channel 2 flag, do the following:TFLG1 = BIT2;This will cle ar Timer Channel 2 flag, and leave all other flags unaffected.– Do not do this:TFLG1 = TFLG1 | BIT2; /* DO NOT DO THIS */This will clear Timer Channel 2 flag, but will also clear any other flag which is set.Suppose, for example, Timer Channel 2 and Timer Channel 3 flags are both setat the same time, so TFLG1 register is 0x0C. You want to deal the Timer Channel2 first and Timer Channel 3 afterwards.The command:TFLG1 = TFLG1 | BIT2; /* DO NOT DO THIS */will read TFLG1, which will return an 0x0C. ORing that with a 0x04 (BIT2) willresult in an 0x0C. Writing that back to TFLG1 will clear Timer Channel 2 flag andTimer Channel 3 flag. Now Timer Channel 3 flag is cleared, so you will never dealwith the event which set Timer Channel 3 flag.9EE 308 Spring 2009/** Program to generate square wave on PT2* Frequency of square wave is 500 Hz*


View Full Document

NMT EE 308 - The MC9S12 Output Compare Function

Documents in this Course
Load more
Download The MC9S12 Output Compare Function
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 The MC9S12 Output Compare Function 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 The MC9S12 Output Compare Function 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?