F28035 输出三相 SPWM脉冲程序
时间: 2024-05-13 15:14:36 浏览: 103
以下是基于F28035的三相SPWM脉冲程序示例:
首先,需要定义SPWM参数,例如谐波数量、采样频率等。这些参数可以根据具体应用进行调整。
```C
#define PWM_FREQUENCY 20000
#define DEAD_BAND 0
#define MAX_DUTY_CYCLE 0.95
#define MIN_DUTY_CYCLE 0.05
#define PWM_COUNTS ((float)CPUFREQ/PWM_FREQUENCY)
#define DEAD_BAND_COUNTS ((float)CPUFREQ*DEAD_BAND/1000)
#define PWM_PERIOD_COUNTS (PWM_COUNTS-2*DEAD_BAND_COUNTS)
#define ONE_CYCLE_COUNTS (6*PWM_PERIOD_COUNTS)
#define HALF_CYCLE_COUNTS (ONE_CYCLE_COUNTS/2)
#define SAMPLE_FREQUENCY (PWM_FREQUENCY*20)
#define SAMPLE_COUNTS ((float)CPUFREQ/SAMPLE_FREQUENCY)
#define HARMONIC_COUNT 25
#define PI 3.14159265358979323846
#define TWO_PI 6.28318530717958647692
```
接着,需要定义SPWM函数,用于生成三相SPWM波形。该函数的实现过程如下:
```C
void SPWM(float phase_shift, float amplitude, float frequency, float *sine_table, float *sine_table_index, float *duty_cycle)
{
float count = 0;
float sine_value = 0;
float sine_table_step = (float)SAMPLE_FREQUENCY/frequency;
float sine_table_index_step = sine_table_step/SAMPLE_COUNTS;
float phase_shift_counts = phase_shift/360*ONE_CYCLE_COUNTS;
float sine_table_index_offset = phase_shift_counts/sine_table_index_step;
float amplitude_counts = amplitude*PWM_PERIOD_COUNTS/3.3;
float harmonic_amplitude[HARMONIC_COUNT+1] = {0, 0.6667, 0.2, 0.1333, 0.1, 0.08, 0.0667, 0.0571, 0.05, 0.0444, 0.04, 0.0364, 0.0333, 0.0308, 0.0286, 0.0267, 0.025, 0.0235, 0.0222, 0.0211, 0.02, 0.019, 0.0182, 0.0174, 0.0167};
for(int i = 0; i < PWM_PERIOD_COUNTS; i++)
{
count++;
if(count > sine_table_step)
{
count = 1;
*sine_table_index += sine_table_index_step;
if(*sine_table_index >= SAMPLE_COUNTS)
{
*sine_table_index = 0;
}
}
sine_value = sine_table[(int)(*sine_table_index+sine_table_index_offset)];
*duty_cycle = 0.5*(amplitude_counts*sine_value/PWM_PERIOD_COUNTS);
for(int j = 1; j <= HARMONIC_COUNT; j++)
{
sine_value = sine_table[(int)(*sine_table_index+sine_table_index_offset)*j];
*duty_cycle += harmonic_amplitude[j]*(amplitude_counts*sine_value/PWM_PERIOD_COUNTS);
}
if(*duty_cycle < MIN_DUTY_CYCLE)
{
*duty_cycle = MIN_DUTY_CYCLE;
}
else if(*duty_cycle > MAX_DUTY_CYCLE)
{
*duty_cycle = MAX_DUTY_CYCLE;
}
}
}
```
最后,需要在主函数中调用SPWM函数,生成三相SPWM波形:
```C
float sine_table[SAMPLE_COUNTS];
float sine_table_index = 0;
float duty_cycle = 0;
for(int i = 0; i < SAMPLE_COUNTS; i++)
{
sine_table[i] = sin(TWO_PI*i/SAMPLE_COUNTS);
}
while(1)
{
SPWM(0, 3.3, PWM_FREQUENCY, sine_table, &sine_table_index, &duty_cycle);
EPwm1Regs.CMPA.half.CMPA = duty_cycle*PWM_PERIOD_COUNTS;
EPwm2Regs.CMPA.half.CMPA = duty_cycle*PWM_PERIOD_COUNTS;
EPwm3Regs.CMPA.half.CMPA = duty_cycle*PWM_PERIOD_COUNTS;
}
```
上述代码中,通过调用SPWM函数生成三相SPWM波形,并将其写入EPwm1Regs、EPwm2Regs和EPwm3Regs寄存器中,以输出到三相驱动器中。
阅读全文