可用单片机编写,首先在网上找个产生SPWM数据表的软件,然后利用这些数据做个数组储存在单片机里。然后根据这些数据确定定时器定时时间即可输出SPWM。单相逆变H桥所需的SPWM
比较简单,三相比较复杂。可以用IR2184S半桥驱动电路作为逆变器驱动,因为是单输入所以程序写起来比较简单直观。
#include<pich>
#include<pic16f685h>
#include<mathh>
//__CONFIG(0X002A); //配置字,禁止欠压复位BOR,16M外部晶振,上电延时使能,禁止看门狗
__CONFIG(HS&PWRTEN&WDTEN);
//打开看门狗,选择高速晶振,上电延时复位,掉电复位使能,代码保护
float sin_am, sin_l,sin_d;//浮点数,幅值变量,临时变量,临时变量
bit sin_up;//sin函数正负半周标志
void intdelay();
void DELAY();
void init_start();
void CCP_start();
const unsigned char sina_[]={7 ,15 ,22 ,29 ,37 ,44 ,51 ,58 ,65 ,72 ,79 ,86 ,92 ,99 ,106 ,112 ,118
,124 ,130 ,136 ,141 ,147 ,152 ,157 ,162 ,166 ,171 ,175 ,179 ,183 ,186 ,190 ,193 ,196 ,198 ,201 ,203
,205 ,206 ,208 ,209 ,210 ,210 ,211 ,211 ,211 ,210 ,210 ,209 ,208 ,206 ,205 ,203 ,201 ,198 ,196 ,193
,190 ,186 ,183 ,179 ,175 ,171 ,166 ,162 ,157 ,152 ,147 ,141 ,136 ,130 ,124 ,118 ,112 ,106 ,99 ,92
,86 ,79 ,72 ,65 ,58 ,51 ,44 ,37 ,29 ,22 ,15 ,7 ,0
};
const unsigned char sinb_[]={7 ,15 ,22 ,29 ,37 ,44 ,51 ,58 ,65 ,72 ,79 ,86 ,92 ,99 ,106 ,112 ,118
,124 ,130 ,136 ,141 ,147 ,152 ,157 ,162 ,166 ,171 ,175 ,179 ,183 ,186 ,190 ,193 ,196 ,198 ,201 ,203
,205 ,206 ,208 ,209 ,210 ,210 ,211 ,211 ,211 ,210 ,210 ,209 ,208 ,206 ,205 ,203 ,201 ,198 ,196 ,193
,190 ,186 ,183 ,179 ,175 ,171 ,166 ,162 ,157 ,152 ,147 ,141 ,136 ,130 ,124 ,118 ,112 ,106 ,99 ,92
,86 ,79 ,72 ,65 ,58 ,51 ,44 ,37 ,29 ,22 ,15 ,7 ,0
};
//---------------------------------------------------------------------------------------
//const unsigned char sina_[]={4, 7, 11, 15, 18, 22, 26, 29, 33, 37, 40, 44, 47, 51,
// 55, 58, 62, 65, 69, 72, 76, 79, 82, 86, 89, 92, 96, 99,
// 102, 106, 109, 112, 115, 118, 121, 124, 127, 130,
// 133, 136, 138, 141, 144, 147, 149, 152, 154, 157,
// 159, 162, 164, 166, 169, 171, 173, 175, 177, 179,
// 181, 183, 185, 186, 188, 190, 191, 193, 194, 196,
// 197, 198, 200, 201, 202, 203, 204, 205, 206, 206,
// 207, 208, 208, 209, 209, 210, 210, 210, 211, 211,
// 211, 211, 211, 211, 211, 210, 210, 210, 209, 209,
// 208, 208, 207, 206, 206, 205, 204, 203, 202, 201,
// 200, 198, 197, 196, 194, 193, 191, 190, 188, 186,
// 185, 183, 181, 179, 177, 175, 173, 171, 169, 166,
// 164, 162, 159, 157, 154, 152, 149, 147, 144, 141,
// 138, 136, 133, 130, 127, 124, 121, 118, 115, 112,
// 109, 106, 102, 99, 96, 92, 89, 86, 82, 79, 76, 72, 69, 65, 62, 58,
// 55, 51, 47, 44, 40, 37, 33, 29, 26, 22, 14, 7, 0, 7, 4, 0
//};
//----------------------------------------------------------------------------------
//const unsigned char sinb_[]={4, 7, 11, 15, 18, 22, 26, 29, 33, 37, 40, 44, 47, 51,
// 55, 58, 62, 65, 69, 72, 76, 79, 82, 86, 89, 92, 96, 99,
// 102, 106, 109, 112, 115, 118, 121, 124, 127, 130,
// 133, 136, 138, 141, 144, 147, 149, 152, 154, 157,
// 159, 162, 164, 166, 169, 171, 173, 175, 177, 179,
// 181, 183, 185, 186, 188, 190, 191, 193, 194, 196,
// 197, 198, 200, 201, 202, 203, 204, 205, 206, 206,
// 207, 208, 208, 209, 209, 210, 210, 210, 211, 211,
// 211, 211, 211, 211, 211, 210, 210, 210, 209, 209,
// 208, 208, 207, 206, 206, 205, 204, 203, 202, 201,
// 200, 198, 197, 196, 194, 193, 191, 190, 188, 186,
// 185, 183, 181, 179, 177, 175, 173, 171, 169, 166,
// 164, 162, 159, 157, 154, 152, 149, 147, 144, 141,
// 138, 136, 133, 130, 127, 124, 121, 118, 115, 112,
// 109, 106, 102, 99, 96, 92, 89, 86, 82, 79, 76, 72, 69, 65, 62, 58,
// 55, 51, 47, 44, 40, 37, 33, 29, 26, 22, 14, 7, 0, 7, 4, 0
//};
//--------------------------------------------------------------------------------------
//const unsigned char sinb_[]=
// {219, 215, 211, 207, 204, 200, 196, 192, 188, 184,
// 180, 177, 173, 169, 165, 162, 158, 154, 150, 147,
// 143, 139, 136, 132, 129, 125, 122, 118, 115, 112,
// 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 65, 63,
// 60, 57, 55, 52, 50, 47, 45, 43, 40, 38, 36, 34, 32, 30, 28, 26, 24, 23, 21,
// 19, 18, 16, 15, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 2,
// 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4,
// 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 21, 23, 24, 26, 28,
// 30, 32, 34, 36, 38, 40, 43, 45, 47, 50, 52, 55, 57, 60, 63, 65, 68, 71,
// 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 115,
// 118, 122, 125, 129, 132, 136, 139, 143, 147,
// 150, 154, 158, 162, 165, 169, 173, 177, 180,
// 184, 188, 192, 196, 205, 215, 219, 223, 215,
// 219, 223
//};
unsigned char sin_num;//SIN函数表查表变量
//===================================================================================
void main()
{
// CLRWDT();//清除看门狗
TRISB=0X00; //RB4RB5RB6RB7为输出
TRISC=0XCB; //11011001设置RC0RC1RC3RC4RC6RC7为输入,其它为输出
PORTB=0X00;
PORTC=0X00;
//--------------------------------------
OPTION=0X42; //使能PORTA上拉,RA2下降沿触发中断(上升:0X47),内部时钟,预分频器分配给TIMER0,分频比为1:8
sin_up=0; //正负半周SIN函数
sin_num=0; //脉宽周期调整计数器
PDC1=1; //死区延时05US
INTCON=0XE0; //11100000:GIE开总中断,timer0使能,
TMR2IE=1;
CCP1CON=0X8C; //10001100;
CCPR1L=0X00;
PR2=0XDF; //设置PWM的工作周期,16Mhz,PWM周期18khz
T2CON=0x04; // 00100100打开TMR2,且使其后分频为比1:5,预分频比为1
GIE=1; //开全局中断
while(1)
{
CLRWDT(); //清除看门狗
PDC1=1;
T2CON=0x04; // 00100100打开TMR2,且使其后分频为比1:5,预分频比为1,
TMR2IE=1; //打开定时器2中断使能
CCP1CON=0X8C; //10001100;
PR2=0XDF; //设置PWM的工作周期,16Mhz,PWM周期18khz
// DELAY();
}
}
//----------------------------------------------------------
//软件延时子程序/
void DELAY()
{
unsigned int i;
for(i=2000;i>0;i--);
}
//-------------中断服务程序------------------------------------
void interrupt int1(void)
{
if(TMR2IF&&TMR2IE)
{
TMR2IF=0;
CLRWDT();
if(sin_num==88){sin_num=0;sin_up=!sin_up;}
if((sin_up==0)&&(sin_num==0)) //低频正半周
{
CCPR1L=0;
CCP1CON=0;
RC4=RC5=0;
RB7=0;
RB6=!RB6;
}
if((sin_up==1)&&(sin_num==0)) //低频负半周
{
CCPR1L=0;
CCP1CON=0;
RC4=RC5=0;
RB6=0;
RB7=!RB7;
}
if(sin_up==0)
{
CCP1CON=0X8C;
CCPR1L=sinb_[sin_num]; //高频正半周
sin_num++;
}
if(sin_up==1)
{
CCP1CON=0X8C;
CCPR1L=sina_[sin_num]; //高频负半周
sin_num++;
}
}
}
linjinfeng_job@163com 能发给一份SPWM的程序吗
//Fc载波Hz;Fs产生的Hz;v精度,8位为256,9位为512 ;time某时刻;
//PI=31415926535897932384626433832795
void SPWM_FUN(long Fc,long Fs,int v,int time,double SPWM){
time=time%Fc;
for(int i=0;i<3;i++){
(i+SPWM)=sin(i2 PI / 3 + PI Fs time/ Fc) v;
}
}
用51单片机啊,大致看你电路觉得用计算法不知道性能如何。而且还要调压,精度要求还蛮高,功率还比较大的,个人觉得有难度。仅仅spwm的话倒好说,倒是后面有些难度。如果单片机计算性能达不到,大不了不用软件调制。如果是毕业设计,建议跟你导师谈谈用什么方案可行。
你要控制舵机是不是?
你用定时器0做时钟源,(255-计数初值)256频率12=系统频率
我现在没有完整程序 给你部分配置
void pwm1_1(void) //PWM的初始化
{
PCA0MD=0x04; //PCA clock is T0 over
PCA0CPL0=8; //32~8
PCA0CPM0=0x42; //设置左轮为8为PWM输出
PCA0CPH0=8;
PCA0CN=0x40; //允许PCA工作
}
void t01_init(void)//Bound rate:2k
{
TCON=0x50; //0101 0000
TMOD=0x52; //T1,T0:timer
CKCON=0x01; //4
TH0=21; //PCA 50Hz 时钟
TL0=21;
}
以上就是关于前辈,我今年也参加电子设计大赛,SPWM程序还没头绪。全部的内容,包括:前辈,我今年也参加电子设计大赛,SPWM程序还没头绪。、PIC发SPWM控制IR2110的原理跟程序、51单片机产生SPWM波控制IR2110的程序怎么写啊等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)