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 2010Ways to implement delays• Software Delayvoid 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 InterruptOnly fixed delays1EE 308 Spring 2010The 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 low2EE 308 Spring 2010Want 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 == T3EE 308 Spring 2010Want 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 long4EE 308 Spring 2010D 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 TENPTT5EE 308 Spring 2010The 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 TTOV.– Writing a 1 to the corresponding bit of CFORC forces an output compare event tooccur, the same as if a successful comparison has taken place (Section 8.6.5 ofHuang).– Using OC7M and OC7D allow Timer Channel 7 to control multiple output comparefunctions (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 modulatedsignals.– We will not discuss these advanced features in this class.6EE 308 Spring 2010TSWAI 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 generate7EE 308 Spring 2010TCTL1 = (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;8EE 308 Spring 2010USING OUTPUT COMPARE ON THE MC9S121. 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)OMx OLx Action0 0 Disconnected0 1 Toggle1 0 Clear1 1 Set(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)9EE 308 Spring 2010#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;}10EE 308 Spring 2010Capturing 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 toa 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 waiting2. Do not get exact time because of delays in software• To solve problems use hardware which latches TCNT when event occurs, and generatesan


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?