最近用PIC16F716产生SPWM波,但一开机总是烧H桥。貌似是程序有错,麻烦各位大侠帮帮忙,看下什么地方有
问题。
PIC16F716的PWM模块工作在半桥模式,H桥一个桥臂是互为相反的SPWM波(该桥臂一直在做高频切换),另一桥
臂是互为相反的50HZ方波。timer2的后分频比是1:5,表格里有40个脉冲宽度,SPWM的周期是50US。主程序的
while循环里是做过压和欠压保护以及过流和短路保护的。50HZ的方波里也加入1ms死区时间(烧H桥与这个时间
太长有关吗)。下面是程序,帮忙看看,谢谢啊~~
#include
#define LED_CTRL_PORT RA2 ////////////////
#define SD_OUT_PORT RB6 ////////////////
#define SD_IN_PORT RB0
#define LEFT_UP_CTRL_PORT RB1
#define LEFT_DOWN_CTRL_PORT RB2
#define RIGHT_UP_CTRL_PORT RB3
#define RIGHT_DOWN_CTRL_PORT RB5
#define HZ50_PORT_MASK 0x06 //RB1,RB2
#define SPWM_PORT_MASK 0x28 // RB5 ,RB3
#define SD_IN_PORT_MASK 0x01 // RB0
#define LED_PORT_MASK 0x04 // RA2
#define SD_OUT_PORT_MASK 0x40 //RB6 ////////////////
#define VOL_PROTECT_NUM 1000
#define CUR_PROTECT_NUM 500
#define FM_CTRL_PORT RB4
__CONFIG(0xFF72);
unsigned char LeftUpCtrlNum=0,LeftDownCtrlNum=1;
unsigned char VoltageProtect=0,CurrentProtect=0;
unsigned int PortectNum=0;
volatile unsigned char SinNum=0;
static unsigned char SinTab[]={8,24,39,54,68,83,96,110,122,134,145,
155,164,172,179,185,189,193,195,197,
197,195,193,189,185,179,172,164,155,
145,134,122,110,96,83,68,54,39,24,8};
void interrupt timer1()
{
if(TMR1IF==1)
{
TMR1IF=0;
TMR1H=0xEC;
TMR1L=0x77;
//50HZ 死区时间 start
LeftUpCtrlNum++;
if(LeftUpCtrlNum==9)
{
LEFT_UP_CTRL_PORT=0;
}
else if(LeftUpCtrlNum==20)
{
LeftUpCtrlNum=0;
LEFT_UP_CTRL_PORT=1;
}
LeftDownCtrlNum++;
if(LeftDownCtrlNum==11)
{
LEFT_DOWN_CTRL_PORT=1;
}
else if(LeftDownCtrlNum==20)
{
LeftDownCtrlNum=0;
LEFT_DOWN_CTRL_PORT=0;
}
//50HZ 死区时间 end
//过流或短路保护 start
PortectNum++;
if(CurrentProtect)
{
if(PortectNum==CUR_PROTECT_NUM)
{
PortectNum=0;
LED_CTRL_PORT=~LED_CTRL_PORT;
FM_CTRL_PORT=~FM_CTRL_PORT;
}
}
if(VoltageProtect)
{
if(PortectNum==VOL_PROTECT_NUM)
{
PortectNum=0;
LED_CTRL_PORT=~LED_CTRL_PORT;
FM_CTRL_PORT=~FM_CTRL_PORT;
}
}
//过流或短路保护 end
}
//占空比调整
if(TMR2IF==1)
{
TMR2IF=0;
SinNum++;
if(SinNum>=40)
{
SinNum=0;
}
CCPR1L=SinTab[SinNum];
T2CON=0x24;
}
}
main(void)
{
unsigned int i;
unsigned char ADCResult;
INTCON=0;
TRISA=0xff;
// TRISB=0xff;
ADCON1=0x04;
ADCON0=0x81;
TRISA&=~(LED_PORT_MASK);
TRISB=SPWM_PORT_MASK | SD_IN_PORT_MASK ;
T1CON=0x00;
TMR1H=0xEC;
TMR1L=0x77;
TMR1IF=0;
T1CON=0x01;
PR2=0xf9;
CCP1CON=0x8C; //10001100;
CCPR1L=SinTab[0];
PWM1CON=0x19;
TMR2IF=0; //Timer2 中断标志位清零
T2CON=0x24; //0010 0100预分频1,后分频5,使能timer2
PIE1=0x03;
INTCON=0xC0;
TRISB&=~SPWM_PORT_MASK;
FM_CTRL_PORT=0;
LED_CTRL_PORT=1;
SD_OUT_PORT=0;
LEFT_UP_CTRL_PORT=1;
LEFT_DOWN_CTRL_PORT=0;
while(1)
{
if(!VoltageProtect)
{
i=50;
while(i>0)
i--;
ADCON0|=0x04;
while(ADCON0&0x04);
ADCResult=ADRES;
//电池电压检测
if(ADCResult<0x28 || ADCResult >0x42)
{
PortectNum=0;
VoltageProtect=1;
FM_CTRL_PORT=1;
LED_CTRL_PORT=0;
SD_OUT_PORT=1;
TRISB |=(SPWM_PORT_MASK|HZ50_PORT_MASK);
}
}
//过流或短路保护信号检测
if(!VoltageProtect && !SD_IN_PORT)/////////////////////
{
PortectNum=0;
CurrentProtect=1;
FM_CTRL_PORT=1;
LED_CTRL_PORT=0;
SD_OUT_PORT=1;
TRISB |=(SPWM_PORT_MASK|HZ50_PORT_MASK);
}
};
}