《三種SPWM算法源碼》由會員分享,可在線閱讀,更多相關(guān)《三種SPWM算法源碼(4頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、這個是頭文件 spwm. hIviz 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_tableUi ntl6 TableSize; 表大小即表中所有數(shù)據(jù)Ui ntl6 Sp wmSize;/SPWM表大小volatile Ui ntl6 *p_S PWM_A;/A相指針volatile Ui ntl6 *p_S PWM_B;B相指針volatile Ui ntl6 *p_SP WM_C;/C
2、相指針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 CalcSpwmWithSym(float32 a/*調(diào)制比 */, float32 w_Hz/* 調(diào)制頻率 */, float32 z_Hz/*載波頻 率 */);void CalcSpwmWithlmSym(float32a
3、/*調(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. htti nclude SP WM. httin elude float. h ttin elude math. httdefi ne MAX_BUF 400ttdefi ne PI 3. 1415926Ui ntl6 g_sp wm_dataMAX_BU
4、F; / 表的數(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. TableSize = MAX_BUF; / 存儲 表的大小EALLOW;PieVectTable. T1UF1NT=&1SR T1UF1NT NORMAL FUNC; EDIS;開下益中斷清楚中斷標志響應(yīng)同組中斷1ER | =M_1NT2; 開中斷 2 PieCtrlRegs. P1E1ER2. bit. 1NTx6=1;
5、/ EvaRegs. EVA1FRA. bit. T1UF1NT=1;PieCtrlRegs. PIEACK. bit. ACK2 = 1; ) void StartSpwm(void)打開下益中斷EvaRegs. EVA1MRA. bit. 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
6、m;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ù)表頭指針 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/* 載波
7、頻率 */)Uintl6 tmp_PR; T1 周期值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=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 Ca
8、lcSpwmWithArea(float32 a/*調(diào)制比 */, Uintl6 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/(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;