• 回复
  • 收藏
  • 点赞
  • 分享
  • 发新帖

求助!PIC16f877产生的PWM.

单片机产生的两路PWM只能是同步的吗?我想驱动半桥电路,怎么延时半个周期啊?
全部回复(17)
正序查看
倒序查看
2013-09-09 11:19
看到很多人做出来了,但具体怎么实现的都没说~
0
回复
2013-09-14 12:21
@xiaoxiaoluohen
看到很多人做出来了,但具体怎么实现的都没说~

include

#include  

void CCP1_init(void)

 {

     CCPR1L=0x19;             //通过设CCPR1L:CCP1CON(bit4,bit5)确定高电平时间,

                             

     CCP1CON=0x0f;           

                                  

     INTCON=0x00;             //关闭总中断及外围中断

     PR2=0x31;                //16位,设定周期,即频率,设定的值与频率大小成反比

                              //通过周期的倒数计算时间,单位us,再转为16进制,或直接输入10进制的值

     TRISC=0xfb;              //RC2PWM输出

     T2CON=0x04;              //开启TMR2,预分频为1,bit2=1TIMER2使能位,bit1:bit0为预分频

 }                              

void main(void)                                            

 {   

       CCP1_init();            //PWM初始化   

       while (1)

      {                                    

       ;                       

      }  

 }

0
回复
2013-09-14 12:22
@adengadeng
include#include voidCCP1_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为预分频 }                             voidmain(void)                                             {         CCP1_init();           //PWM初始化         while(1)     {                                          ;                            }   }
照上面做就行了
0
回复
2013-09-14 12:22
@adengadeng
照上面做就行了

include 加个#include

0
回复
2013-09-16 09:37
@adengadeng
照上面做就行了

谢谢!

这样是只产生了一路PWM吧?我还需要一路和他延时半个周期的,那个波形怎么产生啊?

0
回复
2013-09-20 13:50
@xiaoxiaoluohen
谢谢!这样是只产生了一路PWM吧?我还需要一路和他延时半个周期的,那个波形怎么产生啊?

上面的那个,结构上是不是有错误啊?

0
回复
ymhujiang
LV.4
8
2013-09-23 17:24
@adengadeng
include#include voidCCP1_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为预分频 }                             voidmain(void)                                             {         CCP1_init();           //PWM初始化         while(1)     {                                          ;                            }   }
看不懂,还得多学学源代码呀!
0
回复
sherryhe
LV.2
9
2013-09-26 17:15
@ymhujiang
看不懂,还得多学学源代码呀!
请问STM32F103RET7 能用吗?
0
回复
adengadeng
LV.3
10
2013-09-30 15:37
@xiaoxiaoluohen
谢谢!这样是只产生了一路PWM吧?我还需要一路和他延时半个周期的,那个波形怎么产生啊?

这只是个最简单的PWM单路输出初始化程序,不好意思,没注意看,我以为你只是要输出个PWM就行了.

0
回复
2013-10-01 18:05
@sherryhe
请问STM32F103RET7能用吗?
好多客户用的单片机是不一样的
0
回复
xurubo1983
LV.5
12
2013-10-07 12:36
@adengadeng
这只是个最简单的PWM单路输出初始化程序,不好意思,没注意看,我以为你只是要输出个PWM就行了.
CCP1CON和CCPR1L是如何设定值的?比如我要1KHZ的PWM,应该要如何设置?请指教?
0
回复
wubaoxue
LV.1
13
2013-10-14 09:04

这个是我的半桥程序,有哪位大侠能帮我看看啊!!!

//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();

      }

}

0
回复
wubaoxue
LV.1
14
2013-10-15 08:51
@adengadeng
include#include voidCCP1_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为预分频 }                             voidmain(void)                                             {         CCP1_init();           //PWM初始化         while(1)     {                                          ;                            }   }
要是产生SPWM还需要不断修改CCPR1L中的数值。不知道在SPWM半桥输出的时候,这个死区需要怎么设置?设置的时候需要注意些什么?不过我感觉在正弦波由正半周向负半轴切换的时候其脉冲宽度很窄,基本为零,是不是就不在需要进行死区设置了。哪位大师做过啊,能不能分享下。
0
回复
daihui713
LV.6
15
2013-10-29 17:28
@xiaoxiaoluohen
谢谢!这样是只产生了一路PWM吧?我还需要一路和他延时半个周期的,那个波形怎么产生啊?

只有一个CCP模块,那就只能用定时器互套再产生另一个PWM

0
回复
daihui713
LV.6
16
2013-10-29 17:40
@xurubo1983
CCP1CON和CCPR1L是如何设定值的?比如我要1KHZ的PWM,应该要如何设置?请指教?

PWM周期=(PR2+1)*4*Tosc*TMR2预分频

PWM高电平时间=CCPR1L:DC1B1:DC1B0*Tosc*TMR2预分频          DC1B(1:0)在CCP1CON中设定

0
回复
2017-04-18 10:32
@adengadeng
include#include voidCCP1_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为预分频 }                             voidmain(void)                                             {         CCP1_init();           //PWM初始化         while(1)     {                                          ;                            }   }

 CCPR1L=0x19;             //通过设CCPR1L:及CCP1CON(bit4,bit5)确定高电平时间, 

这个怎么算它的高电平,看不懂公式

0
回复
xapgy
LV.4
18
2017-04-27 15:46
@wubaoxue
这个是我的半桥程序,有哪位大侠能帮我看看啊!!!//spwm调制,逆变器,#include#defineucharunsignedchar#defineuintunsignedint__CONFIG(0X3F32);constucharsinTab[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初始化*/ucharB_D=0,i;voidinit(){  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++;    }}voidmain(){    init();    while(1)      {         tmr2();      }}
你的pic16f877a程序调试的怎么样了?我在论坛二手交易买的那个2000W逆变器单片机坏了能发个程序么?或者买你都可以
0
回复