/***************普通SPWM程序************************/
#ifndef _NORMAL_SPWM_H
#define _NORMAL_SPWM_H
//SPWM 表结构体 三项公用一个表
typedef struct _SPWM_table
{
Uint16 TableSize; //表大小 即表中所有数据
Uint16 SpwmSize; //SPWM表大小
volatile Uint16 *p_SPWM_A; //A相指针
volatile Uint16 *p_SPWM_B; //B相指针
volatile Uint16 *p_SPWM_C; //C相指针
Uint16 *p_HeadTable; //表头指针 指向SPWM表
}SPWM_TABLE;
extern SPWM_TABLE g_SPWM_Table; //全局SPWM表
void InitSpwm(void);
void StartSpwm(void);
interrupt void ISR_T1UFINT_NORMAL_FUNC(void);
void CalcSpwmWithSym(float32 a/*调制比*/,float32 w_Hz/*调制频率*/,float32 z_Hz/*载波频率*/);
void CalcSpwmWithImSym(float32 a/*调制比*/,Uint16 w_Hz/*调制频率*/,Uint32 z_Hz/*载波频率*/);
void CalcSpwmWithArea(float32 a/*调制比*/,Uint16 w_Hz/*调制频率*/,Uint32 z_Hz/*载波频率*/);
#endif
源文件
#include "DSP281x.h"
#include "SPWM.h"
#include "float.h"
#include "math.h"
#define MAX_BUF 400
#define PI 3.1415926
Uint16 g_spwm_data[MAX_BUF]; //表的数据存储
SPWM_TABLE g_SPWM_Table; //全局SPWM表
//SPWM初始化程序
void InitSpwm(void)
{
g_SPWM_Table.p_HeadTable = g_spwm_data ; //指向数据表
g_SPWM_Table.TableSize = MAX_BUF; //存储表的大小
EALLOW;
PieVectTable.T1UFINT=&ISR_T1UFINT_NORMAL_FUNC;
EDIS;
IER|=M_INT2; //开中断2
PieCtrlRegs.PIEIER2.bit.INTx6=1; //开下益中断
EvaRegs.EVAIFRA.bit.T1UFINT=1; //清楚中断标志
PieCtrlRegs.PIEACK.bit.ACK2 = 1; //响应同组中断
}
void StartSpwm(void)
{
EvaRegs.EVAIMRA.bit.T1UFINT = 1; //打开下益 中断
}
/对称规则采样法
void CalcSpwmWithSym(float32 a/*调制比*/,float32 w_Hz/*调制频率*/,float32 z_Hz/*载波频率*/)
{
Uint16 tmp_PR; //T1周期值
volatile Uint16 i,n,*p;
float32 m;
m = z_Hz/w_Hz ; //求出载波比
g_SPWM_Table.SpwmSize =(Uint16)m;
tmp_PR = g_T1_Clk /(2*z_Hz); //计算出其周期值
p=g_SPWM_Table.p_HeadTable; //得到数据表头指针
for(i=0;i<(Uint16)m;i++)
{
n=tmp_PR*(0.5-0.5*a*sin((i+0.75)*2*PI/m));
*p=n;
p++;
}
}
//不对称规则采样法
void CalcSpwmWithImSym(float32 a/*调制比*/,Uint16 w_Hz/*调制频率*/,Uint32 z_Hz/*载波频率*/)
{
Uint16 tmp_PR; //T1周期值
volatile Uint16 i,n,*p;
float32 m;
m = z_Hz/w_Hz ; //求出载波比
g_SPWM_Table.SpwmSize =(Uint16)m;
tmp_PR = g_T1_Clk /(2*z_Hz); //计算出其周期值
p=g_SPWM_Table.p_HeadTable; //得到数据表头指针
for(i=0;i<(Uint16)m;i++)
{
n=tmp_PR*(0.5-0.25*a*(sin((i+0.25)*2*PI/m)+sin((i+0.75)*2*PI/m)));
*p=n;
p++;
}
}
//面积法
void CalcSpwmWithArea(float32 a/*调制比*/,Uint16 w_Hz/*调制频率*/,Uint32 z_Hz/*载波频率*/)
{
//Uint16 tmp_PR; //T1周期值
volatile Uint16 i,n,*p;
float32 m,n1,n2;
m = z_Hz/w_Hz ; //求出载波比
g_SPWM_Table.SpwmSize =(Uint16)m;
//tmp_PR = g_T1_Clk /(2*z_Hz); //计算出其周期值
p=g_SPWM_Table.p_HeadTable; //得到数据表头指针
n=m;
m/=2; //除去一半 计算半波
n1=(float32)g_T1_Clk/(8.0*m*w_Hz); // 计算首相
n2=(float32)g_T2_Clk/(8.0*PI*w_Hz)*a;
for(i=0;i<n;i++)
{
*p=n1-n2*(cos(i*PI/m)-cos((i+1)*PI/m));
p++;
}
}
//中断程序
interrupt void ISR_T1UFINT_NORMAL_FUNC(void)
{
static Uint16 cnt=0 ; //计数
EvaRegs.CMPR1 = g_spwm_data[cnt];
cnt++;
if(cnt>=g_SPWM_Table.SpwmSize)
cnt = 0;
EvaRegs.EVAIFRA.bit.T1UFINT=1;
PieCtrlRegs.PIEACK.bit.ACK2 = 1;
EINT;
}
基本所有变频器采用的都是以上算法中的一种,优化后所产生的正弦波信号,优化以上算法可以得到更加接近于正弦波的交流信号波形。