STM32無刷電機源碼幾資料 STM32方波霍爾按鍵調速電路原理圖如下:
單片機源程序如下:
- /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
- * File Name : main.c
- * Author : MCD Application Team
- * Version : V2.0.1
- * Date : 06/13/2008
- * Description : Main program body
- ********************************************************************************
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
- * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
- * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
- * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
- * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- #include "main.h"
- #include "CAN\can.h"
- #define ADC1_DR_Address ((u32)0x4001244C)
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- static vu32 TimingDelay = 0;
- volatile struct {
- unsigned Key : 1;
- unsigned CalSpeed : 1;
- unsigned Sec : 1;
- unsigned Fault : 1;
- }Flags;
- unsigned int DesiredSpeed=500;//目標速度
- unsigned int ActualSpeed;//實際速度
- unsigned int pwm=500;//pwm值
- unsigned int T3Count;
- unsigned int ActualSpeed5[3];
- vu16 ADC_DMABUF;
- unsigned int AveActualSpeed;
- unsigned char AveNum;
- unsigned char j;
-
- float kp=2,ki=1,kd=0.0;//pid參數
- int ek=0,ek1=0,ek2=0;
- float duk;
- int du;
- int ekSpeed=0;
- u16 motor_statue=0;
- u16 startcnt=0;
- extern u16 My_PWM;//pwm值
- extern u16 Hall,time; //霍爾 ,計時時間
- extern bool Direction; //電機旋轉方向
- extern u32 timer3value,timer3avg;
- extern void TIM1_Configuration1(void);
- int state,state1,state2,state3,counter1,counter2,counter3,speed_1,aim_speed,check_run,set_speed;
- short ADC_ConvertedValue[5]={0,0,0,0,0};
- TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- ErrorStatus HSEStartUpStatus;
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void CalculateDC(int u,int y);
- void TIM3_Configuration1(void);
- void TIM2_Configuration1(void);
- void TIM4_Configuration1(void);
- void SysTick_Configuration(void);
- void DMA_Configuration1(void);
- void ADC_Configuration1(void);
- int pid(int nonce,int aim);
- u8 key_con(void);
- /* Private functions ---------------------------------------------------------*/
- /*******************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- #ifdef DEBUG
- debug();
- #endif
- int i;
- u8 keytemp=0;//按鍵臨時值
- u8 flagccw=0;
- NVIC_InitTypeDef NVIC_InitStructure;
- /* System Clocks Configuration */
- RCC_Configuration();//系統時鐘配置
-
- /* NVIC configuration */
- NVIC_Configuration();//中斷配置
- DMA_Configuration1();//DMA
- ADC_Configuration1();//ADC配置
-
- TIM1_Configuration1();//TIM1配置
- TIM2_Configuration1(); //TIM2配置
- TIM4_Configuration1();//TIM4配置
-
-
- GPIO_Configuration(); //GPIO配置
- SysTick_Configuration();//滴答時鐘配置
- SysTick_CounterCmd(SysTick_Counter_Enable);//使能配置
- aim_speed=1000; //pwm值
- set_speed=1000;//設置速度值
- timer3avg=0;
- Hall=5;
- while (1)
- {
-
- keytemp= key_con(); //按鍵讀取
- if(keytemp==1) //啟動
- {
- TIM_Cmd(TIM1, ENABLE);
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
-
- startcnt=0;
-
- }
- if(keytemp==2)//停止
- {
- TIM_Cmd(TIM1, DISABLE);
- TIM_CtrlPWMOutputs(TIM1, DISABLE);
-
- }
- if(keytemp==3) //速度加
- {
- if(time >100)
- {
- if(set_speed <5000)
- set_speed+=10;
- time =0;
- }
-
-
- }
- else if(keytemp==4)//速度減
- {
- if(time >100)
- {
- if(set_speed >200 )
- set_speed-=10;
- time =0;
- }
- }
- if(keytemp==5) //換方向
- {
- if(time >1800)
- {
- time=0;
- if(flagccw==0)
- {
- Direction=1;
-
- }
- else
- {
- Direction=0;
-
- }
- flagccw=~flagccw;
- }
-
- }
- if(startcnt<36) //換相6次后啟動
- {
- My_PWM = aim_speed;
- if(time>2)
- {
- Hall_SW();
- if(Hall==5) Hall=1;
- else if(Hall==1) Hall=3;
- else if (Hall==3) Hall=2;
- else if (Hall==2) Hall=6;
- else if (Hall==6) Hall=4;
- else if (Hall==4) Hall=5;
- time=0;
- startcnt++;
- }
-
- }
- else
- {
- startcnt=37;
- My_PWM = aim_speed;
-
-
-
- }
-
- }
- }
- int pid(int nonce,int aim) //PID運算
- {
- static int ek_2=0;
- static int ek_1=0;
- static int ek=0;
- int uk;
-
- ek=aim-nonce; //aim 設置值 nonce 實際值
- uk=kp*(ek-ek_1)+ki*ek+kd*(ek-2*ek_1+ek_2);
- ek_2=ek_1;
- ek_1=ek;
- if(uk<100) //限幅度
- {
- uk=100;
- }
- if(uk>3000)
- {
- uk=3000;
- }
-
- return (uk);
- }
- /*******************************************************************************
- * 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)
- {
- // Enable Prefetch Buffer
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- // Flash 2 wait state
- FLASH_SetLatency(FLASH_Latency_2);
- // HCLK = SYSCLK
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- // PCLK2 = HCLK
- RCC_PCLK2Config(RCC_HCLK_Div1);
- // PCLK1 = HCLK/2
- RCC_PCLK1Config(RCC_HCLK_Div2);
- // 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)
- {
- }
- }
- else
- {
- // If HSE fails to start-up, the application will have wrong clock configuration.
- // User can add here some code to deal with this error
- // Go to infinite loop
- while (1)
- {
- }
- }
- }
- /*******************************************************************************
- * Function Name : key_con
- * Description : Configures the Motor operation mode
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- u8 key_con(void)//讀取按鍵參數
- {
- static u8 key;
- key=0;
- if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0))
- {
- key=2;
-
- }
- if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13))
- {
- key=1;
-
- }
- if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14))
- {
- key=3;
-
- }
- if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15))
- {
- key=4;
-
- }
- if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7))
- {
- key=5;
-
- }
- return key;
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : Configures the different GPIO ports.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD ,ENABLE);
- //按鍵輸入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_7|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//IO口上拉
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//IO口
- // GPIO_Init(GPIOB, &GPIO_InitStructure);
- //led輸出
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- /* 配置Hall接口IO */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /*霍爾信號線中斷配置*/
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource8);
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line6|EXTI_Line7|EXTI_Line8;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- /*PA8,PA9,PA10 為上半橋臂*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9 | GPIO_Pin_10 ;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /*PB13,PB14,PB15 為下半橋臂*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
-
- /*******************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configure the nested vectored interrupt controller.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- #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
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- #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)
- {
- }
- }
- #endif
- /*******************************************************************************
- * Function Name : SysTick_Config
- * Description : Configure a SysTick Base time to 10 ms.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SysTick_Configuration(void)
- {
- /* Select AHB clock(HCLK) as SysTick clock source */
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
-
- /* Set SysTick Priority to 3 */
- NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 5, 0);
-
- /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
- SysTick_SetReload(900000);
-
- /* Enable the SysTick Interrupt */
- SysTick_ITConfig(ENABLE);
- }
- /*******************************************************************************
- * Function Name : Delay
- * Description : Inserts a delay time.
- * Input : nCount: specifies the delay time length (time base 10 ms).
- * Output : None
- * Return : None
- *******************************************************************************/
- void Delay(u32 nCount)
- {
- TimingDelay = nCount;
- /* Enable the SysTick Counter */
- //SysTick_CounterCmd(SysTick_Counter_Enable);
-
- while(TimingDelay != 0)
- {
- }
- /* Disable the SysTick Counter */
- //SysTick_CounterCmd(SysTick_Counter_Disable);
- /* Clear the SysTick Counter */
- SysTick_CounterCmd(SysTick_Counter_Clear);
- }
- /*******************************************************************************
- * Function Name : Decrement_TimingDelay
- * Description : Decrements the TimingDelay variable.
- * Input : None
- * Output : TimingDelay
- * Return : None
- *******************************************************************************/
- void Decrement_TimingDelay(void)
- {
- if (TimingDelay != 0x00)
- {
- TimingDelay--;
- }
- }
- /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
復制代碼
所有資料51hei提供下載:
LKZN_STM32方波霍爾按鍵調速 -PID.rar
(1.72 MB, 下載次數: 92)
|