文档库 最新最全的文档下载
当前位置:文档库 › STM32F4的三相SPWM逆变器源码

STM32F4的三相SPWM逆变器源码

//该源码是基于STM32F4的三相SPWM逆变器的C程序
#include "stm32f4_discovery.h"
#include "timer.h"
#include "FONT.h"
#include "LCD12864.h"
#include "outputdata.h"
#include
#include "arm_math.h"


uint16_t SYSCLK = 5000; //定时0.2ms
uint16_t PWM_Frequency = 5000; //5KHz

#define PusleValue 0
#define DEADTIME 84 //0.5us

extern uint16_t Phase_A;
extern uint16_t Phase_B;
extern uint16_t Phase_C;


uint16_t TIM5_PrescalerValue = 0;
uint16_t TIM5_Period = 0;
uint16_t TimerPeriod = 0;
uint32_t QuadCNT = 0;
uint32_t PusleCNT = 0;
extern uint16_t ITCNT;
uint16_t CurCNTValue = 0;
uint16_t LastCNTValue = 0;
uint16_t DetaCNTValue = 0;
uint16_t CNTValue = 0;
extern int OutData[4];
extern double Sine_Table[360];
extern double Theta;
extern NVIC_InitTypeDef NVIC_InitStructure; //定义指向嵌套中断向量控制器初始化结构体的指针

arm_pid_instance_f32 *SX_SetVal;
extern __IO uint32_t Frequency;

uint16_t CycleNum = 0;
float32_t FreqSet = 50;
arm_pid_instance_f32 *SX_Points; //定义一个能指向PID结构体的指针


void TIM2_Config(void)
{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //定义指向基本定时器初始化结构体的指针
// TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //Active TIM2


// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10 | GPIO_Pin_11; //TIM2全部通道(GPIOB)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*----------------------------------------------------------------*/
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_TIM2);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource15,GPIO_AF_TIM2);
TIM_TimeBaseStructure.TIM_Prescaler = 0; //时钟预分频数
TIM_TimeBaseStructure.TIM_Period = 0; //自动重装载寄存器的值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //采样分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方式

TIM_DeInit(TIM2);
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// //编码配置
// TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12,
// TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising上升沿捕获
// TIM_ICStructInit(&TIM_ICInitStructure);
// TIM_ICInitStructure.TIM_ICFilter = 0; //比较滤波器
// TIM_ICInit(TIM2, &TIM_ICInitStructure);

TIM_Cmd(T

IM2, ENABLE); //开启TIM5时钟

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //通道TIM5
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //副优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断通道
NVIC_Init(&NVIC_InitStructure);
}
/*PWMControlOutput*/
void THphasePWM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //定义指向基本定时器初始化结构体的指针
TIM_OCInitTypeDef TIM_OCInitStructure; //定义指向定时器输出初始化结构体的指针
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //定义指向定时器外部关闭与死区初始化结构体的指针

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //开TIM1时钟

TIM_CHA_Config();
TimerPeriod = (uint32_t)(SystemCoreClock/PWM_Frequency)-1;

/* Time Base configuration 配置PWM的周期*/
TIM_TimeBaseStructure.TIM_Prescaler = 0; //TIM1时钟168MHz
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM1配置为计数方式 TimerPeriod = (SystemCoreClock / 17570) - 1;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod; //定时器计数频率
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //TIM1时钟分频系数
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //TIM1时钟重复次数

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //基本定时器初始化

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM1设置为PWM输出模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM1输出状态使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //TIM1输出互补状态使能

TIM_OCInitStructure.TIM_Pulse = PusleValue;; //TIM1的PWM占空比设置为0
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //配置PWM成输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //配置PWMN成输出极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //配置PWM空闲模式
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

TIM_OC1Init(TIM1, &TIM_OCInitStructure); //初始化两组互补的PWM
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //初始化两组互补的PWM
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //初始化两组互补的PWM

/* 死区相关的设置*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = DEADTIME; //死区时间为DEADTIME/SystemCoreClock
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; //开启外部break功能。
TIM_BDTRInitStructure

.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;

TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure); //配置死区功能
}
/*TIM_CHA_Config*/
void TIM_CHA_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义指向GPIO初始化结构体的指针

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA |
RCC_AHB1Periph_GPIOB|
RCC_AHB1Periph_GPIOE , ENABLE); //开GPIOA、GPIOB时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | //PA8 CHA1
GPIO_Pin_10; //PA10 CHA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //无上拉电阻和下拉电阻
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PE11 CHA2
GPIO_Init(GPIOE, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|
GPIO_Pin_13 | //PB13 CHA1N
GPIO_Pin_14 | //PB14 CHA2N
GPIO_Pin_15 ; //PB15 CHA3N
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* 将相关I/O口映射到AF1*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_TIM1);
}

float32_t PIDCalc(float32_t Freq) //PID计算
{
float32_t pid_out;

SX_Points->state[0] = FreqSet- Freq; //计算偏差

pid_out = SX_Points->Kp*SX_Points->state[0]
-SX_Points->Ki*SX_Points->state[1]
+SX_Points->Kd*SX_Points->state[2]; //PID计算

SX_Points->state[1] = SX_Points->state[0]; //上一次偏差
SX_Points->state[2] = SX_Points->state[1]; //上上一次偏差

return pid_out; //输出PID控制量
}


/*TIM2中断服务程序*/
void TIM2_IRQHandler(void)
{
static uint16_t Counter_sine1 = 0; //A相
static uint16_t Counter_sine2 = 120; //滞后A相120度
static uint16_t Counter_sine3 = 240; //超前A相120度

static uint8_t D5_CNT = 0;
static uint8_t D3_CNT = 7;
static uint8_t D6_CNT = 14;

if ( TIM_GetITStatus(TIM2,TIM_IT_Update)) //判断中断来源
{
// TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);

TIM_SetCompare1(TIM1,(uint32_t)Sine_Table[Counter_sine1]); //A相PA8、PB13
if(Counter_sine1>=359){
Counter_sine1 = 0;
if(D5_CNT>=19){
D5_CNT = 0;
STM_EVAL_LEDToggle(LED5); //RED
}
D5_CNT++;
}
TIM_SetCompare2(TIM1,(uint32_t)Sine_Table[Counter_sine2]); //B相PE11

、PB14
if(Counter_sine2>=359){
if(D3_CNT>=19){
D3_CNT = 0;
STM_EVAL_LEDToggle(LED3); //Orange
}
D3_CNT++;
Counter_sine2 = 0;
}
TIM_SetCompare3(TIM1,(uint32_t)Sine_Table[Counter_sine3]); //C相PB10、PB15
if(Counter_sine3>=359){
if(D6_CNT>=19){
D6_CNT = 0;
STM_EVAL_LEDToggle(LED6); //Blue
}
D6_CNT++;
Counter_sine3 = 0;
}
Counter_sine1++;
Counter_sine2++;
Counter_sine3++;
CycleNum++;

}

TIM_ClearITPendingBit(TIM2,TIM_FLAG_Update); //清除TIM3中断标志
}










相关文档