Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Digital Co n t r ol Microsys te m s Part 5: Pulse Width Modulation - PWM

PWM It's p r e t ty m u c h co m m o n s e n s e t h a t if you t u r n a m o t o r o n a n d off fas t e no ug h, s ay o n e r u n ni ng a fa n, t h e fa n' s s pee d is going t o be n eit her f ully off n o r f ully o n. It wo ul d r u n a t a re d uce d s pee d, a n d t h a t s p ee d wo ul d r eflect t h e aver age a m o u n t of ti m e t h e switc h was o n. This is t h e e s se nce of PWM, Pulse Widt h Mo d ulatio n. PWM is a way of get ting a n alog (contin u o u sly variable) co n t r ol over s o m e ty pe s of d evices. Moto r s, inca n d e sce n t ligh t s, a n d LED's a re t h r ee co m m o nly PWM'ed d evices.

A PWM line c ontrolling a m ot or's s p e e d. The s had ed area repre s e nt s on tim e. A is 9 0% o n, B is 5 0% o n, and C is 1 0% o n. So me MCU's like t he PIC12F683 h ave b uilt - in circ uit ry for d riving a n o u t p u t line a s a PWM co n t r oller. When selecting a PIC for yo ur p r oject, look o n t h e d a t a s h ee t fo r a pi n n a m e d CCP, w hich s t a n d s for Ca p t u r e Co m p a r e PWM. It is p o s sible t o d o PWM wit h o u t h a r dw a re s u p p o r t, b u t it's m u c h les s wor k t o u s e t he CCS co m piler libraries w hich ar e writ te n for t h e b uilt - in p e ri p he r al. PWMing m ul ti ple lines si m ult a neo u sly wit h o u t h a r dw a re s u p p o r t ca n r e q uire sc he d uling t he r es t of yo ur MCU's co d e a r o u n d t h e PWM a n d ca n be a challenge. The 1 2F68 3 h a s o ne CCP pi n. PWM is b a se d in ti mi ng, a n d in co m p u te r s ti mi ng is ba se d in co u n ti ng. All micr o p r oces s o r s re q uire a n accu r a te o scillator w hic h p r o d uce s a s t re a m of always ide n tical p ulse s. This is t h e syste m clock. In o u r u s e of t h e PIC12F683, we're t a ki ng

Digital Co n t r ol Microsys te m s Part 5: Pulse Width Modulation - PWM a dva n t age of t he goo d e n o ug h b uilt - in oscillato r w hic h p r o d uce s a n 8 Mega her t z (8 millio n p ul se s p e r s eco n d) clock. This s q u a r e wave is t h e m a s t e r d r u m be at, t h e m e t r o n o m e, w hich s e q u e nce s al m o s t all of t he MCU's inner wor kings. The PIC's b uilt - in ti m e r s for ins t a nce, f u nctio n by co u n ti ng clock p ulse s a n d t h e p ulse s of o t h e r co u n te r s w hich ar e co u n ting clock p ulse s. The interco n nectio n of t h e se co u n t er s is in p a r t ar r a nge d by t h e s et u p co de m e n tio ne d in t he s o u rce co de below. The PWM diagr a m a bove s h ow s t h r ee d u ty cycles. The d u ty cycle is t he p e rce n t o n ti m e. Fully o n is 1 0 0%, a n d f ully off is 0% d u ty cycle. The begin ni ng of a n on p ulse is calle d its rising edge. The ti m e be twee n rising e dge s is t he s a m e for all t h r ee of t h e wavefor m s. The ti me fro m rising e dge t o rising e dge in a wavefor m is t he wavefor m' s period. Perio d is t he recipr ocal of freq ue ncy. If t h e ti m e fr o m rising e dge t o rising e dge in t h e s a m e wavefor m is 1 m illiseco n d, t he fre q ue ncy is 1 kilo he r t z. This is t h e PWM freq u e ncy u s e d in a n alog2.C, below. For co m p a ris o n, t he p e rio d of t h e MCU's b uilt - in clock is 1 / 8 0 0 0 0 0 0 o r 0.000 0 0 0 1 2 5 s eco n d s (125 n a n o s eco n d s). p e rio d = 1 / fre q ue ncy fre q ue ncy = 1 / p e rio d PWM d o e s n o t (neces s a rily) cha nge t h e fre q ue ncy of t he o u t p u t. If yo u s e t u p t h e PIC fo r a low PWM fre q ue ncy w hich yo u ca n h ea r w hile t he MCU co n t r ols a m o t o r's s p ee d, yo u will h e a r t h e ti m b r e of t he m o t o r's vibra tio n beco m e h a r s h e r wit h increa si ng d u ty cycle (increa sing p ower), b u t t h e pi tc h will n o t cha nge. This s o u n d is n o t d u e t o t h e r o ta tio n of t h e m o t o r' s ar m a t u r e, r a t h er t o m icr o sco pic vibr atio n s of t h e a r m a t u r e as it r eac ts t o t he m o t o r m ag ne t s. It's si milar t o a p o o r q u ality lo u d s p ea ker in t his rega r d. Gener ally, if co n ditio n s allow, m o s t p e o ple p r efer t o ch o o s e a PWM freq ue ncy high e no ug h t h a t it wo n' t be a u dible (above 2 0kH z). On t o case s. Th ree lines of s e t u p co de m u s t be p r e se n t t o activate t he PWM a s a n o u t p u t: We're u sing t h e inte r n al o scillator
#use delay(clock=8000000)

