DSP

DSP PWM控制LED闪烁

2019-07-13 10:24发布

PWM让LED闪烁主要是调节占空比,一个周期内高电平和低电平的比例不同LED的亮度不同,根据输出高电平的时间来控制LED闪烁。 主要步骤:
  • 初始化GPIO
  • 初始化PWM
  • 循环延迟(改变占空比,控制LED的亮度)
主要设置:
  • 设置时钟周期:EPwm1Regs.TBPRD
  • 设置比较寄存器:EPwm1Regs.CMPA.half.CMPA
  • 设置动作控制寄存器:EPwm1Regs.AQCTLA
时钟周期是PWM的一个周期,不同计数模式计数方式不同,这里只说向上计数。从0开始计数,直到PRD为止,然后接着从0开始计数。向下和向上-下计数模式类似。 当时钟计数值等于比较寄存器的值,会向动作发送消息,进行相应的动作,相应动作是在动作控制寄存器里设置的。 这里只用到了这些,其余的没用到,不好的地方还请指教,有问题的也可以留言探讨共同进步。下面是代码: /* * This project is just for testing pwm */ #include "DSP2833x_Project.h" void InitGpio(void); void InitPwm(void); int main(void) { Uint16 flag = 1; InitSysCtrl(); InitXintf16Gpio(); DINT; InitPieCtrl(); IFR = 0x0000; IER = 0x0000; InitPieVectTable(); InitGpio(); InitPwm(); while(1) { if(EPwm1Regs.CMPA.half.CMPA > 999) flag = 1; if(EPwm1Regs.CMPA.half.CMPA == 0) flag = 0; flag == 0 ? EPwm1Regs.CMPA.half.CMPA ++ : EPwm1Regs.CMPA.half.CMPA --; DELAY_US(1000); } return 0; } void InitGpio(void) { EALLOW; GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0; GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1; EDIS; } void InitPwm(void) { EALLOW; EPwm1Regs.TBPRD = 999; //set time Period EPwm1Regs.TBPHS.half.TBPHS = 0; EPwm1Regs.TBCTR = 0; EPwm1Regs.TBCTL.all = 0; /*EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP; // Count up EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW; //loaded from its shadow register EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; // Clock ratio to SYSCLKOUT EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;*/ EPwm1Regs.CMPA.half.CMPA = 999; //set compare EPwm1Regs.CMPCTL.all = 0; /*EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW; // Load registers every ZERO EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW; EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;*/ //set actions EPwm1Regs.AQCTLA.all = 0x24; /*EPwm1Regs.AQCTLA.bit.CAU = 0x10; //当计数值等于CMPA时将PWM1A置高 EPwm1Regs.AQCTLA.bit.PRD = 0x01; //当计数值等于PRD时将PWM1A置低*/ EPwm1Regs.AQCTLB.all = 0x12; /*EPwm1Regs.AQCTLB.bit.CAU = 0x01; //当计数值等于CMPA时将PWM1B置低 EPwm1Regs.AQCTLB.bit.PRD = 0x10; //当计数值等于PRD时将PWM1B置高*/ EDIS; }