求助!PIC16f877产生的PWM.
include
#include
void CCP1_init(void)
{
CCPR1L=0x19; //通过设CCPR1L:及CCP1CON(bit4,bit5)确定高电平时间,
CCP1CON=0x0f;
INTCON=0x00; //关闭总中断及外围中断
PR2=0x31; //16位,设定周期,即频率,设定的值与频率大小成反比
//通过周期的倒数计算时间,单位us,再转为16进制,或直接输入10进制的值
TRISC=0xfb; //设RC2为PWM输出
T2CON=0x04; //开启TMR2,预分频为1,bit2=1为TIMER2使能位,bit1:bit0为预分频
}
void main(void)
{
CCP1_init(); //PWM初始化
while (1)
{
;
}
}
这个是我的半桥程序,有哪位大侠能帮我看看啊!!!
//spwm调制,逆变器,
#include
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0X3F32);
const uchar sinTab[200]={ //调制度M=0.9;正弦波代码数组
0,1,2,3,4,5,6,7,8,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31,32,33,33,34,35,36,37,38,39,
40,41,41,42,43,44,45,45,46,47,
48,48,49,50,51,51,52,53,53,54,
55,55,56,56,57,58,58,59,59,60,
60,61,61,62,62,62,63,63,64,64,
64,65,65,65,65,66,66,66,66,66,
67,67,67,67,67,67,67,67,67,67,
67,67,67,67,67,67,67,67,67,
67,66,66,66,66,66,65,65,65,65,
64,64,64,63,63,62,62,62,61,61,
60,60,59,59,58,58,57,56,56,55,
55,54,53,53,52,51,51,50,49,48,
48,47,46,45,45,44,43,42,41,41,
40,39,38,37,36,35,34,33,33,32,
31,30,29,28,27,26,25,24,23,22,
21,20,19,18,17,16,15,14,13,12,
11,10,8,7,6,5,4,3,2,1,0
};
/*基波50Hz,载波20KHz,半周期内的采样点数为100,则pwm的周期=1/20ms=50us*/
/*SPWM初始化*/
uchar B_D=0,i;
void init()
{
TRISB=0X28; //
PR2=149; //SPWM为50uS=150*4*(1/12)
CCP1CON=0XBd; //spwm模式选择,半桥1011,1101,A、c高有效,b、d低有效
CCPR1L=0; //占空比为0
PWM1CON=0X00; //
TMR2IF=0;
T2CON=0X04; //不分频,使能TMR2
}
void tmr2()
{
if(TMR2IF)
{
if(i==200)
{
i=0;
B_D=!B_D;
}
if(B_D)
{
TRISB=0X20; //B...C
}
else
{
TRISB=0X08; //D...A
}
PR2=149; //SPWM为50uS=150*4*(1/12)
CCPR1L=sinTab[i];
TMR2IF=0;
i++;
}
}
void main ()
{
init();
while(1)
{
tmr2();
}
}
CCPR1L=0x19; //通过设CCPR1L:及CCP1CON(bit4,bit5)确定高电平时间,
这个怎么算它的高电平,看不懂公式