You are on page 1of 3

9/2/2014

Motor PWM example for the dsPIC30F4011 | ad hocumentation n. fast, instinctive documentation of ideas and solutions.

/ / / /T h i sp r o g r a mi sa ne x a m p l eo ft h em o t o rP W Mf a c i l i t yo nt h e / /d s P I C 3 0 F 4 0 1 1 . / / / /W r i t t e nb yT e dB u r k e-l a s tu p d a t e d4 1 2 2 0 1 0 / / # i n c l u d e< l i b p i c 3 0 . h > # i n c l u d e< p 3 0 f 4 0 1 1 . h > / /C o n f i g u r a t i o ns e t t i n g s _ F O S C ( C S W _ F S C M _ O F F&F R C _ P L L 1 6 ) ; / /F o s c = 1 6 x 7 . 5 M H z ,F c y = 3 0 M H z _ F W D T ( W D T _ O F F ) ; / /W a t c h d o gt i m e ro f f _ F B O R P O R ( M C L R _ D I S ) ; / /D i s a b l er e s e tp i n / /F u n c t i o np r o t o t y p e s v o i dc o n f i g u r e _ p i n s ( ) ; u n s i g n e di n tr e a d _ a n a l o g _ c h a n n e l ( i n tn ) ; i n tm a i n ( ) { i n tv o l t a g e ; / /S e tu pw h i c hp i n sa r ew h i c h c o n f i g u r e _ p i n s ( ) ; w h i l e ( 1 ) { / /A n a l o gi n p u t0c o n t r o l sP W M1d u t yc y c l e . v o l t a g e=r e a d _ a n a l o g _ c h a n n e l ( 0 ) ; P D C 1=( i n t ) ( ( v o l t a g e/1 0 2 3 . 0 )*2*P T P E R ) ; / /A n a l o gi n p u t1c o n t r o l sP W M2d u t yc y c l e . v o l t a g e=r e a d _ a n a l o g _ c h a n n e l ( 1 ) ; P D C 2=( i n t ) ( ( v o l t a g e/1 0 2 3 . 0 )*2*P T P E R ) ; / /A n a l o gi n p u t2c o n t r o l sP W M3d u t yc y c l e . v o l t a g e=r e a d _ a n a l o g _ c h a n n e l ( 2 ) ; P D C 3=( i n t ) ( ( v o l t a g e/1 0 2 3 . 0 )*2*P T P E R ) ; } r e t u r n0 ;
http://batchloaf.wordpress.com/2010/12/06/motor-pwm-example-for-the-dspic30f4011/ 1/3

9/2/2014

Motor PWM example for the dsPIC30F4011 | ad hocumentation n. fast, instinctive documentation of ideas and solutions.

} v o i dc o n f i g u r e _ p i n s ( ) { / /C o n f i g u r eR D 0a sad i g i t a lo u t p u t L A T D=0 ; T R I S D=0 b 1 1 1 1 1 1 1 0 ;

/ /C o n f i g u r ea n a l o gi n p u t s T R I S B=0 x 0 1 F F ; / /P o r tBa l li n p u t s A D P C F G=0 x F F 0 0 ; / /L o w e s t8P O R T Bp i n sa r ea n a l o gi n p u t s A D C O N 1=0 ; / /M a n u a l l yc l e a rS A M Pt oe n ds a m p l i n g ,s t a r tc o n A D C O N 2=0 ; / /V o l t a g er e f e r e n c ef r o mA V D Da n dA V S S A D C O N 3=0 x 0 0 0 5 ; / /M a n u a lS a m p l e ,A D C S = 5>T a d=3 * T c y=0 . 1 u s A D C O N 1 b i t s . A D O N=1 ;/ /T u r nA D CO N / /C o n f i g u r eP W Mf o rf r e er u n n i n gm o d e / /P W Mp e r i o d=T c y*p r e s c a l e*P T P E R=0 . 3 3 n s*6 4*9 4 7 0=2 0 m s P W M C O N 1=0 x 0 0 F F ;/ /E n a b l ea l lP W Mp a i r si nc o m p l e m e n t a r ym o d e P T C O N=0 ; _ P T C K P S=3 ; / /p r e s c a l e = 1 : 6 4( 0 = 1 : 1 ,1 = 1 : 4 ,2 = 1 : 1 6 ,3 = 1 : 6 4 ) P T P E R=9 4 7 0 ; / /2 0 m sP W Mp e r i o d( 1 5 b i tp e r i o dv a l u e ) P D C 1=0 ; / /0 %d u t yc y c l eo nc h a n n e l1( m a xi s6 5 5 3 6 ) P D C 2=0 ; / /0 %d u t yc y c l eo nc h a n n e l2( m a xi s6 5 5 3 6 ) P D C 3=0 ; / /0 %d u t yc y c l eo nc h a n n e l3( m a xi s6 5 5 3 6 ) P T M R=0 ; / /C l e a r1 5 b i tP W Mt i m e rc o u n t e r _ P T E N=1 ; / /E n a b l eP W Mt i m eb a s e } / /T h i sf u n c t i o nr e a d sas i n g l es a m p l ef r o mt h es p e c i f i e d / /a n a l o gi n p u t .I ts h o u l dt a k el e s st h a n2 . 5 u si ft h ec h i p / /i sr u n n i n ga ta b o u t3 0M I P S . u n s i g n e di n tr e a d _ a n a l o g _ c h a n n e l ( i n tc h a n n e l ) { A D C H S=c h a n n e l ; / /S e l e c tt h er e q u e s t e dc h a n n e l A D C O N 1 b i t s . S A M P=1 ; / /s t a r ts a m p l i n g _ _ d e l a y 3 2 ( 3 0 ) ; / /1 u sd e l a y@3 0M I P S A D C O N 1 b i t s . S A M P=0 ; / /s t a r tC o n v e r t i n g w h i l e( ! A D C O N 1 b i t s . D O N E ) ;/ /S h o u l dt a k e1 2*T a d=1 . 2 u s r e t u r nA D C B U F 0 ; }

http://batchloaf.wordpress.com/2010/12/06/motor-pwm-example-for-the-dspic30f4011/

2/3

9/2/2014

Motor PWM example for the dsPIC30F4011 | ad hocumentation n. fast, instinctive documentation of ideas and solutions.

http://batchloaf.wordpress.com/2010/12/06/motor-pwm-example-for-the-dspic30f4011/

3/3

You might also like