Re: 如何使用30F4011的PWM
|
||||
---|---|---|---|---|
新會員
|
真的真的太感謝您了~~~~原來除了使用dspic30f4011的pwm內定的time中斷之外.原來還要多設一組time中斷 .我會去試試看.感嗯啊...果然是高手,我已經了解了,謝謝啊!!
發表於: 2008/7/6 21:01
|
|||
|
如何使用30F4011的PWM
|
||||
---|---|---|---|---|
新會員
|
各位大大好:
我是個PIC新手,想請問有關曾百由老師的PWM程式. 我應用在直流馬達上.當pwm輸出0.3s後.反轉pwm的相位再0.3s .重複動作. 使用1組pwm腳位: PWM1H(PIN37) & PWM1L(PIN36)... 我是用兩組PWM.PWM1程式來做正反轉 . 因為我用這樣去試 . 一直無法做正反轉 . 而且只會跑單一方向 .改了很多次. 都做不出來 . int main(void) //主程式開始 { ADPCFG=0xFFFF; while(1) { if(flag_a==0) //旗標判斷 { Init_MCPWM( ); //呼叫PWM. for (i=0;i<200;i++) //延遲時間 for(j=0;j<100;j++) flag_a=1; //跑完0.3s. 設定為1 ; } else { Init_MCPWM1( );} //呼叫PWM1 for (i=0;i<200;i++) //延遲時間 for(j=0;j<100;j++) flag_a=0; //跑完0.3s. 設定為0 ; } } 以上就為主程式的迴圈 下面為PWM的設定.就是照著曾老師的程式修改而已 //******************************************* //*與 MCPWM1() 相同 .只有改變 dutycycle的數值 使其輸出剛好相反. ***** //******************************************* void Init_MCPWM(void) { /* Holds the PWM interrupt configuration value*/ unsigned int config; /* Holds the value to be loaded into dutycycle register */ unsigned int period; /* Holds the value to be loaded into special event compare register */ unsigned int sptime; /* Holds PWM configuration value */ unsigned int config1; /* Holds the value be loaded into PWMCON1 register */ unsigned int config2; /* Holds the value to configure the special event trigger postscale and dutycycle */ unsigned int config3; /* The value of ‘dutycyclereg’ determines the duty cycle register(PDCx) to be written */ unsigned int dutycyclereg; unsigned int dutycycle; unsigned char updatedisable; /* Configure pwm interrupt enable/disable and set interrupt priorties */ config = (PWM_INT_DIS & PWM_FLTA_DIS_INT & PWM_INT_PR1& PWM_FLTA_INT_PR0); ConfigIntMCPWM( config ); /* Configure PWM to generate square wave of 50% duty cycle */ dutycyclereg = 1; dutycycle = 0x0059; updatedisable = 0; SetDCMCPWM(dutycyclereg,dutycycle,updatedisable); period = 0x002e; sptime = 0x0; DTCON1=0x00f; config1 = (PWM_EN & PWM_IDLE_STOP & PWM_OP_SCALE16 & PWM_IPCLK_SCALE16 &PWM_MOD_UPDN); config2 = (PWM_MOD1_COMP &PWM_PDIS3H & PWM_PDIS2H & PWM_PEN1H & PWM_PDIS3L & PWM_PDIS2L & PWM_PEN1L); config3 = (PWM_SEVOPS1 & PWM_OSYNC_PWM & PWM_UEN); OpenMCPWM(period,sptime,config1,config2,config3); } //***************************************************************************** //********** 與 MCPWM() 相同 .只有改變 dutycycle的數值 使其輸出剛好相反. ***** //***************************************************************************** void Init_MCPWM1(void) { /* Holds the PWM interrupt configuration value*/ unsigned int config; /* Holds the value to be loaded into dutycycle register */ unsigned int period; /* Holds the value to be loaded into special event compare register */ unsigned int sptime; /* Holds PWM configuration value */ unsigned int config1; /* Holds the value be loaded into PWMCON1 register */ unsigned int config2; /* Holds the value to configure the special event trigger postscale and dutycycle */ unsigned int config3; /* The value of ‘dutycyclereg’ determines the duty cycle register(PDCx) to be written */ unsigned int dutycyclereg; unsigned int dutycycle; unsigned char updatedisable; /* Configure pwm interrupt enable/disable and set interrupt priorties */ config = (PWM_INT_DIS & PWM_FLTA_DIS_INT & PWM_INT_PR1 & PWM_FLTA_INT_PR0); ConfigIntMCPWM( config ); /* Configure PWM to generate square wave of 50% duty cycle */ dutycyclereg = 1; dutycycle = 0x0003; updatedisable = 0; SetDCMCPWM(dutycyclereg,dutycycle,updatedisable); period = 0x002b; sptime = 0x0; DTCON1=0x000f; config1 = (PWM_EN & PWM_IDLE_STOP & PWM_OP_SCALE16& PWM_IPCLK_SCALE16 &PWM_MOD_UPDN); config2 = (PWM_MOD1_COMP &PWM_PDIS3H & PWM_PDIS2H & PWM_PEN1H &PWM_PDIS3L & PWM_PDIS2L & PWM_PEN1L); config3 = (PWM_SEVOPS1 & PWM_OSYNC_PWM & PWM_UEN); OpenMCPWM(period,sptime,config1,config2,config3); } 基本上PWM與PWM1的程式都是照著曾老師的程式做修改.想說先試著修改程式人後能更深入了解應用.馬達單一方向的都OK.但是要做更改轉向就不行...請各位大大可以跟我說是我邏輯有錯嗎?還是PWM可以不可以用迴圈的方式設定延遲時間嗎 ?謝謝~!感恩
發表於: 2008/6/29 21:27
|
|||
|