ECE/CS 5780/6780: Embedded System DesignScott R. LittleLecture 14: Output CompareScott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 1 / 22AdministriviaTraveling next week.Schedule change for the next exam due to technical open house.New date will be April 1.No labs will be held during the week of March 24.Lab 9 will be due the week of March 31.Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 2 / 22Basic Principles of Output CaptureOutput compare can create square waves, generate pulses,implement time 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 registerScott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 3 / 22Basic Principles of Output Compare (cont)Output compare pin can control an external device.Output compare event occ urs 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.Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 4 / 22Control 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=1Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 5 / 22Applications of Output CompareCan create a fixed time 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.Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 6 / 22Periodic 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++; }Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 7 / 22Pulse-Width ModulationScott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 8 / 22Pulse-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}Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 9 / 22Pulse-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;Init();while(1);}Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 10 / 22Pulse-Width Modulation OverheadComponent 6812Process the interrupt (cycles) 9Execute the handler (cycles) 27-28Total time T (cycles) 36-37Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 11 / 22Frequency MeasurementDirect measurement of frequency involves counting input pulsesfor a fixed amount of time.Can use input capture to count pulses, and output capture tocreate a fixed time interval.Input Capture handler increments Counter.Output compare handler calculates frequency:f =Counterfixed timeThe frequency resolution is:f =1fixed timeScott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 12 / 22Frequency MeasurementScott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 13 / 22Frequency Measurement#define Rate 20000 // 10 msvoid Init(void) {asm sei // make atomicTIOS |= 0x20; // enable OC5TSCR1 = 0x80; // enableTSCR2 = 0x01; // 500 ns clockTIE |= 0x22; // Arm OC5 and IC1TC5 = TCNT+Rate; // First in 10 msTCTL4 = (TCTL4&0xF3)|0x04; /* C1F set on rising edges */Count = 0; // Set up for firstDone = 0; // Set on measurementsTFLG1 = 0x22; // clear C5F, C1Fasm cli}Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 14 / 22Frequency Measurement (cont)void interrupt 9 TC1handler(void){Count++; // number of rising edgesTFLG1 = 0x02; // ack, clear C1F}void interrupt 13 TC5handler(void){TFLG1= 0x20; // AcknowledgeTC5 = TC5+Rate; // every 10 msFreq = Count; // 100 Hz unitsDone = 0xff;Count = 0; // Setup for next}Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 15 / 22Conversion Between Frequency and PeriodCould measure frequency from period measurement:f =1pIf range of period measurement is 36µs to 32ms with resolutionof 500ns, frequency range is 31 to 27,778Hz.f =1p·1500ns=2000000pResolution relationship is not as obvious:∆f =1(1/f ) − ∆p− f =1(1/f ) − 500ns− fScott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 16 / 22Relationship Between Frequency and PeriodFrequency (Hz) Period (µs) ∆f (Hz)31,250 32 50020,000 50 20010,000 100 505,000 200 132,000 500 21,000 1,000 0.5500 2,000 0.13200 5,000 0.02100 10,000 0.00550 20,000 0.00131.25 32,000 0.0005Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 17 / 22Pulse-Width Modulation (PWM) ConfigurationDedicated hardware can create PWM signals on port P with nooverhead.MODRR register can connect PWM system to port T pins.PWME register used to enable PWM channels.Either three 16-bit channels or up to six 8-bit channels.CON01 bit connects two 8-bit channels to form one 16-bitchannel (similarly for CON23 and CON45).Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 18 / 22PWM ConfigurationOutput is high number of counts in corresponding PWMDTYregister, and total counts in a cycle in the correspondingPWMPER register.PPOLx=1PPOLx=0Scott R. Little (Lecture 14: Output Compare) ECE/CS 5780/6780 19 / 22Clock ChoiceMany
View Full Document