久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4574|回復: 3
收起左側

STM32方波霍爾按鍵調速控制無刷電機源碼與原理圖

[復制鏈接]
ID:411433 發表于 2018-10-17 22:41 | 顯示全部樓層 |閱讀模式
STM32無刷電機源碼幾資料 STM32方波霍爾按鍵調速電路原理圖如下:
0.jpg

單片機源程序如下:
  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name          : main.c
  3. * Author             : MCD Application Team
  4. * Version            : V2.0.1
  5. * Date               : 06/13/2008
  6. * Description        : Main program body
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/

  15. /* Includes ------------------------------------------------------------------*/
  16. #include "stm32f10x_lib.h"
  17. #include "main.h"
  18. #include "CAN\can.h"

  19. #define ADC1_DR_Address    ((u32)0x4001244C)
  20. /* Private typedef -----------------------------------------------------------*/
  21. /* Private define ------------------------------------------------------------*/
  22. /* Private macro -------------------------------------------------------------*/
  23. /* Private variables ---------------------------------------------------------*/
  24. static vu32 TimingDelay = 0;

  25. volatile struct {
  26.                 unsigned Key           :                1;
  27.                 unsigned CalSpeed :         1;
  28.                 unsigned Sec      :                1;
  29.                 unsigned Fault           :                1;
  30.                 }Flags;

  31. unsigned int DesiredSpeed=500;//目標速度
  32. unsigned int ActualSpeed;//實際速度
  33. unsigned int pwm=500;//pwm值
  34. unsigned int T3Count;
  35. unsigned int ActualSpeed5[3];
  36. vu16 ADC_DMABUF;
  37. unsigned int AveActualSpeed;
  38. unsigned char AveNum;
  39. unsigned char j;
  40.                  

  41. float kp=2,ki=1,kd=0.0;//pid參數
  42. int ek=0,ek1=0,ek2=0;
  43. float duk;
  44. int du;
  45. int ekSpeed=0;
  46. u16 motor_statue=0;
  47. u16  startcnt=0;
  48. extern u16 My_PWM;//pwm值
  49. extern u16 Hall,time; //霍爾 ,計時時間
  50. extern bool Direction; //電機旋轉方向
  51. extern u32 timer3value,timer3avg;
  52. extern void TIM1_Configuration1(void);
  53. int state,state1,state2,state3,counter1,counter2,counter3,speed_1,aim_speed,check_run,set_speed;
  54. short ADC_ConvertedValue[5]={0,0,0,0,0};
  55. TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
  56. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  57. TIM_OCInitTypeDef  TIM_OCInitStructure;
  58. EXTI_InitTypeDef EXTI_InitStructure;
  59. ErrorStatus HSEStartUpStatus;  

  60. /* Private function prototypes -----------------------------------------------*/
  61. void RCC_Configuration(void);
  62. void GPIO_Configuration(void);
  63. void NVIC_Configuration(void);
  64. void CalculateDC(int u,int y);
  65. void TIM3_Configuration1(void);
  66. void TIM2_Configuration1(void);
  67. void TIM4_Configuration1(void);
  68. void SysTick_Configuration(void);
  69. void DMA_Configuration1(void);
  70. void ADC_Configuration1(void);
  71. int pid(int nonce,int aim);
  72. u8 key_con(void);
  73. /* Private functions ---------------------------------------------------------*/
  74. /*******************************************************************************
  75. * Function Name  : main
  76. * Description    : Main program.
  77. * Input          : None
  78. * Output         : None
  79. * Return         : None
  80. *******************************************************************************/
  81. int main(void)
  82. {
  83.         #ifdef DEBUG
  84.         debug();
  85.         #endif
  86.           int i;
  87.          u8 keytemp=0;//按鍵臨時值
  88.         u8 flagccw=0;
  89.                  NVIC_InitTypeDef NVIC_InitStructure;
  90.         /* System Clocks Configuration */
  91.         RCC_Configuration();//系統時鐘配置
  92.      
  93.         /* NVIC configuration */
  94.         NVIC_Configuration();//中斷配置
  95.   DMA_Configuration1();//DMA
  96.         ADC_Configuration1();//ADC配置
  97.        
  98.         TIM1_Configuration1();//TIM1配置                  
  99.         TIM2_Configuration1(); //TIM2配置          
  100.         TIM4_Configuration1();//TIM4配置       

  101.        
  102.        
  103.         GPIO_Configuration(); //GPIO配置
  104.         SysTick_Configuration();//滴答時鐘配置
  105.         SysTick_CounterCmd(SysTick_Counter_Enable);//使能配置

  106.         aim_speed=1000; //pwm值
  107.         set_speed=1000;//設置速度值
  108.         timer3avg=0;
  109.   Hall=5;
  110.         while (1)
  111.         {
  112.                
  113.    keytemp= key_con(); //按鍵讀取
  114.   if(keytemp==1) //啟動
  115.         {
  116.                     TIM_Cmd(TIM1, ENABLE);
  117.                     TIM_CtrlPWMOutputs(TIM1, ENABLE);
  118.                        
  119.                                 startcnt=0;
  120.                
  121.         }       
  122.         if(keytemp==2)//停止
  123.         {
  124.                      TIM_Cmd(TIM1, DISABLE);
  125.                       TIM_CtrlPWMOutputs(TIM1, DISABLE);
  126.                
  127.         }
  128.   if(keytemp==3) //速度加
  129.         {
  130.                 if(time >100)
  131.                 {
  132.                           if(set_speed <5000)
  133.                                  set_speed+=10;
  134.                                 time =0;
  135.                 }
  136.                  
  137.                        
  138.         }
  139. else if(keytemp==4)//速度減
  140. {
  141.                  if(time >100)
  142.                 {
  143.                           if(set_speed >200 )
  144.                                  set_speed-=10;
  145.                                 time =0;
  146.                 }
  147. }
  148. if(keytemp==5) //換方向
  149. {
  150.                 if(time >1800)
  151.                 {
  152.                         time=0;
  153.                         if(flagccw==0)
  154.                 {
  155.                          Direction=1;
  156.                
  157.                 }
  158.                 else
  159.                 {
  160.                           Direction=0;
  161.                           
  162.                 }
  163.                 flagccw=~flagccw;
  164.                 }
  165.           
  166. }

  167.   if(startcnt<36)  //換相6次后啟動
  168.         {
  169.                 My_PWM = aim_speed;
  170.         if(time>2)
  171.          {
  172.                 Hall_SW();
  173.                  if(Hall==5) Hall=1;
  174.                  else if(Hall==1) Hall=3;
  175.                  else if (Hall==3) Hall=2;
  176.                  else if (Hall==2) Hall=6;
  177.                  else if (Hall==6) Hall=4;
  178.                  else if (Hall==4) Hall=5;
  179.                 time=0;
  180.                   startcnt++;
  181.         }
  182.        
  183.         }
  184.   else
  185.         {
  186.                 startcnt=37;
  187.                 My_PWM = aim_speed;
  188.   

  189.                
  190.         }


  191.        
  192.         }
  193. }

  194. int pid(int nonce,int aim)  //PID運算
  195. {
  196.         static int ek_2=0;
  197.         static int ek_1=0;
  198.         static int ek=0;

  199.         int uk;
  200.        
  201.         ek=aim-nonce;  //aim  設置值 nonce 實際值

  202.         uk=kp*(ek-ek_1)+ki*ek+kd*(ek-2*ek_1+ek_2);
  203.         ek_2=ek_1;
  204.         ek_1=ek;
  205.         if(uk<100)  //限幅度
  206.         {
  207.                 uk=100;
  208.         }
  209.         if(uk>3000)
  210.         {
  211.                  uk=3000;
  212.         }
  213.                
  214.         return (uk);
  215. }

  216. /*******************************************************************************
  217. * Function Name  : RCC_Configuration
  218. * Description    : Configures the different system clocks.
  219. * Input          : None
  220. * Output         : None
  221. * Return         : None
  222. *******************************************************************************/
  223. void RCC_Configuration(void)
  224. {
  225.     ErrorStatus HSEStartUpStatus;

  226.     // RCC system reset(for debug purpose)
  227.     RCC_DeInit();

  228.     // Enable HSE
  229.     RCC_HSEConfig(RCC_HSE_ON);

  230.     // Wait till HSE is ready
  231.     HSEStartUpStatus = RCC_WaitForHSEStartUp();

  232.     if(HSEStartUpStatus == SUCCESS)
  233.     {
  234.         // Enable Prefetch Buffer
  235.         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  236.         // Flash 2 wait state
  237.         FLASH_SetLatency(FLASH_Latency_2);

  238.         // HCLK = SYSCLK
  239.         RCC_HCLKConfig(RCC_SYSCLK_Div1);

  240.         // PCLK2 = HCLK
  241.         RCC_PCLK2Config(RCC_HCLK_Div1);

  242.         // PCLK1 = HCLK/2
  243.         RCC_PCLK1Config(RCC_HCLK_Div2);

  244.         // PLLCLK = 8MHz * 9 = 72 MHz
  245.         RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  246.         // Enable PLL
  247.         RCC_PLLCmd(ENABLE);

  248.         // Wait till PLL is ready
  249.         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  250.         {
  251.         }

  252.         // Select PLL as system clock source
  253.         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  254.         // Wait till PLL is used as system clock source
  255.         while(RCC_GetSYSCLKSource() != 0x08)
  256.         {
  257.         }
  258.     }
  259.     else
  260.     {
  261.         // If HSE fails to start-up, the application will have wrong clock configuration.
  262.         // User can add here some code to deal with this error

  263.         // Go to infinite loop
  264.         while (1)
  265.         {
  266.         }
  267.     }


  268. }
  269. /*******************************************************************************
  270. * Function Name  : key_con
  271. * Description    : Configures the Motor operation mode
  272. * Input          : None
  273. * Output         : None
  274. * Return         : None
  275. *******************************************************************************/
  276. u8 key_con(void)//讀取按鍵參數
  277. {
  278.          static u8 key;
  279.         key=0;
  280.          if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0))
  281.          {
  282.                  key=2;
  283.                  
  284.          }
  285.           if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13))
  286.          {
  287.                  key=1;
  288.                  
  289.          }
  290.           if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14))
  291.          {
  292.                  key=3;
  293.                  
  294.          }
  295.           if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15))
  296.          {
  297.                  key=4;
  298.                  
  299.          }
  300.           if(!GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7))
  301.          {
  302.                  key=5;
  303.                  
  304.          }
  305.          return key;
  306. }
  307. /*******************************************************************************
  308. * Function Name  : GPIO_Configuration
  309. * Description    : Configures the different GPIO ports.
  310. * Input          : None
  311. * Output         : None
  312. * Return         : None
  313. *******************************************************************************/
  314. void GPIO_Configuration(void)
  315. {
  316.         GPIO_InitTypeDef GPIO_InitStructure;  

  317.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD ,ENABLE);
  318.         //按鍵輸入
  319.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_7|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15 ;         
  320.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//IO口上拉
  321.   GPIO_Init(GPIOC, &GPIO_InitStructure);   
  322. //        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2  ;           
  323. //  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//IO口
  324. //  GPIO_Init(GPIOB, &GPIO_InitStructure);   
  325.         //led輸出
  326.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  327.          GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;
  328.          GPIO_Init(GPIOD, &GPIO_InitStructure);  
  329.          
  330.           /* 配置Hall接口IO */
  331.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
  332.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  333.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  334.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  335.        
  336.         /*霍爾信號線中斷配置*/
  337.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);
  338.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7);
  339.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource8);

  340.         EXTI_InitStructure.EXTI_Line = EXTI_Line6|EXTI_Line7|EXTI_Line8;
  341.         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  342.         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  343.         EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  344.         EXTI_Init(&EXTI_InitStructure);       

  345.          /*PA8,PA9,PA10 為上半橋臂*/
  346.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9 | GPIO_Pin_10 ;
  347.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  348.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  349.           GPIO_Init(GPIOA, &GPIO_InitStructure);

  350.           /*PB13,PB14,PB15 為下半橋臂*/
  351.           GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
  352.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  353.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  354.           GPIO_Init(GPIOB, &GPIO_InitStructure);

  355. }


  356. /*******************************************************************************
  357. * Function Name  : NVIC_Configuration
  358. * Description    : Configure the nested vectored interrupt controller.
  359. * Input          : None
  360. * Output         : None
  361. * Return         : None
  362. *******************************************************************************/
  363. void NVIC_Configuration(void)
  364. {
  365.         NVIC_InitTypeDef NVIC_InitStructure;
  366.   
  367.         #ifdef  VECT_TAB_RAM  
  368.                 /* Set the Vector Table base location at 0x20000000 */
  369.                 NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  370.         #else  /* VECT_TAB_FLASH  */
  371.                 /* Set the Vector Table base location at 0x08000000 */
  372.                 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  373.         #endif

  374.         /* Configure one bit for preemption priority */
  375.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  376.   


  377.         NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
  378.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  379.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  380.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ENABLE;
  381.         NVIC_Init(&NVIC_InitStructure);          

  382.        

  383.           
  384.        
  385.         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
  386.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  387.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  388.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  389.         NVIC_Init(&NVIC_InitStructure);                 
  390.                   
  391.         NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
  392.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  393.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  394.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  395.         NVIC_Init(&NVIC_InitStructure);                         

  396.         NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
  397.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  398.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  399.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  400.         NVIC_Init(&NVIC_InitStructure);                 

  401. }

  402. #ifdef  DEBUG
  403. /*******************************************************************************
  404. * Function Name  : assert_failed
  405. * Description    : Reports the name of the source file and the source line number
  406. *                  where the assert_param error has occurred.
  407. * Input          : - file: pointer to the source file name
  408. *                  - line: assert_param error line source number
  409. * Output         : None
  410. * Return         : None
  411. *******************************************************************************/
  412. void assert_failed(u8* file, u32 line)
  413. {
  414.   /* User can add his own implementation to report the file name and line number,
  415.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  416.   /* Infinite loop */
  417.   while (1)
  418.   {
  419.   }
  420. }
  421. #endif

  422. /*******************************************************************************
  423. * Function Name  : SysTick_Config
  424. * Description    : Configure a SysTick Base time to 10 ms.
  425. * Input          : None
  426. * Output         : None
  427. * Return         : None
  428. *******************************************************************************/
  429. void SysTick_Configuration(void)
  430. {
  431.     /* Select AHB clock(HCLK) as SysTick clock source */
  432.     SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
  433.    
  434.     /* Set SysTick Priority to 3 */
  435.     NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 5, 0);
  436.    
  437.     /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
  438.     SysTick_SetReload(900000);
  439.    
  440.     /* Enable the SysTick Interrupt */
  441.     SysTick_ITConfig(ENABLE);
  442. }

  443. /*******************************************************************************
  444. * Function Name  : Delay
  445. * Description    : Inserts a delay time.
  446. * Input          : nCount: specifies the delay time length (time base 10 ms).
  447. * Output         : None
  448. * Return         : None
  449. *******************************************************************************/
  450. void Delay(u32 nCount)
  451. {
  452.   TimingDelay = nCount;

  453.   /* Enable the SysTick Counter */
  454.   //SysTick_CounterCmd(SysTick_Counter_Enable);
  455.   
  456.   while(TimingDelay != 0)
  457.   {
  458.   }

  459.   /* Disable the SysTick Counter */
  460.   //SysTick_CounterCmd(SysTick_Counter_Disable);

  461.   /* Clear the SysTick Counter */
  462.   SysTick_CounterCmd(SysTick_Counter_Clear);
  463. }

  464. /*******************************************************************************
  465. * Function Name  : Decrement_TimingDelay
  466. * Description    : Decrements the TimingDelay variable.
  467. * Input          : None
  468. * Output         : TimingDelay
  469. * Return         : None
  470. *******************************************************************************/
  471. void Decrement_TimingDelay(void)
  472. {
  473.   if (TimingDelay != 0x00)
  474.   {
  475.     TimingDelay--;
  476.   }
  477. }

  478. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
