ECE/CS 5780/6780: Embedded System DesignChris J. MyersLecture 13: Output CompareChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design1 / 56Variable Duty Cycle Square WaveChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design2 / 56Basic Principles of Output CaptureOutput compare can create square waves, generate pulses, implementtime delays, and execute periodic interrupts.Can also use with input capture to measure frequency.Each output capture module has:An external output pin, OCnA flag bitA force control bit FOCnTwo control bits, OMn, OLnAn interrupt mask bit (arm)A 16-bit output compare registerChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design3 / 56Basic Components of Output CompareChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design4 / 56Basic Principles of Output Compare (cont)Output compare pin can control an external device.Output compare event occurs and sets flag when either:1The 16-bit TCNT matches the 16-bit OC register2The software writes a 1 to the FOC bit.OMn, OLn bits specify effect of event on the output pin.Two or three actions result from a compare event:1The OCn output bit changes2The output compare flag is set.3An interrupt is requested if the mask is 1.Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design5 / 56Applications of Output CompareCan create afixedtime delay.1Read the current 16-bit TCNT2Calculate TCNT+fixed3Set 16-bit output compare register to TCNT+fixed4Clear the output compare flag5Wait for the output compare flag to be setDelay of steps 1 to 4 sets the minimum delay.Maximum delay is 65,536 cycles.Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design6 / 56Output Compare Interface on 6812Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design7 / 56Control Bits and FlagsOutput compares are on port T (i.e., PTT).Set pin to output compare mode by setting bit to 1 in TIOS.Output compare registers are TC0, ..., TC7.Arm interrupts using TIE.Flags are found in TFLG1.Set effect of trigger using TCTL1 and TCTL2.Can force an output compare by setting bit to 1 in CFORC.OMn OLn Effect of when TOCn=TCNT0 0 Does not affect OCn0 1 Toggle OCn1 0 Clear OCn=01 1 Set OCn=1Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design8 / 56Output Compare 7When TC7=TCNT, can set or clear any output compare pins.OC7M register selects pins to be affected by OC7 event.OC7D register specifies value of pin after OC7 event.Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design9 / 56Periodic Interrupt Using Output Capture#define PERIOD 1000unsigned short Time;void OC6_Init(void){asm sei // Make atomicTSCR1 = 0x80;TSCR2 = 0x02; // 1 MHz TCNTTIOS |= 0x40; // activate OC6TIE |= 0x40; // arm OC6TC6 = TCNT+50; // first in 50usTime = 0; // Initializeasm cli } // enable IRQvoid interrupt 14 OC6handler(void){TC6 = TC6+PERIOD; // next in 1 msTFLG1 = 0x40; // acknowledge C6FTime++; }Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design11 / 56Square-Wave GenerationChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design12 / 56Square-Wave Generationunsigned short Period; // in usecvoid ritual(void) {asm sei // make atomicTIOS |= 0x08; // enable OC3DDRT |= 0x08; // PT3 is outputTSCR1 = 0x80; // enableTSCR2 = 0x01; // 500 ns clockTCTL2 = (TCTL2&0x3F)|0x40; // toggleTIE |= 0x08; // Arm output compare 3TFLG1 = 0x08; // Initially clear C3FTC3 = TCNT+50; // First one in 25 usasm cli }void interrupt 11 TC3handler(void){TFLG1 = 0x08; // ack C3FTC3 = TC3+Period;} // calculate NextChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design14 / 56Square-Wave Generation OverheadComponent 6812Process the interrupt (cycles,µs) 9=2.25µsExecute the entire handler (cycles,µs) 20=5µsTotal time (µs) 7.25µsInterrupt every Time to processFreq. Period (cycles) (cycles) Overhead (%)10 Hz 100 ms 100,000 29 0.03100 Hz 10 ms 10,000 29 0.31 kHz 1 ms 1000 29 35 kHz 200 µs 200 29 14.51/P P (µs) P 29 2900/PChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design15 / 56Pulse-Width ModulationChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design16 / 56Pulse-Width Modulated Square-Waveunsigned short High; // Cycles Highunsigned short Low; // Cycles Lowvoid Init(void){asm sei // make atomicTIOS |= 0x08; // enable OC3DDRT |= 0x08; // PT3 is outputTSCR1 = 0x80; // enableTSCR2 = 0x01; // 500 ns clockTIE |= 0x08; // Arm output compare 3TFLG1 = 0x08; // Initially clear C3FTCTL2 = (TCTL2&0x3F)|0x40; // toggleTC3 = TCNT+50; // first right awayasm cli}Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design18 / 56Pulse-Width Modulated Square-Wave (cont)void interrupt 11 TC3handler (void){TFLG1 = 0x08; // ack C3Fif(PTT&0x08){ // PT3 is now highTC3 = TC3+High; // 1 for High cyc}else{ // PT3 is now lowTC3 = TC3+Low; // 0 for Low cycles}}void main(void){High=8000; Low=2000;ritual();while(1);}Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design20 / 56Pulse-Width Modulation OverheadComponent 6812Process the interrupt (cycles) 9Execute the handler (cycles) 27-28Total time T (cycles) 36-37Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design21 / 56Delayed Pulse GenerationChris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design22 / 56Delayed Pulse Generationvoid Pulse(unsigned short Delay,unsigned short Width){asm sei // make atomicTIOS |= 0x08; // enable OC3DDRT |= 0x08; // PT3 is outputTSCR1 = 0x80; // enableTSCR2 = 0x01; // 500 ns clockTC7 = TCNT+Delay;TC3 = TC7+Width;OC7M = 0x08; // connect OC7 to PT3OC7D = 0x08; // PT3=1 when TC7=TCNTTCTL2=(TCTL2&0x3F)|0x80; // PT3=0 when TC3=TCNTTFLG1 = 0x08; // Clear C3FTIE |= 0x08; // Arm C3Fasm cli}Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design24 / 56Delayed Pulse Generation (cont)void interrupt 11 TC3handler(void){OC7M = 0; // disconnect OC7 from PT3OC7D = 0;TCTL2 &=~0xC0; // disable OC3TIE &= ~0x08; // disarm C3F}Chris J. Myers (Lecture 13: Output Compare)ECE/CS 5780/6780: Embedded System Design26 / 56Frequency MeasurementDirect measurement of frequency involves counting input pulses for afixed amount of time.Can use input capture to count
View Full Document