Desig n a te t he Ca p t u r e a n d Co m p a r e pi n a s PWM o u t p u t
setup_ccp1(CCP_PWM);

Sets t h e fre q ue ncy of t he PWM


setup_timer_2(T2_DIV_BY_16, 127, 1);

Using t h e PWM: t h e value of i beco m e s t h e PWM value


set_pwm1_duty(i);

Digital Co n t r ol Microsys te m s Part 5: Pulse Width Modulation - PWM

//analog2.c -

test of A2D with PWM

// read analog port // print value of analog port 1 (PIC12F683 pin 6) // set pwm value on CCP pin (PIC12F683 pin 5) #include <12F683.h> #device ADC=8 #include <STDLIB.h> #fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN #use delay(clock=8000000) #use RS232(Baud=9600,Xmit=PIN_A0) //PIN_A0 (PIC12F683 pin 7) sends to LCDebug //sets up the putc() and printf() functions int i; void main(void){ setup_adc_ports(sAN1); setup_adc(ADC_CLOCK_INTERNAL);

//make analog port 1 active as input //analog clock setup

setup_ccp1(CCP_PWM); //Enable Pulse Width Modulator Mode setup_timer_2(T2_DIV_BY_16, 127, 1); putc(12); while(1){ set_adc_channel(1); //select the channel, set A2D to pin 6 (GP1 as AN1) delay_us(100); i = read_adc(); printf("%3u\r",i); set_pwm1_duty(i); } } //delay 100 microseconds for A2D to stabilize //take the reading pin 6 //print a 3 digit number and Carriage Return //home and clear the display

We're u si ng t he PWM in 8 - bit m o d e. This gives u s 2 5 6 s t e p s of co n t r ol fro m f ull off (0) t o f ull o n (255). The PIC a n d t h e CCS co m piler ca n also PWM wit h 1 0 - bit re s ol u tio n, w hich wo ul d give 1 0 2 4 s te p s of co n t r ol fro m f ull off (0) t o f ull o n (102 3). The circuit for a n alog2.c b uild s o n w ha t h a s co m e before it. The CCP pi n (calle d CCP1 in t h e d a t a s h eet) is PIC12F683 pi n 5. We've di m m e d a n LED co n necte d t o t h a t pi n a n d n o w we'll u s e t he s a m e PWM co de t o co n t r ol a m o t o r s pee d. What we n ee d t o a d d t o m a k e t h a t p o s sible ar e a tra nsistor a n d a diode. The ty pe of dio d e is a 1N4 0 0 7 r ectifier, a n d t h e ty pe of t r a n sis t or is a TIP122 d a rlingt o n. First, h e r e's t h e sc h e m a tic for t he circ uit.

Digital Co n t r ol Microsys te m s Part 5: Pulse Width Modulation - PWM

The re ar e o nly a very few ways t o h o ok a loa d t o a m icr oco n t r oller. With t h e exce p tio n of ligh ting a n LED, t h e r e will always h ave t o be s o m e kin d of interface d evice be twee n t h e MCU a n d t h e act ua t o r o r loa d. This is beca u s e t h e Inp u t / O u t p u t (I /O) pi n s we've bee n con n ec ting t o ca n o nly s u p ply t h e m o s t feeble cu r re n t, 1 0 t o 2 0 mA (millia m p s, t h o u s a n d t h s of a n a m p) m axi m u m, w hile s m all t o m e diu m m o t o r s n ee d h u n d r e d s of millia m p s t o s everal a m p s (in ar t m a ki ng, 5 a m p s is o n t he big e n d). The m o s t b a sic way t o get a MCU o u t p u t u p t o a u s a ble a m o u n t of p ower is t o a m plify t h e o u t p u t wit h a t r a n sis t o r. Here is a s m all p r ogr a m w hich s h ow s t h e differe nce in co ding PWM wit h a 1 0 - bit o u t p u t ver s u s a n 8 - bit o u t p u t. The 1 0 - bit ver sio n gives 1 0 2 4 s te p s of o u t p u t p o wer a s t h e co n t r ol variable (long i) is cha nge d fro m 0 t o 1 0 2 3 a n d b ack. The 8 - bit ver sio n gives 2 5 6 s te p s of o u t p u t p ower a s t he PWM co n t r ol variable (int j) is cha nge d fr o m 0 t o 2 5 5 a n d back. The 1 0 bit ver sio n is s m o o t he r t h a n t he 8 - bit ver sio n beca u se it t a ke fo u r ti me s a s m a ny s t e p s of cha nge in t h e o u t p u t t o s p a n t he s a m e 0 t o 1 0 0% r a nge of d u ty cycle. It also will cha nge m o r e slowly if t he ti me be twee n u p d a t e s t o t h e PWM co n t r ol variable ar e t he s a m e a s t h e 8 - bit ver sio n. Nor m ally t h e extr a s m o o t h n e s s of t h e 1 0 - bit variable is n't t h e be s t r eas o n for u sing t he 1 0 - bit m o d e. Mostly it's a h el p in s e t ti ng t he s p a n of t he o u t p u t r a nge wit h o u t losing t oo m u c h a dj u s t a bility beca u s e of cho p pi ng off t h e u n d e sir able h ea d a n d t ail p a r t s of t h e s p ee d r a nge.
//HBridge1.CH-Bridgeandrelayreverser/speedcontrolwithPIC12F6838-pinmcu //ShowsPWMmotorcontrol.PotonA-to-Dcontrolsaccelerationrateasmotorgoesfrom

