STM32控制的高压无刷直流电机
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- #include "stm32f10x_MClib.h"
- #include "MC_Globals.h"
- //debug in RAM
- //#define VECT_TAB_RAM
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- EXTI_InitTypeDef EXTI_InitStructure;
- /* Private function prototypes -----------------------------------------------*/
- void NVIC_Configuration(void);
- void GPIO_Configuration(void);
- void RCC_Configuration(void);
- void PVD_Configuration(void);
- void TB_Init(void);
- void LCD_Dis_ZEQ(void);
- void Running_ZEQ(void);
- void InterruptConfig(void);
- void ExtOnConfig(void);
- extern volatile bool HallTimeOut;
- extern u16 Duty;
- extern u16 Duty1;
- extern u16 Duty2;
- extern u32 speed_integral;
- extern u32 hRotorFreq;
- extern u16 wGlobal_Flags;
- extern u8 bMenu_index;
- volatile bool PID_Control;
- volatile bool ReStartFlag=FALSE;
- volatile u8 StartTime=0;
- /*******************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- //u8 i=0,j=0,k=0;
- #ifdef DEBUG
- debug();
- #endif
- /*Core initialization*/
- /*NVIC first,before RCC initialization,use HSI,because RCC initialization also may have INT,GPIO last*/
- NVIC_Configuration();
- RCC_Configuration();
- PVD_Configuration();
- GPIO_Configuration();
- TB_Init(); //use systick as the timer base
- //hall timer TIMER2 initialization
- HALL_HallTimerInit();
- /*shunt channel*/
- #ifdef THREE_SHUNT
- SVPWM_3ShuntInit(); /* Current sensing by Three Shunt resistors */
- #elif defined ICS_SENSORS
- SVPWM_IcsInit();
- #endif
- //#if 0
- //PID parameters initialization
- PID_Init(&PID_Speed_InitStructure);
- KEYS_Init();
- LCD_Dis_ZEQ();
- StartTimer(TIMERDISPLAY,0);
- Running_ZEQ();
- //#endif
- //while(1);
- /* while(1)
- {
- switch(KEYS_Read())
- {
- case ONOFF:{LCD_Print(1,0,"Key ONOFF");LCD_Putchar(++i+0x30);break;}
- case PLUS:{LCD_Print(1,0,"Key PLUS");LCD_Putchar(++j+0x30);break;}
- case MINUS:{LCD_Print(1,0,"Key MINUS");LCD_Putchar(++k+0x30);break; }
- default:break;
- }
- }
- */
-
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : Configures the TIM1 Pins.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIOC clock */
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE);
- /*Reset CPIOC after system reset*/
- GPIO_DeInit(GPIOC);
- GPIO_StructInit(&GPIO_InitStructure);
-
- /* Configure PC0-PC3,PC10-PC12 Output push-pull for LCD Display */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3\
- | GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
- 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 : Configures the different system clocks.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- 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();
-
- 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 : PVD_Configuration
- * Description : Settup the Program Voltage Detect function
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void PVD_Configuration(void)
- {
- PWR_PVDLevelConfig(PWR_PVDLevel_2V8); //config the voltage threshold
- PWR_PVDCmd(ENABLE); //enable PVD
- EXTI_StructInit(&EXTI_InitStructure);
- EXTI_InitStructure.EXTI_Line = EXTI_Line16; //connect to Line16
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //enable PVD interrupt
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;//low voltage trigger interrupt
- EXTI_InitStructure.EXTI_LineCmd = ENABLE; //enable interrupt lin
- EXTI_Init(&EXTI_InitStructure);//initiation the EXTI
- }
- /*******************************************************************************
- * Function Name : TB_Init
- * Description : TimeBase peripheral initialization. The base time is set to
- * 1ms and the related interrupt is enabled
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void TB_Init(void)
- {
- /* Select AHB/8 clock(HCLK) as SysTick clock source */
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
- /* SysTick interrupt each 1ms with Core clock equal to 72MHz,1/72M*8*9000=0.001s */
- SysTick_SetReload(9000);
- /* Enable SysTick Counter */
- SysTick_CounterCmd(SysTick_Counter_Enable);
- /* Enable SysTick interrupt */
- SysTick_ITConfig(ENABLE);
- }
- /*******************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configures the Vector Table base address.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_DeInit();
- //if debug the code in RAM,must define VECT_TAB_RAM
- #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
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 4, 0);
- //hall input interrupt Priority must highest
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- // TIM1 update even interrupt
- NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /*******************************************************************************
- * Function Name : LCD_Dis _ZEQ
- * Description : LCD_Display
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void LCD_Dis_ZEQ(void)
- {
- LCD_Init();
- LCD_Clear();
- Display_Welcome_Message();
- }
- /*******************************************************************************
- * Function Name : Running_ZEQ
- * Description : While_running
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void Running_ZEQ(void)
- {
- while(1)
- {
- Display_LCD();
- KEYS_process();
- switch (State)
- {
- case IDLE: // Idle state
- break;
- case INIT:
- MCL_Init();
- if(StartTime>4) State=FAULT;
- else State = START;
- break;
-
- case START:
- //if(ReadTimer(TIMERSTARTUP)==WAIT) MCL_Init();
- //if(ReadTimer(TIMERSTARTUP)==OK) StopTimer(TIMERSTARTUP);
- break;
- case STOP: // motor stopped
- /* Main PWM Output Disable */
- TIM1_CtrlPWMOutputs(DISABLE);
- State = WAITING;
- StartTimer(TIMERDELAY,500);
- break;
-
- case WAITING: // waiting state
- TIM_SetCounter(TIM2, 0);
- Duty = 0;
- Duty1 = 0;
- Duty2 = 0;
- speed_integral = 0;
- TIM_Cmd(TIM2, DISABLE);
- TIM_Cmd(TIM3, DISABLE);
- TIM_ITConfig(TIM3, TIM_IT_Trigger, DISABLE);
- if (ReadTimer(TIMERDELAY)==OK)
- {
- #ifdef HALL_SENSORS
- if (hRotorFreq == 0)
- {
- State=IDLE;
- }
- #endif
- }
- break;
-
- case FAULT:
- TIM1_CtrlPWMOutputs(DISABLE);
- State = WAITING;
- StartTimer(TIMERDELAY,500);
- TIM_Cmd(TIM3, DISABLE);
- TIM_Cmd(TIM2, DISABLE);
- bMenu_index=ERR_MENU;
- break;
-
- default:
- break;
- }
- }
- }
- #ifdef DEBUG
- /*******************************************************************************
- * Function Name : assert_failed
- * Description : Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * Input : - file: pointer to the source file name
- * - line: assert_param error line source number
- * Output : None
- * Return : None
- *******************************************************************************/
- void assert_failed(u8* file, u32 line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- //printf("Wrong parameters value: file %s on line %d\r\n", file, line);
- }
- }
- #endif
- /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
STM32控制的高压无刷直流电机程序.rar
(246.62 KB, 下载次数: 51)
|