本帖最后由 zeshoufx 于 2020-8-25 22:13 编辑
一、硬件
基于GD32303C-EVAL 开发板
timer0的pwm输出,PE8-PE13
二、svpwm
空间矢量脉宽调制(SVPWM)技术在电机驱动、变频技术等领域得到了广泛运用, 由于其直流侧电压利用率高且输出谐波含量低的优点,受到了开发人员的青睐。
三、MATLAB仿真
四、程序
/*
昆明电器科学研究所 张恩寿 2019年5月3日
*/
#include "bitband.h"
#include "led_config.h"
#include "key_lookup.h"
#include "systick.h"
#include "usart.h"
#include"arm_math.h"
#include<math.h>
void GPIO_config(void);
void TIM_config(void);
void DAC_config(void);
void bound_check(float *var);
uint8_t sec_idn(float angle);
void tim_cal(void);
timer_parameter_struct TIM_TimeBaseStructure;
timer_oc_parameter_struct TIM_OCInitStructure;
timer_break_parameter_struct TIM_BDTRInitStructure;
float PWM_F= 5000; // <- chang for PWM_F
float T_svm;
float theta1=0,theta2=120,theta3=240,Va,Vb,Vc,Val,Vbe,spc_angle,spc_mag; //<-- angle and signal declaration
float MI=1.0f;
float clock=120000000;
float ARR_val;
uint8_t sig_flag=0,sector;
float T1,T2,T0,T1n,T2n,T0n;
float32_t CMP1, CMP2, CMP3;
void GPIO_config()
{
rcu_periph_clock_enable(RCU_GPIOE);
rcu_periph_clock_enable(RCU_AF);
gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_MAX,GPIO_PIN_8);
gpio_pin_remap_config(GPIO_TIMER0_FULL_REMAP,ENABLE);
}
void TIM_config()
{
rcu_periph_clock_enable(RCU_TIMER0);
rcu_periph_clock_enable(RCU_TIMER2);
TIM_TimeBaseStructure.period = (uint16_t)ARR_val-1;
TIM_TimeBaseStructure.prescaler = 0;
TIM_TimeBaseStructure.clockdivision=TIMER_CKDIV_DIV1;
TIM_TimeBaseStructure.alignedmode = TIMER_COUNTER_EDGE;
TIM_TimeBaseStructure.counterdirection=TIMER_COUNTER_UP;
timer_init(TIMER0, &TIM_TimeBaseStructure);
timer_enable(TIMER0);
TIM_OCInitStructure.outputstate = TIMER_CCX_ENABLE;
TIM_OCInitStructure.outputnstate = TIMER_CCXN_ENABLE;
TIM_OCInitStructure.ocpolarity = TIMER_OC_POLARITY_HIGH;
TIM_OCInitStructure.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_channel_output_config(TIMER0,TIMER_CH_0,&TIM_OCInitStructure);
timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,T1);
timer_channel_output_config(TIMER0,TIMER_CH_1,&TIM_OCInitStructure);
timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,T2);
timer_channel_output_config(TIMER0,TIMER_CH_2,&TIM_OCInitStructure);
timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,T0);
// TIM_CtrlPWMOutputs(TIM1, ENABLE);
// TIM1->BDTR = 0xFF99;
TIM_BDTRInitStructure.breakpolarity=TIMER_BREAK_POLARITY_LOW;
TIM_BDTRInitStructure.breakstate=TIMER_BREAK_DISABLE;
TIM_BDTRInitStructure.deadtime=50;
TIM_BDTRInitStructure.ideloffstate=TIMER_IOS_STATE_ENABLE;
TIM_BDTRInitStructure.outputautostate=TIMER_OUTAUTO_ENABLE;
TIM_BDTRInitStructure.protectmode=TIMER_CCHP_PROT_OFF;
TIM_BDTRInitStructure.runoffstate=TIMER_ROS_STATE_ENABLE;
timer_break_config(TIMER0,&TIM_BDTRInitStructure);
timer_primary_output_config(TIMER0,ENABLE);
timer_auto_reload_shadow_enable(TIMER0);
TIM_TimeBaseStructure.period =4665;
TIM_TimeBaseStructure.prescaler = 0;
TIM_TimeBaseStructure.clockdivision=TIMER_CKDIV_DIV1;
TIM_TimeBaseStructure.counterdirection=TIMER_COUNTER_UP;
timer_init(TIMER2, &TIM_TimeBaseStructure);
timer_interrupt_enable(TIMER2,TIMER_INT_UP);
timer_enable(TIMER2);
nvic_irq_enable(TIMER2_IRQn,3,2);
}
void DAC_config()
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_DAC);
gpio_init(GPIOA,GPIO_MODE_AIN,GPIO_OSPEED_MAX,GPIO_PIN_4|GPIO_PIN_5);
dac_wave_bit_width_config(DAC0,DAC_WAVE_BIT_WIDTH_12);
dac_wave_bit_width_config(DAC1,DAC_WAVE_BIT_WIDTH_12);
dac_wave_mode_config(DAC0,DAC_WAVE_DISABLE);
dac_wave_mode_config(DAC1,DAC_WAVE_DISABLE);
dac_trigger_disable(DAC0);
dac_trigger_disable(DAC1);
dac_output_buffer_enable(DAC0);
dac_output_buffer_enable(DAC1);
dac_enable(DAC0);
dac_enable(DAC1);
}
void TIMER2_IRQHandler(void)
{
if(timer_interrupt_flag_get(TIMER2,TIMER_INT_FLAG_UP))
{
sig_flag =1;
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_UP);
}
}
void bound_check(float *var)
{
if(*var ==360)
{
*var = 0;
}
}
uint8_t sec_idn(float angle)
{
uint8_t sec_sig;
sec_sig = (angle/60 )+ 1;
if(sec_sig == 7)
{
sec_sig =6;
}
return sec_sig;
}
void tim_cal(void)
{
uint8_t ac_f =0;
T1 = sqrt(3)*T_svm*spc_mag*sinf((sector*PI/3)-spc_angle);
T2 = sqrt(3)*T_svm*spc_mag*sinf(spc_angle-(sector-1)*PI/3);
T0 = T_svm -T1 -T2;
}
五、结果
|