如何利用ARM里的PWM模块产生SPWM波
利用PWM模块产生SPWM波,主要是利用了PWM模块里一个比较器中断功能。通过比较器,产生一个中断,再附值产生一个PWM脉冲,一直循环,下去,通过附不同的值,就可以产生一个SPWM了。主要程序如下:
const 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口外设
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); // 使能PWM外设
GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);// 设置PD0,PD1为PWM0和PWM1
PWMGenConfigure(PWM_BASE, PWM_GEN_0, // 设置PWM发生器0为上下计数方式,两路PWM不同步
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC | PWM_GEN_ACT_ZERO | PWM_GEN_ACT_ONE <6);
//PWM0A 在计数器为0时,输出 为低电平,与比较器A值相等时输出为高电平 。
PWMOutputInvert(PWM_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT,true); //设置PWM两路反相
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, 1000); // 设置两路PWM的共同周期
PWMDeadBandEnable(PWM_BASE,PWM_GEN_0,15,15); //死区时间设置
PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, spwmwidth[16]); // 分别设置两路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发生器0
HWREG(PWM_BASE+0X044)|=0x01;
// PWMGenIntRegister(PWM_BASE, PWM_GEN_0,*SpwmISRHandler) ;
PWMIntEnable(PWM_BASE,PWM_INT_GEN_0); //中断使能 WM发生器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();
}
ARMPWM模块SPWM 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)