《三種SPWM算法源碼》由會員分享,可在線閱讀,更多相關《三種SPWM算法源碼(4頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、這個是頭文件 spwm. h
I viz I" ■!■ ■]” —> \I“ ■】" 1" ?]" %fx %Lr /
I I "
/*************** 普八通 SP VM ttifndef NORMAL S PWM H #defi ne _NORMAL_S PWM_H 〃SPWM 表結(jié)構(gòu)體三項公用一個表 typ edef struct _SP WM_table
Ui ntl6 TableSize; 〃表大小即表中所有數(shù)據(jù)
Ui ntl6 Sp wmSize;
//SPWM
表大小
volatile Ui ntl6 *p_S PWM_A;
//A相指針
2、volatile Ui ntl6 *p_S PWM_B;
〃B相指針
volatile Ui ntl6 *p_SP WM_C;
//C相指針
Ui nt 16 *p_HeadTable;
針 .
//表頭指 我向SDWM先
}SP WM_TABLE;
extern SPW_TABLE g_SPW_Table; // 全局 SPW 表
void In itS pwm(void);
void StartS pwm(void);
in terru pt void ISR_T1UFINT_NORMAL_FUNC(void);
void
3、 CalcSpwmWithSym(float32 a/*調(diào)制比 */, float32 w_Hz/* 調(diào)制頻率 */, float32 z_Hz/*載波頻 率 */);
void CalcSpwmWithlmSym(float32 a/*調(diào)制比 */, Uintl6 w_Hz/* 調(diào)制頻率 */, Uint32 z_Hz/*載波
頻率*/);
void CalcSpwmWithArea(float32 a/*調(diào)制比 */, Uintl6 w_Hz/* 調(diào)制頻率 */, Uint32 z_Hz/*載波頻率 */);
tten dif
源文件
tti nclude 〃DS P281x.
4、 h〃
tti nclude 〃SP WM. h〃
ttin elude "float. h〃 ttin elude "math. h〃
ttdefi ne MAX_BUF 400
ttdefi ne PI 3. 1415926
Ui ntl6 g_sp wm_data[MAX_BUF]; // 表的數(shù)據(jù)存儲 SPW_TABLE g_SPW_Table; // 全局 SPWM 表
//SPW初始化程序
void InitSpwm(void)
g_SPW_Table. p_HeadTable = g_spwm_data ; // 指向數(shù)據(jù)表 g_SPW_Table. Table
5、Size = MAX_BUF; // 存儲 表的大小
EALLOW;
PieVectTable. T1UF1NT=&1SR T1UF1NT NORMAL FUNC; EDIS;
開下益中斷
〃清楚中斷標志
〃響應同組中斷
1ER | =M_1NT2; 〃開中斷 2 PieCtrlRegs. P1E1ER2. bit. 1NTx6=1; // EvaRegs. EVA1FRA. bit. T1UF1NT=1;
PieCtrlRegs. PIEACK. bit. ACK2 = 1; ) void StartSpwm(void)
打開下益中斷
EvaRegs. EVA1MRA. b
6、it. T1UF1NT = 1; // 5
〃對稱規(guī)則采樣法
void Cal cSpwmW i t hSym (f 1 oat 32 a/* 調(diào)制比 */, float32 w_Hz/* 調(diào)制頻率 */, fl oat 32 z_Hz/* 載波頻 率 */) (
Uintl6 tmp_PR; //Tl 周期值
volatile Uintl6 i,n, *p;
float32 m;
m = z_Hz/w_Hz ; //求出載波比 g_SPWM_Table. SpwmSize =(Uintl6)m;
tmp_PR = g_Tl_Clk /(2*z_Hz); 〃計算出其周期值
p=
7、g_SPW_Table. p_HeadTable; 〃得到數(shù)據(jù)表頭指針 for (i=0;i<(Uintl6)m;i++)
? n=tmp_PR* (0. 5-0. 5*a*sin((i+0. 75) *2*Pl/m));
*p=n;
P++; )}
〃不對稱規(guī)則采樣法
void CalcSpwmWithlmSym(float32 a/*調(diào)制比 */, Uintl6 w_Hz/* 調(diào)制頻率 */, Uint32 z_Hz/* 載波頻率 */)
Uintl6 tmp_PR; 〃T1 周期值
volatile Uintl6 i,n, *p;
float32 m;
m = z_Hz
8、/w_Hz ; // 求出載波比 g_SPWM_Table. SpwmSize = (Uintl6)m;
tmp_PR = g_Tl_Clk /(2*z_Hz); 〃計算出其周期值
p=g_SPW_Table, pJeadTable; // 得到數(shù)據(jù)表頭指針 for (i=0; i<(Uintl6)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/*調(diào)制比 */, Uintl
9、6 w_Hz/* 調(diào)制頻率 */, Uint32 z_Hz/*載波頻率*/)
//Uintl6 tmp_PR; 〃T1 周期值
volatile Uintl6 i,n, *p;
float32 m, nl, n2;
m = z_Hz/w_Hz ; //求出載波比
g_SPWM_Table. SpwmSize =(Uintl6)m;
//tmp_PR = g_Tl_Clk /(2*z_Hz); 〃計算出其周期值 p=g_SPW_Table. P_HeadTable; // 得到數(shù)據(jù)表頭指 針
n=m;
m/=2; 〃除去一半計算半波
nl=(float32)g_Tl_Clk/(
10、8. 0*m*w_Hz) ; // 計算首相
n2=(float32)g_T2_Clk/(8. 0*PI*w_Hz)*a;
for(i=0;i=g_SPWM_Table. SpwmSize) ent = 0;
EvaRegs. EVAIFRA. bit. TlUFINT=l;
PieCtrlRegs. PIEACK. bit. ACK2 = 1; EINT;