dsp28335 pwm实现流水灯,只要.c文件,不需要.h文件,用定时器延时
时间: 2023-12-06 13:02:44 浏览: 100
dsp28335流水灯程序
5星 · 资源好评率100%
以下是基于DSP28335的PWM实现流水灯的.c文件代码,不需要使用.h文件,使用定时器延时:
```c
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
// Function Prototypes
interrupt void cpu_timer0_isr(void);
// Global Variables
Uint16 pwm_period = 375; // PWM周期
Uint16 pwm_duty = 187; // PWM占空比,即50%
Uint16 pwm_counter = 0; // PWM计数器,用于计算PWM占空比
Uint16 led_counter = 0; // LED计数器,用于控制流水灯
void main(void)
{
// Initialize System Control:
InitSysCtrl();
// Initialize GPIO:
InitGpio();
// Initialize PIE:
InitPieCtrl();
// Disable CPU interrupts:
DINT;
// Initialize the PIE vector table:
InitPieVectTable();
// Setup CPU Timer0:
ConfigCpuTimer(&CpuTimer0, 150, 1000000);
// Set up GPIO for PWM output:
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1; // Disable pull-up on GPIO0 (PWM1)
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // Configure GPIO0 as PWM1 output
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // Set GPIO0 as output
EDIS;
// Enable CPU interrupts:
IER |= M_INT1;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
// Start CPU Timer0:
StartCpuTimer0();
// Loop forever:
while(1)
{
// Do nothing
}
}
// CPU Timer0 ISR
interrupt void cpu_timer0_isr(void)
{
// Acknowledge the interrupt
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
// Increment PWM counter
pwm_counter++;
// Check if PWM counter is greater than PWM duty
if(pwm_counter > pwm_duty)
{
// Set GPIO0 low
GpioDataRegs.GPACLEAR.bit.GPIO0 = 1;
}
else
{
// Set GPIO0 high
GpioDataRegs.GPASET.bit.GPIO0 = 1;
}
// Check if PWM counter is equal to or greater than PWM period
if(pwm_counter >= pwm_period)
{
// Reset PWM counter
pwm_counter = 0;
}
// Increment LED counter
led_counter++;
// Check if LED counter is greater than or equal to 500,000
if(led_counter >= 500000)
{
// Reset LED counter
led_counter = 0;
// Shift LED pattern to the left
pwm_duty = pwm_duty << 1;
// Check if LED pattern has shifted beyond 100%
if(pwm_duty >= pwm_period)
{
// Reset LED pattern to 50%
pwm_duty = 187;
}
}
}
```
以上代码将CPU Timer0配置为1 MHz的计时器,并使用定时器中断来实现PWM输出和流水灯效果。其中,使用了两个计数器pwm_counter和led_counter来控制PWM输出和流水灯效果。在每次CPU Timer0中断时,增加pwm_counter和led_counter的值,然后根据pwm_counter的值来控制PWM输出,根据led_counter的值来控制LED流水灯效果。最后将pwm_duty的值左移一位来实现LED流水灯效果,当pwm_duty的值超过PWM周期时,将pwm_duty的值重置为50%。
阅读全文