復制代碼

所有資料51hei提供下載:
LKZN_STM32方波霍爾按鍵調速 -PID.rar (1.72 MB, 下載次數: 92)
回復

使用道具 舉報

ID:407876 發表于 2018-10-18 08:28 | 顯示全部樓層
樓主,好貼。
回復

使用道具 舉報

ID:341045 發表于 2018-10-25 15:48 | 顯示全部樓層
謝謝樓主分享。
回復

使用道具 舉報

ID:165979 發表于 2020-1-3 09:01 | 顯示全部樓層
想下來看看
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 人妖av| 久久久成| 久久亚洲国产精品 | 337p日本欧洲亚洲大胆 | 一区二区不卡高清 | 国产在线观看一区二区三区 | 免费一区二区 | av免费网| 99re国产精品 | 国产精品日韩一区二区 | 国产欧美精品一区二区色综合朱莉 | 伊人网综合在线观看 | 久草网址| 五月婷婷丁香 | 99热在这里只有精品 | 91亚洲一区| 天堂久久久久久久 | 国产免费一区二区三区 | 国产乱码精品一区二区三区中文 | 亚洲第一天堂无码专区 | 国产成人综合在线 | 中文字幕一区在线观看视频 | 国产精品久久久久久久久免费樱桃 | av激情在线 | 国产99视频精品免视看9 | 美女一级黄 | 亚洲欧美在线观看 | 日韩精品在线一区 | 殴美成人在线视频 | 不卡一区二区三区四区 | 久久一级| 神马久久久久久久久久 | 日本小电影在线 | 亚洲一区二区av | 亚洲精品福利视频 | 国产视频二区在线观看 | 最新中文字幕在线 | 久久99久久99精品免视看婷婷 | 久久精品国产v日韩v亚洲 | 超黄视频网站 | 精品欧美色视频网站在线观看 |