dsp pwm实现流水灯,只要.c文件,不需要.h文件
时间: 2023-12-06 08:02:44 浏览: 164
以下是一个简单的DSP PWM实现流水灯的.c文件示例:
```c
#include "F28x_Project.h"
#define LED1_GPIO 31 // LED1控制IO口
#define LED2_GPIO 34 // LED2控制IO口
#define LED3_GPIO 38 // LED3控制IO口
#define PWM_FREQ 5000 // PWM频率
#define PWM_PERIOD (200000000/PWM_FREQ) // PWM周期
#define DUTY_CYCLE_1 (PWM_PERIOD/3) // LED1占空比
#define DUTY_CYCLE_2 (2*PWM_PERIOD/3) // LED2占空比
#define DUTY_CYCLE_3 (PWM_PERIOD) // LED3占空比
Uint16 led1_state = 0;
Uint16 led2_state = 0;
Uint16 led3_state = 0;
void Init_GPIO(void);
void Init_PWM(void);
void main(void)
{
InitSysCtrl();
Init_GPIO();
Init_PWM();
while (1)
{
if (led1_state == 1)
{
EPwm1Regs.CMPA.bit.CMPA = DUTY_CYCLE_1;
EPwm2Regs.CMPA.bit.CMPA = 0;
EPwm3Regs.CMPA.bit.CMPA = 0;
led1_state = 0;
led2_state = 1;
}
else if (led2_state == 1)
{
EPwm1Regs.CMPA.bit.CMPA = 0;
EPwm2Regs.CMPA.bit.CMPA = DUTY_CYCLE_2;
EPwm3Regs.CMPA.bit.CMPA = 0;
led2_state = 0;
led3_state = 1;
}
else if (led3_state == 1)
{
EPwm1Regs.CMPA.bit.CMPA = 0;
EPwm2Regs.CMPA.bit.CMPA = 0;
EPwm3Regs.CMPA.bit.CMPA = DUTY_CYCLE_3;
led3_state = 0;
led1_state = 1;
}
DELAY_US(100000); // 100ms延时
}
}
void Init_GPIO(void)
{
EALLOW;
// 配置LED控制IO口为GPIO输出模式
GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0; // GPIO31 - LED1
GpioCtrlRegs.GPAMUX1.bit.GPIO34 = 0; // GPIO34 - LED2
GpioCtrlRegs.GPBMUX1.bit.GPIO38 = 0; // GPIO38 - LED3
GpioCtrlRegs.GPADIR.bit.GPIO31 = 1; // GPIO31 - LED1
GpioCtrlRegs.GPADIR.bit.GPIO34 = 1; // GPIO34 - LED2
GpioCtrlRegs.GPBDIR.bit.GPIO38 = 1; // GPIO38 - LED3
EDIS;
}
void Init_PWM(void)
{
EALLOW;
// 配置PWM引脚和时钟
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // EPWM1A
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // EPWM2A
GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1; // EPWM3A
CpuSysRegs.PCLKCR2.bit.EPWM1ENCLK = 1; // EPWM1时钟使能
CpuSysRegs.PCLKCR2.bit.EPWM2ENCLK = 1; // EPWM2时钟使能
CpuSysRegs.PCLKCR2.bit.EPWM3ENCLK = 1; // EPWM3时钟使能
// 配置PWM模块
EPwm1Regs.TBPRD = PWM_PERIOD; // PWM周期
EPwm1Regs.TBCTL.bit.CTRMODE = 0; // 计数器停止
EPwm1Regs.TBCTL.bit.PHSEN = 0; // 禁止相位补偿
EPwm1Regs.TBCTL.bit.SYNCOSEL = 0;// 禁止同步输出
EPwm1Regs.TBCTL.bit.HSPCLKDIV = 0;// 高速时钟分频
EPwm1Regs.TBCTL.bit.CLKDIV = 0; // 时钟分频
EPwm1Regs.CMPA.bit.CMPA = 0; // 初始占空比
EPwm2Regs.TBPRD = PWM_PERIOD; // PWM周期
EPwm2Regs.TBCTL.bit.CTRMODE = 0; // 计数器停止
EPwm2Regs.TBCTL.bit.PHSEN = 0; // 禁止相位补偿
EPwm2Regs.TBCTL.bit.SYNCOSEL = 0;// 禁止同步输出
EPwm2Regs.TBCTL.bit.HSPCLKDIV = 0;// 高速时钟分频
EPwm2Regs.TBCTL.bit.CLKDIV = 0; // 时钟分频
EPwm2Regs.CMPA.bit.CMPA = 0; // 初始占空比
EPwm3Regs.TBPRD = PWM_PERIOD; // PWM周期
EPwm3Regs.TBCTL.bit.CTRMODE = 0; // 计数器停止
EPwm3Regs.TBCTL.bit.PHSEN = 0; // 禁止相位补偿
EPwm3Regs.TBCTL.bit.SYNCOSEL = 0;// 禁止同步输出
EPwm3Regs.TBCTL.bit.HSPCLKDIV = 0;// 高速时钟分频
EPwm3Regs.TBCTL.bit.CLKDIV = 0; // 时钟分频
EPwm3Regs.CMPA.bit.CMPA = 0; // 初始占空比
// 配置PWM计数器
EPwm1Regs.TBCTL.bit.CTRMODE = 0x00; // 计数器停止
EPwm1Regs.TBCTL.bit.SYNCOSEL = 0x0; // 禁止同步输出
EPwm1Regs.TBCTL.bit.PHSEN = 0x0; // 禁止相位补偿
EPwm1Regs.TBCTL.bit.HSPCLKDIV = 0x0; // 高速时钟分频
EPwm1Regs.TBCTL.bit.CLKDIV = 0x0; // 时钟分频
EPwm1Regs.TBCTL.bit.FREE_SOFT = 0x3; // 使能停止后自动清零
EPwm2Regs.TBCTL.bit.CTRMODE = 0x00; // 计数器停止
EPwm2Regs.TBCTL.bit.SYNCOSEL = 0x0; // 禁止同步输出
EPwm2Regs.TBCTL.bit.PHSEN = 0x0; // 禁止相位补偿
EPwm2Regs.TBCTL.bit.HSPCLKDIV = 0x0; // 高速时钟分频
EPwm2Regs.TBCTL.bit.CLKDIV = 0x0; // 时钟分频
EPwm2Regs.TBCTL.bit.FREE_SOFT = 0x3; // 使能停止后自动清零
EPwm3Regs.TBCTL.bit.CTRMODE = 0x00; // 计数器停止
EPwm3Regs.TBCTL.bit.SYNCOSEL = 0x0; // 禁止同步输出
EPwm3Regs.TBCTL.bit.PHSEN = 0x0; // 禁止相位补偿
EPwm3Regs.TBCTL.bit.HSPCLKDIV = 0x0; // 高速时钟分频
EPwm3Regs.TBCTL.bit.CLKDIV = 0x0; // 时钟分频
EPwm3Regs.TBCTL.bit.FREE_SOFT = 0x3; // 使能停止后自动清零
// 配置PWM比较器
EPwm1Regs.CMPCTL.bit.SHDWAMODE = 0x3; // 立即更新占空比
EPwm1Regs.CMPCTL.bit.LOADAMODE = 0x0; // 立即更新占空比
EPwm1Regs.CMPCTL.bit.SHDWBMODE = 0x3; // 立即更新占空比
EPwm1Regs.CMPCTL.bit.LOADBMODE = 0x0; // 立即更新占空比
EPwm1Regs.AQCTLA.bit.CAU = 0x2; // 当PWM计数器计数到CMPA时,设置PWM输出为高电平
EPwm1Regs.AQCTLA.bit.CAD = 0x1; // 当PWM计数器计数到CMPA时,设置PWM输出为低电平
EPwm2Regs.CMPCTL.bit.SHDWAMODE = 0x3; // 立即更新占空比
EPwm2Regs.CMPCTL.bit.LOADAMODE = 0x0; // 立即更新占空比
EPwm2Regs.CMPCTL.bit.SHDWBMODE = 0x3; // 立即更新占空比
EPwm2Regs.CMPCTL.bit.LOADBMODE = 0x0; // 立即更新占空比
EPwm2Regs.AQCTLA.bit.CAU = 0x2; // 当PWM计数器计数到CMPA时,设置PWM输出为高电平
EPwm2Regs.AQCTLA.bit.CAD = 0x1; // 当PWM计数器计数到CMPA时,设置PWM输出为低电平
EPwm3Regs.CMPCTL.bit.SHDWAMODE = 0x3; // 立即更新占空比
EPwm3Regs.CMPCTL.bit.LOADAMODE = 0x0; // 立即更新占空比
EPwm3Regs.CMPCTL.bit.SHDWBMODE = 0x3; // 立即更新占空比
EPwm3Regs.CMPCTL.bit.LOADBMODE = 0x0; // 立即更新占空比
EPwm3Regs.AQCTLA.bit.CAU = 0x2; // 当PWM计数器计数到CMPA时,设置PWM输出为高电平
EPwm3Regs.AQCTLA.bit.CAD = 0x1; // 当PWM计数器计数到CMPA时,设置PWM输出为低电平
EDIS;
}
```
这个代码实现了三个LED流水灯,每个LED的占空比不同,通过三个PWM信号控制。需要注意的是,这个代码中使用了DSP的库函数和寄存器宏定义,如果你使用的是不同型号或者不同品牌的DSP,可能需要对代码进行修改。
阅读全文