程序来源:FOC SDK2.0
说明:在FOC SDK2.0的基础上稍稍修改,并做了一些注释,主要是为了记录总结收获。删掉或者注释了一些暂时用不到的功能。
以下为main.c中的程序:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h" // STM32固件库头文件
#include "stm32f10x_MClib.h" // STM32电机库头文件
#include "MC_Globals.h" // 全局变量声明
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void RCC_Configuration(void);
/*******************************************************************************
* Function Name : main
* Description : 主函数
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
NVIC_Configuration(); // 初始化配置
RCC_Configuration();
GPIO_Configuration();
#ifdef THREE_SHUNT
SVPWM_3ShuntInit(); // 三电阻采样SVPWM初始化
#endif
#ifdef ENCODER // 编码器初始化
ENC_Init();
#ifdef OBSERVER_GAIN_TUNING
STO_StateObserverInterface_Init();
STO_Init();
#endif
#elif defined HALL_SENSORS //霍尔传感器初始化
HALL_HallTimerInit();
#ifdef OBSERVER_GAIN_TUNING
STO_StateObserverInterface_Init();
STO_Init();
#endif
#elif defined NO_SPEED_SENSORS //无感初始化
STO_StateObserverInterface_Init();
STO_Init();
#ifdef VIEW_ENCODER_FEEDBACK
ENC_Init();
#elif defined VIEW_HALL_FEEDBACK
HALL_HallTimerInit();
#endif
#endif
#ifdef BRAKE_RESISTOR //制动电阻初始化
MCL_Brake_Init();
#endif
TB_Init(); //系统滴答定时器初始化,500us一次中断
PID_Init(&PID_Torque_InitStructure, &PID_Flux_InitStructure, &PID_Speed_InitStructure);//PID初始化
KEYS_Init(); //按键操作初始化
DBGMCU_Config(DBGMCU_TIM1_STOP, ENABLE); //MCU处于调试模式时,TIM1停止工作,属于固件库函数
MCL_Init_Arrays(); //初始化母线电压和温度值
LCD_Init(); //LCD初始化
LCD_Clear(White);
LCD_SetTextColor(Blue);
LCD_SetBackColor(White);
Display_Welcome_Message();
while(1)
{
Display_LCD();
MCL_ChkPowerStage(); //检查逆变器状态:是否过温、欠压
KEYS_process(); //响应按键操作
switch (State)
{
//空闲模式
case IDLE:
break;
//初始化模式
case INIT:
MCL_Init(); //每次电机启动时执行电机控制初始化
TB_Set_StartUp_Timeout(3000); //等待3s
State = START; //转到开始模式
break;
//开始模式
case START:
//passage to state RUN is performed by startup functions;
break;
//运行模式
case RUN:
#ifdef ENCODER //各种情况错误状态指示
if(ENC_ErrorOnFeedback() == TRUE)
{
MCL_SetFault(SPEED_FEEDBACK);
}
#elif defined HALL_SENSORS
if(HALL_IsTimedOut())
{
MCL_SetFault(SPEED_FEEDBACK);
}
if (HALL_GetSpeed() == HALL_MAX_SPEED)
{
MCL_SetFault(SPEED_FEEDBACK);
}
#elif defined NO_SPEED_SENSORS
#endif
break;
//停止模式
case STOP:
TIM_CtrlPWMOutputs(TIM1, DISABLE);//禁止PWM输出
State = WAIT; //状态转到等待模式
#ifdef THREE_SHUNT //禁止电流采集功能
SVPWM_3ShuntAdvCurrentReading(DISABLE);
#endif
Stat_Volt_alfa_beta.qV_Component1 = Stat_Volt_alfa_beta.qV_Component2 = 0;
#ifdef THREE_SHUNT
SVPWM_3ShuntCalcDutyCycles(Stat_Volt_alfa_beta);
#endif
TB_Set_Delay_500us(2000); //延时1s
break;
//等待模式转到空闲模式
case WAIT:
if (TB_Delay_IsElapsed() == TRUE)
{
#ifdef ENCODER
if(ENC_Get_Mechanical_Speed() ==0)
{
State = IDLE;
}
#elif defined HALL_SENSORS
if (HALL_IsTimedOut())
{
State=IDLE;
}
#elif defined NO_SPEED_SENSORS
STO_InitSpeedBuffer();
State=IDLE;
#endif
}
break;
//错误模式,清除错误并转到空闲状态
case FAULT:
if (MCL_ClearFault() == TRUE)
{
if(wGlobal_Flags & SPEED_CONTROL == SPEED_CONTROL)
{
bMenu_index = CONTROL_MODE_MENU_1;
}
else
{
bMenu_index = CONTROL_MODE_MENU_6;
}
#if defined NO_SPEED_SENSORS
STO_InitSpeedBuffer();
#endif
State = IDLE;
wGlobal_Flags |= FIRST_START;
}
break;
default:
break;
}
}
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the TIM1 Pins.
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE); //打开GPIOC时钟
GPIO_DeInit(GPIOC); //GPIOC配置为缺省值
GPIO_StructInit(&GPIO_InitStructure); //初始化为缺省值,与上一个函数配套使用
/* 配置PC.06, PC.07, PC.08 and PC.09为推挽输出模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : 配置系统时钟为72MHz
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
// 选择PLL时钟源
// 倍频至72MHz
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : 中断向量表基地址选择
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
该程序完成了初始化和有限状态机的转换。
问题:
1. 如何转到RUN状态?
2.GPIOC作用?
加油(ง •_•)ง