EE 308 Feb. 25, 2002The HC12 Output Compare Function;Want event to happen at a certain timeWant to produce pulse pulse with width TPA0Twhile (TCNT != 0x0000) ;PORTA = PORTA | 0x01;while (TCNT != T) ;PORTA = PORTA & ~0x01;Wait until TCNT == 0x0000, then bring PA0 highWait until TCNT == T, then bring PA0 low1EE 308 Feb. 25, 2002Want event to happen at a certain timeWant to produce pulse pulse with width TPA0Twhile (TCNT != 0x0000) ;PORTA = PORTA | 0x01;while (TCNT != T) ;PORTA = PORTA & ~0x01;Wait until TCNT == 0x0000, then bring PA0 highWait until TCNT == T, then bring PA0 lowProblems: 1) May miss TCNT == 0x0000 or TCNT == T2) Time not exact −− software delays3) Cannot do anything else while waiting2EE 308 Feb. 25, 2002Want 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 Feb. 25, 2002D QVCCWriteReadTFLG1TFLG1OCxFRegisterTCNT16 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)PORT TTCxInterruptTMSK1I BitCCRCxICxFPTx PinM−CLKWrite time you want eventto happen to TCx RegisterTell HC12 what typeof event you want4EE 308 Feb. 25, 2002The HC12 Output Compare FunctionThe HC12 allows you to force an event to happen on any of the eight PORTTpinsAn external event is a rising edge, a falling edge, or a toggleTo use the Output Compare Function:– Enable the timer subsystem (set TEN bit of TSCR)– Set the prescaler– Tell the HC12 that you want to use Bit x of PORTT for output compare– Tell the HC12 what you want to do on Bit x of PORTT (generate risingedge, falling edge, or toggle)– Tell the HC12 what time you want the event to occur– Tell the HC12 if you want an interrupt to be generated when the event isforced to occur5EE 308 Feb. 25, 2002TMSK2 = 0x03;Write a 1 to Bit 7 of TSCR to turn on timerTEN TSWAI TSBCK TFFCA 0x0086 TSCRTo turn on the timer subsystem: TSCR = 0x80;Set the prescaler in TMSK2TOI PUPT RDPT TCRE PR2 PR1 PR0 0x008D TMSK2PR2 PR1 PR00.1250.2500.5001.0002.0004.00000 11 01 1000 11 01 1000001111Period Overflow( s) (ms)µ8.19216.38432.76865.536262.144131.072−−−−−−−−−−−−To have overflow rate of 65.536 ms:−−−−−−−−−−−−Make sure the overflow time is greater than the time difference you want to generate06EE 308 Feb. 25, 2002IOS7 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;0x0088 TCTL10x0089 TCTL2Write 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:TCTL1 = (TCTL1 | 0x01) & ~0x02;DisconnectedToggleClearSetConfigurationCF7 CF6 CF4 CF3 CF2 CF0CF5 CF1 0x008E TFLG1C4I C2IC5Ibit in TMSK1 registerC7I C6I C3I C1I C0I 0x008C TMSK1To clear the flag, write a 1 to the bit you want to clear (0 to all others)To wait until TCNT == TC4: while ((TFLG1 & 0x10) == 0) ;To enable interrupt when compare occurs, set correspondingTo enable interrupt when TCNT == TC4: TMSK1 = TMSK1 | 0x10;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;When TCNT == TCn, the specified action will occur, and flag CFn will be set.To clear flag bit for Pin 4: TFLG1 = 0x10;7EE 308 Feb. 25, 2002USING OUTPUT COMPARE ON THE HC121. In the main program:(a) Turn on timer subsystem (TSCR reg)(b) Set prescaler (TMSK2 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 (TMSK1 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 Feb. 25, 2002/** Program to generate square wave on PT2* Frequency of square wave is 500 Hz* Period of square wave is 2 ms* Set prescale to give 1 us cycle* 2 ms is 2,000 cycles of 1 us/cycle**/#include "hc12b32.h"#define PERIOD 2000#define HALF_PERIOD (PERIOD/2)#define TRUE 1main(){TSCR = 0x80; /* Turn on timer subsystem */TMSK2 = 0x03; /* Set prescaler to 8 */TIOS = TIOS | 0x04; /* Configure PT2 as Output Compare */TCTL2 = (TCTL2 | 0x10) & ˜0x20; /* Set up PT2 to toggle on compare */TFLG2 = 0x04; /* Clear flag and enable interrupt on C2 */TMSK1 = TMSK1 | 0x04;enable();while (TRUE){_asm("wai");}}@interrupt void toc2_isr(void){TC2 = TC2 + HALF_PERIOD;TFLG1 = 0x04;}9EE 308 Feb. 25, 2002/* INTERRUPT VECTORS TABLE 68HC12*/void toc2_isr(); /* character receive handler */void (* const _vectab[])() = { /* 0x0B10 */0, /* BDLC */0, /* ATD */0, /* reserved */0, /* SCI0 */0, /* SPI */0, /* Pulse acc input */0, /* Pulse acc overf */0, /* Timer overf */0, /* Timer channel 7 */0, /* Timer channel 6 */0, /* Timer channel 5 */0, /* Timer channel 4 */0, /* Timer channel 3 */toc2_isr, /* Timer channel 2 */0, /* Timer channel 1 */0, /* Timer channel 0 */0, /* Real time */0, /* IRQ */0, /* XIRQ */0, /* SWI */0, /* illegal */0, /* cop fail */0, /* cop clock fail */(void *)0xff80, /* RESET */};10EE 308 Feb. 25, 2002Pulse Width ModulationOften want to control something by adjusting the percentage of time the ob-ject is turned onFor example,– A DC motor — the higher the percentage, the faster the motor goes– A light – the higher the percentage, the brighter the light– A heater – the higher the percentage, the more heat outputCan use Output Compare to generate a PWM signalBecause PWM is used so often the HC12 has a built-in PWM systemThe PWM system on the HC12 is very flexible– It allows you to set a wide range of PWM frequencies– It allows you to generate up to 4 separate PWM signals, each with adifferent frequency– It allows you to generate 8-bit PWM signals (with 0.5% accuracy) or16-bit PWM signals (with 0.002% accuracy)– It allows you to select high polarity or low polarity for the PWM signal– It allows you to use left-aligned or center-aligned PWM signalsBecause the HC12 PWM systes is so flexible, it is fairly complicated to pro-gramTo simplify the discussion we will
View Full Document