dsPIC33更新PWM占空比和频率

时间:2016-08-28 19:22:03

标签: frequency pwm dspic

我正在尝试使用一个PWM发生器控制每个对角线的H桥转换器(在我的情况下为PWM1和PWM4)。

我希望能够控制频率和占空比。 为此,我在PWM4周期开始时产生一个触发中断,然后为两个PWM通道更新寄存器的周期,相位和占空比。

问题是,当此更新发生时,一段时间左右会出现一些意外行为。见图。

PWM4为青色,PWM1为品红色,当PWM4的触发中断发生时黄色切换。

似乎PWM1(品红色)更新到更慢的频率(应该如此),但PWM4继续在旧的频率上运行另一段时间。 PWM1还会产生一个短脉冲。 我测量了更新周期,占空比和相位寄存器值所需的时间,它大约是1us,所以这不应该是一个问题。 以下是PWM配置:

void PWM4Config()  {

ANSELEbits.ANSE6 = 0;
ANSELEbits.ANSE7 = 0;
TRISEbits.TRISE6 = 0;
TRISEbits.TRISE7 = 0;

PTPER = 7002;                   //period je 50us
PTCON2bits.PCLKDIV = 0b000;     //1:1  

//fazni stavovi
PHASE4 = 0;
SPHASE4 = 0;

//duty
PDC4 = 3501;
SDC4 = 3501;
//dead time
DTR4 = 0;
//set PWM mode to independent, active high
IOCON4bits.PENH = 1;
IOCON4bits.PENL = 1;
IOCON4bits.POLH = 0;
IOCON4bits.POLL = 0;
IOCON4bits.PMOD = 0b11;
IOCON4bits.OVRENH = 0;
IOCON4bits.OVRENL = 0;
IOCON4bits.OVRDAT = 0b00;
IOCON4bits.FLTDAT = 0b00;
IOCON4bits.CLDAT = 0b00;
IOCON4bits.SWAP = 0;
IOCON4bits.OSYNC = 0;

//set primary time base, edge aligned, independent duty cycles
PWMCON4 = 0x0000;
//PWMCON4bits.IUE = 1;  //privremeno
//config faults
FCLCON4 = 0x0003;

//config trigger & interrupt
IFS6bits.PWM4IF = 0;
IEC6bits.PWM4IE = 1;  
TRGCON4 = 0x0000;       //triger at every period of pwm
TRIG4 = 0x0000;         //triger at start of pwm period
PWMCON4bits.TRGIEN = 0;


}

void PWM1Config()  {

ANSELEbits.ANSE0 = 0;
ANSELEbits.ANSE1 = 0;
TRISEbits.TRISE0 = 0;
TRISEbits.TRISE1 = 0;

PTPER = 7002;                   //period je 50us
PTCON2bits.PCLKDIV = 0b000;     //1:1  

//fazni stavovi
PHASE1 = 3051;
SPHASE1 = 3501;

//duty
PDC1 = 3501;
SDC1 = 3501;
//dead time
DTR1 = 0;
//set PWM mode to independent, active high
IOCON1bits.PENH = 1;
IOCON1bits.PENL = 1;
IOCON1bits.POLH = 0;
IOCON1bits.POLL = 0;
IOCON1bits.PMOD = 0b11;
IOCON1bits.OVRENH = 0;
IOCON1bits.OVRENL = 0;
IOCON1bits.OVRDAT = 0b00;
IOCON1bits.FLTDAT = 0b00;
IOCON1bits.CLDAT = 0b00;
IOCON1bits.SWAP = 0;
IOCON1bits.OSYNC = 0;

//set primary time base, edge aligned, independent duty cycles
PWMCON1 = 0x0000;
//PWMCON1bits.IUE = 1;  //privremeno
//config faults
FCLCON1 = 0x0003;

//config trigger & interrupt
//IFS5bits.PWM1IF = 0;
//IEC5bits.PWM1IE = 1;  
//TRGCON1 = 0x0000;       //triger na svakom periodu pwm-a
//TRIG1 = 0x0000;         //triger je na pocetku pwm-a
//PWMCON1bits.TRGIEN = 0;


}

使用主时基将它们设置为独立模式。

1 个答案:

答案 0 :(得分:0)

我通过在更新两个PWM通道的占空比和频率之前禁用PWM(通过清零PTEN位)解决了这个问题,然后立即启用它。结果是可能有一个PWM脉冲比更新时应该稍短,但不会发生互补通道的脉冲重叠。 这更像是一种解决方法,而不是真正的解决方案,但它符合我的目的。