如何利用ARM里的PWM模塊產(chǎn)生SPWM波
利用PWM模塊產(chǎn)生SPWM波,主要是利用了PWM模塊里一個比較器中斷功能。通過比較器,產(chǎn)生一個中斷,再附值產(chǎn)生一個PWM脈沖,一直循環(huán),下去,通過附不同的值,就可以產(chǎn)生一個SPWM了。主要程序如下:
本文引用地址:http://m.butianyuan.cn/article/201611/316675.htmconst INT16U spwmwidth[spwmct]={24,72,120,168,215,262,308,353,398,441,484,525,565,604,641,677,
711,743,774,803,829,854,877,897,916,932,946,958,967,974,979,981,
981,979,974,967,958,946,932,916,897,877,854,829,803,774,743,711,
677,641,604,565,525,484,441,398,353,308,262,215,168,120,72,24
};
void SpwmInit(void)
{
SysCtlPWMClockSet(SYSCTL_PWMDIV_64); // PWM時鐘源64分頻
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // 使能PD口外設(shè)
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); // 使能PWM外設(shè)
GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);// 設(shè)置PD0,PD1為PWM0和PWM1
PWMGenConfigure(PWM_BASE, PWM_GEN_0, // 設(shè)置PWM發(fā)生器0為上下計數(shù)方式,兩路PWM不同步
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC | PWM_GEN_ACT_ZERO | PWM_GEN_ACT_ONE <<6);
//PWM0A 在計數(shù)器為0時,輸出 為低電平,與比較器A值相等時輸出為高電平 。
PWMOutputInvert(PWM_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT,true); //設(shè)置PWM兩路反相
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, 1000); // 設(shè)置兩路PWM的共同周期
PWMDeadBandEnable(PWM_BASE,PWM_GEN_0,15,15); //死區(qū)時間設(shè)置
PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, spwmwidth[16]); // 分別設(shè)置兩路PWM的匹配值
// PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 1000);
PWMOutputState(PWM_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, true);// 使能PWM0和PWM1
PWMGenEnable(PWM_BASE, PWM_GEN_0); // 使能PWM發(fā)生器0
HWREG(PWM_BASE+0X044)|=0x01;
// PWMGenIntRegister(PWM_BASE, PWM_GEN_0,*SpwmISRHandler) ;
PWMIntEnable(PWM_BASE,PWM_INT_GEN_0); //中斷使能 WM發(fā)生器0模塊
IntEnable(INT_PWM0);
}
#endif
/***********中斷程序*****************************************************************************/
void SpwmISRHandler (void)
{
static INT16U count;
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr;
#endif
OS_ENTER_CRITICAL();
OSIntNesting++;
OS_EXIT_CRITICAL();
PWMGenIntClear(PWM_BASE, PWM_GEN_0,PWMIntStatus(PWM_BASE, true)|PWM_INT_GEN_0 );//清除中斷
PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, spwmwidth[count]);
if (count>=63||count<=0 ){
count=1 ;
}
else {
count++;
}
OSIntExit();
}
評論