U of U CS 5780 - Lecture 14 - Output Compare

Unformatted text preview:

CS/ECE 5780/6780:Embedded System DesignJohn RegehrLecture 14: Output CompareBasic Ideas of Output CompareIOutput compare can create square waves, generate pulses,implement time delays, and execute periodic interrupts.ICan also u se with inp ut capture to measure frequency.IEach output compare module has:IAn external output pin, OCnIA flag bitIA force control bit FOCnITwo control bits, OMn, OLnIAn interrupt mask bitIA 16-bit output compare registerBasic Ideas of Output Compare (cont)IOutput compare pin can control an external device.IOutput compare event occurs and sets flag when either:1. The 16-bit TCNT matches the 16-bit OC register2. The software writes a 1 to the FOC bit.IOMn, OLn bits specify effect of event on the output pin.ITwo or three actions result from a compare event:1. The OCn output bit changes2. The output compare flag is set.3. An interrupt is requested if the mask is 1.Basic Components of Output CompareControl Bits and FlagsIOutput compares are on port T (i.e., PTT).ISet pin to output compare mode by setting bit to 1 in TIOS.IOutput compare registers are TC0, . . . , TC7.IArm interrupts using TIE.IFlags are found in TFLG1.ISet effect of trigger using TCTL1 and TCTL2.ICan 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=1Applications of Output CompareICan create a fixed time delay.1. Read the current 16-bit TCNT2. Calculate TCNT+fixed3. Set 16-bit output compare register to TCNT+fixed4. Clear the output compare flag5. Wait for the output compare flag to be setIDelay of steps 1 to 4 sets the minimum delay.IMaximum delay is 65,536 cycles.Periodic Interrupt Using Output Compare#define PERIOD 1000unsigned short Time;void OC6_Init(void){asm sei // Make atomicTSCR1 = 0x80; // Turn on timerTSCR2 = 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++; }Pulse-Width ModulationPulse-Width Modulated Square-Waveunsigned short High; // Cycles Highunsigned short Low; // Cycles Lowvoid Init(void){asm sei // make atomicTSCR1 = 0x80; // Turn on timerTSCR2 = 0x01; // 500 ns clockTIOS |= 0x08; // enable OC3DDRT |= 0x08; // PT3 is outputTIE |= 0x08; // Arm output compare 3TFLG1 = 0x08; // Initially clear C3FTCTL2 = (TCTL2&0x3F)|0x40; // toggleTC3 = TCNT+50; // first right awayasm cli}Pulse-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);}Pulse-Width Modulation OverheadComponent 6812Process the interrupt (cycles) 9Execute the handler (cycles) 27-28Total time T (cycles) 36-37Frequency MeasurementIDirect measurement of frequency involves counting inputpulses for a fixed amount of time.ICan use input capture to count pulses, and output compare tocreate a fixed time interval.IInput Capture handler increments Counter.IOutput compare handler calculates frequency:f =Counterfixed timeIThe frequency resolution is:f =1fixed timeFrequency MeasurementFrequency Measurement#define Rate 20000 // 10 msvoid Init(void) {asm sei // make atomicTSCR1 = 0x80; // Turn on timerTSCR2 = 0x01; // 500 ns clockTIOS |= 0x20; // enable OC5TIE |= 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}Frequency 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}Pulse-Width Modulation (PWM) ConfigurationIDedicated hardware can create PWM signals on port P withno overhead.IMODRR register can connect PWM system to port T pins.IPWME register us ed to enable PWM channels.IEither three 16-bit c hannel s or up to six 8-bit channels.ICON01 bit connects two 8-bit channels to form one 16-bitchannel (similarly for CON23 and CON45).PWM ConfigurationIOutput is high number of counts in correspondingPWMDTY register, and total counts in a cycle in thecorrespondi ng PWMPER register.PPOLx=1PPOLx=0Clock ChoiceIMany possible choices for the clock.IA and B c locks configured by the PWMPRCLK register as adivided down version of the E clock between E and E/128.ISA clock is the A clock divided by two time s value inPWMSCLA register.ISB clock is the B clock divided by two times value inPWMSCLB register.IChannels 0, 1, 4, and 5 can use either A or SA clock whilechannels 2 and 3 use either the B or SB clock.8-bit PWM Output (10ms Period)void PWM_Init(void){MODRR |= 0x01; // PT0 with PWMPWME |= 0x01; // enable channel 0PWMPOL |= 0x01; // PT0 high then lowPWMCLK |= 0x01; // Clock SAPWMPRCLK = (PWMPRCLK&0xF8)|0x04; // A=E/16PWMSCLA = 5; // SA=A/10, 0.25*160=40usPWMPER0 = 250; // 10ms periodPWMDTY0 = 0; // initially off}void PWM_Duty0(unsigned char duty){PWMDTY0 = duty; // 0 to 250}8-bit PWM Output (1s Period)void PWM_Init(void){MODRR |= 0x08; // PT3 with PWMPWME |= 0x08; // enable channel 3PWMPOL |= 0x08; // PT3 high then lowPWMCLK &=~0x08; // Clock BPWMCTL |= 0x20; // Concatenate 2+3PWMPRCLK = (PWMPRCLK&0x8F)|0x60; // B=E/64PWMPER23 = 62500; // 1s periodPWMDTY23 = 0; // initially off}// Set the duty cycle on PT3 outputvoid PWM_Duty(unsigned short duty){PWMDTY23 = duty; // 0 to 62500}Output Compare SummaryIOutput compare is a broadly useful techniqu e for taking apredefined action at a precise timeIDedicated PWM modules have even lower overheadIA pulse accumulator is a related device that counts


View Full Document

U of U CS 5780 - Lecture 14 - Output Compare

Documents in this Course
Lab 1

Lab 1

5 pages

FIFOs

FIFOs

10 pages

FIFOs

FIFOs

5 pages

FIFO’s

FIFO’s

12 pages

MCU Ports

MCU Ports

12 pages

Serial IO

Serial IO

26 pages

Load more
Download Lecture 14 - Output Compare
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 Lecture 14 - Output Compare 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 Lecture 14 - Output Compare 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?