Digital Co n t r ol Microsys te m s Part 5: Pulse Width Modulation - PWM


//zerospeedtofullspeed,reverses,andrepeats.OutputlinesarePWMdutycycle //anddirection.Outputscanbeconnectedtodrivertransistorandrelayinterface(cheap, //fault-tolerant)orLMD18200TH-Bridge(small,modern). // //Pressingthepushbuttoncauses3tripsthroughthewhileloop // //LMD18200TH-Bridgemotordriver3Amps55Voltsmax //LMD18200pins //pin1 bootstrapcap.01uFtopin2 //pin2 Motorlead1;Bootstrapcapalsotopin1 //pin3 direction-toPIC12F683pin3 //pin4 brakeinput-ground //pin5 PWMinput-toCCP1pinPIC12F883pin5 //pin6* +VforchipANDmotor12-24VDC(NOT5volts) //pin7 ground //pin8 currentsense-notconnected //pin9 thermalflag-notconnected //pin10 Motorlead2;Bootstrapcapalsotopin11 //pin11 bootstrapcap.01uFtopin10 //*add220uFand1uFcapstogroundatthispin //12F683PINS //PIN2(PIN_A5)--1kpullupandpushbuttontoground //PIN3(PIN_A4)--direction //PIN5(PIN_A2)--PWMout //PIN6(PIN_A1)--AN1analoginput #include<12F683.h> #deviceADC=8 #include<STDLIB.h> #fusesINTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN #usedelay(clock=8000000) longi;//variabletohold10-bitPWMvalue intj;//variabletohold8-bitPWMvalue intk;//scratchpadvariable intanalog_val; shortdirection=0; voidmain(void){ setup_adc_ports(sAN1); //setupPIC12F683pin6asanalog setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(1); //ADCchannelfor10kvoltagedividerpot setup_ccp1(CCP_PWM); //EnablePulseWidthModulatorMode

/*Cycletimewillbe 1/clock*4*t2div*pr2. foran8MHzclock, (1/8000000)*4*255*1=.0001275sec=127.5microsec=7843Hz*/

Digital Co n t r ol Microsys te m s Part 5: Pulse Width Modulation - PWM


setup_timer_2(T2_DIV_BY_1,255,1); //thiswhileloopshows10-bitmode.VariablecontainingPWMvaluemustbealong while(1){ while(input(PIN_A5));//waitforbuttonpress for(k=0;k<3;k++){ output_bit(PIN_A4,direction); for(i=0;i<1024;i++){ //callingset_pwm1_duty()withalongcausescompilertouse10-bitmode set_pwm1_duty(i); analog_val=read_adc();//takethereadingpin6 delay_ms(analog_val); } for(i=1023;i>0;i--){ set_pwm1_duty(i); analog_val=read_adc(); delay_ms(analog_val); } direction=(~direction);

//thiswhileloopshows8-bitmode.VariablecontainingPWMvaluemustbeanint /* while(1){ while(input(PIN_A5));//waitforbuttonpress for(k=0;k<3;k++){ output_bit(PIN_A4,direction); for(j=0;j<255;j++){ //callingset_pwm1_duty()withanintcausescompilertouse8-bitmode set_pwm1_duty(j); analog_val=read_adc();//takethereadingpin6 delay_ms(analog_val);

for(j=255;j>0;j--){ set_pwm1_duty(j); analog_val=read_adc(); delay_ms(analog_val); }

Digital Co n t r ol Microsys te m s Part 5: Pulse Width Modulation - PWM


} direction=(~direction);

} */

You might also like