这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 嵌入式开发» MCU» 使用ADuC7060的PWM1端口输出PWM脉冲,断电后再上电导致PWM输出异常

共2条 1/1 1 跳转至

使用ADuC7060的PWM1端口输出PWM脉冲,断电后再上电导致PWM输出异常,怎么解决?

工程师
2024-01-20 19:41:25 打赏

我打算使用ADuC7060的PWM1端口输出PWM脉冲,单端输出,占空比是50%,周期是100μS,在用Keil调试时能正常输出我所需要的PWM脉冲,后来断电后再上电却发现PWM输出异常,主要是占空比异常,周期还是正常的。后来我将官方的PWM程序烧写进去后再将我的PWM测试程序烧写到芯片中后又正常了,但是一断电再上电就出现异常了。
请问大虾这是什么造成的。解决方式是什么?
下面附上我的PWM测试程序:
int main(void)
{
POWKEY1 = 0x1;
POWCON0 = 0x78; // Set core to max CPU speed of 10.24Mhz
POWKEY2 = 0xF4;
// Initialize the GPIO pins for PWM mode
GP0CON0 = BIT16; // Select PWM1 output
PWMCON = BIT0; // PWM Standard mode, UCLK/2
PWM0COM2 = 0x80; // Configure PWM1 output low trigger time
PWM0LEN = 0xFF; // Configure PWM1 output high trigger time
while (1)
{
}
}
官方的PWM程序:
unsigned char bTripDetected = 0;
int main(void)
{
POWKEY1 = 0x1;
POWCON0 = 0x78; // Set core to max CPU speed of 10.24Mhz
POWKEY2 = 0xF4;

// Initialize the GPIO pins for PWM mode
GP0CON0 = BIT16; // Select PWM1 output
// GP1CON = BIT8 + BIT12 // Select PWMSYNC and PWMTRIP functions
// + BIT16 + BIT20 // Select PWM2, PWM3 output
// + BIT24; // Select PWM4 output
// GP2CON = BIT0 + BIT4; // Select PWM0 and PWM5 output

PWMCON = BIT0 + BIT10; // PWM Standard mode, UCLK/2, PWM Trip interrupt enabled

ConfigurePWM(); // Call function to configure PWM
bTripDetected = 0;
IRQEN = BIT17; // Enable PWM Trip interrupt source
while (1)
{
if (bTripDetected == 1)
{
bTripDetected = 0;
ConfigurePWM();
}
}
}
void IRQ_Handler(void) __irq // Unused in this application
{
unsigned long IRQSTATUS = 0;
IRQSTATUS = IRQSTA; // Read off IRQSTA register
if ((IRQSTATUS BIT17) == BIT17) //If PWM trip interrupt source
{
bTripDetected = 1; // Set flag to re configure the PWM block
PWMCLRI = 0xFF; // Clear the PWM trip interrupt source
}
}
void ConfigurePWM(void)
{
//Configure PWM0 for 20khz duty cycle
// P0.4
PWM0COM0 = 0x80; // Configure PWM0 output high trigger time
PWM0COM1 = 0xFF; // Configure PWM0 output Low trigger time
// PWM0COM2 = 0x80; // Configure PWM1 output low trigger time
PWM0LEN = 0xFF; // Configure PWM1 output high trigger time
/* //Configure PWM2 for 10khz duty cycle
// P1.5
PWM1COM0 = 0xFF; // Configure PWM2 output high trigger time
PWM1COM1 = 0x1FF; // Configure PWM2 output Low trigger time
PWM1COM2 = 0xFF; // Configure PWM3 output low trigger time
PWM1LEN = 0x1FF; // Configure PWM3 output high trigger time
//Configure PWM4 for 40khz duty cycle
// P2.1
PWM2COM0 = 0x40; // Configure PWM4 output high trigger time
PWM2COM1 = 0x80; // Configure PWM4 output Low trigger time
PWM2COM2 = 0x40; // Configure PWM5 output low trigger time
PWM2LEN = 0x80; // Configure PWM6 output high trigger time
*/
}




关键词: ADuC7060 PWM

助工
2024-01-20 19:42:18 打赏
2楼

出现PWM输出异常的原因可能是芯片在断电后重新上电时,部分寄存器的值没有恢复到默认状态。解决这个问题的方式是在初始化PWM前,先对相关寄存器进行恢复操作。

你可以尝试在程序的开头添加如下代码,将相关寄存器恢复到默认状态,然后再配置PWM输出:

```
void reset_registers(void)
{
// 将相关寄存器恢复到默认状态
PWMCON0 = 0;
PWMCON1 = 0;
PWMCON2 = 0;
PWMTH0 = 0;
PWMTH1 = 0;
PWMTH2 = 0;
PWMADR0 = 0;
PWMADR1 = 0;
PWMADR2 = 0;
}

int main(void)
{
// 调用恢复寄存器函数
reset_registers();

// 后续的代码继续配置和输出PWM信号
...
}


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]