找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2704|回复: 0
收起左侧

适合STM32全系的低压有刷直流马达驱动驱动实例源码

[复制链接]
ID:371707 发表于 2018-7-14 21:38 | 显示全部楼层 |阅读模式
资料来自于ST官网,需要的朋友自取
0.png

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @file    Multi/Examples/MotionControl/IHM13A1_ExampleFor1BiDirMotor/Src/main.c
  4.   * @author  IPC Rennes
  5.   * @version V1.3.0
  6.   * @date    March 16th, 2018
  7.   * @brief   This example shows how to use one X-NUCLEO-IHM13A1 expansion board with
  8.   * 1 bidirectionnal Brush DC motor.
  9.   * The demo sequence starts when the user button is pressed.
  10.   * Each time, the user button is pressed, the demo step is changed
  11.   ******************************************************************************
  12.   * @attention
  13.   *
  14.   * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
  15.   *
  16.   * Redistribution and use in source and binary forms, with or without modification,
  17.   * are permitted provided that the following conditions are met:
  18.   *   1. Redistributions of source code must retain the above copyright notice,
  19.   *      this list of conditions and the following disclaimer.
  20.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  21.   *      this list of conditions and the following disclaimer in the documentation
  22.   *      and/or other materials provided with the distribution.
  23.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  24.   *      may be used to endorse or promote products derived from this software
  25.   *      without specific prior written permission.
  26.   *
  27.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  31.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37.   *
  38.   ******************************************************************************
  39.   */

  40. /* Includes ------------------------------------------------------------------*/
  41. #include "main.h"

  42. /** @defgroup IHM13A1_Example_for_1_Bidirectionnal_motor
  43.   * @{
  44.   */

  45. /* Private typedef -----------------------------------------------------------*/
  46. /* Private define ------------------------------------------------------------*/
  47. #define MAX_STEPS (11)
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. static volatile uint16_t gLastError;
  51. static volatile bool gButtonPressed = FALSE;
  52. static volatile uint8_t gStep = MAX_STEPS;

  53. Stspin240_250_Init_t gStspin240_250InitParams =
  54. {
  55.   {20000}, // Frequency of PWM of Input Bridge in Hz up to 100000Hz
  56.   20000,                // Frequency of PWM used for Ref pin in Hz up to 100000Hz
  57.   50,                   //Duty cycle of PWM used for Ref pin (from 0 to 100)
  58.   0                  // Dual Bridge configuration  ( always FALSE for STSPIN250)
  59. };
  60.    
  61. /* Private function prototypes -----------------------------------------------*/
  62. static void MyFlagInterruptHandler(void);
  63. void ButtonHandler(void);
  64. /* Private functions ---------------------------------------------------------*/

  65. /**
  66.   * @brief  Main program
  67.   * @param  None
  68.   * @retval None
  69.   */
  70. int main(void)
  71. {
  72.   /* STM32xx HAL library initialization */
  73.   HAL_Init();
  74.   
  75.   /* Configure the system clock */
  76.   SystemClock_Config();
  77.   
  78.     /* Configure KEY Button */
  79.   BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);   
  80.   
  81.   
  82.   /* Set Systick Interrupt to the highest priority to have HAL_Delay working*/
  83.   /* under the user button handler */
  84.   HAL_NVIC_SetPriority(SysTick_IRQn, 0x0, 0x0);
  85.   
  86.   //----- Init of the Motor control library
  87.   /* Set the Stspin240_250 library to use 1 device */
  88.   BSP_MotorControl_SetNbDevices(BSP_MOTOR_CONTROL_BOARD_ID_STSPIN250, 1);
  89.   /* When BSP_MotorControl_Init is called with NULL pointer,                  */
  90.   /* the Stspin240_250 library parameters are set with the predefined values from file   */
  91.   /* stspin240_250_target_config.h, otherwise the registers are set using the   */
  92.   /* Stspin240_250_Init_t pointer structure                */
  93.   /* Uncomment the call to BSP_MotorControl_Init below to initialize the      */
  94.   /* device with the structure gStspin240_250InitParams declared in the the main.c file */
  95.   /* and comment the subsequent call having the NULL pointer                   */
  96.   BSP_MotorControl_Init(BSP_MOTOR_CONTROL_BOARD_ID_STSPIN250, &gStspin240_250InitParams);
  97.   //BSP_MotorControl_Init(BSP_MOTOR_CONTROL_BOARD_ID_STSPIN250, NULL);
  98.   
  99.   /* Disable Dual bridge configuration as it is not supported by STSPIN250*/
  100.   BSP_MotorControl_SetDualFullBridgeConfig(0);
  101.   
  102.   /* Attach the function MyFlagInterruptHandler (defined below) to the flag interrupt */
  103.   BSP_MotorControl_AttachFlagInterrupt(MyFlagInterruptHandler);

  104.   /* Attach the function Error_Handler (defined below) to the error Handler*/
  105.   BSP_MotorControl_AttachErrorHandler(Error_Handler);

  106.   /* Set PWM Frequency of Ref to 15000 Hz */
  107.   BSP_MotorControl_SetRefFreq(0,15000);

  108.   /* Set PWM duty cycle of Ref to 60% */
  109.   BSP_MotorControl_SetRefDc(0,60);
  110.   
  111.   /* Set PWM Frequency of bridge A inputs to 10000 Hz */
  112.   BSP_MotorControl_SetBridgeInputPwmFreq(0,10000);
  113.   
  114.   /* Infinite loop */
  115.   while(1)
  116.   {
  117.     /* Each time the user button is pressed, the step is increased by 1 */
  118.     if (gButtonPressed)
  119.     {
  120.       gButtonPressed = FALSE;
  121.       gStep++;
  122.       if (gStep > MAX_STEPS)
  123.       {
  124.         gStep = 0;
  125.       }
  126.       
  127.       switch (gStep)
  128.       {  
  129.         case 0:
  130.           /*********** Step 0  ************/
  131.           /* Set speed of motor 0 to 100 % */
  132.           BSP_MotorControl_SetMaxSpeed(0,100);
  133.           /* start motor 0 to run forward*/
  134.           /* if chip is in standby mode */
  135.           /* it is automatically awakened */
  136.           BSP_MotorControl_Run(0, FORWARD);
  137.           break;
  138.       
  139.          case 1:
  140.           /*********** Step 1  ************/
  141.           /* Set speed of motor 0 to 75 % */
  142.           BSP_MotorControl_SetMaxSpeed(0,75);
  143.           break;
  144.       
  145.         case 2:
  146.           /*********** Step 2 ************/
  147.           /* Set speed of motor 0 to 50 % */
  148.           BSP_MotorControl_SetMaxSpeed(0,50);   
  149.           break;      
  150.       
  151.         case 3:
  152.           /*********** Step 3 ************/
  153.           /* Set speed of motor 0 to 25 % */
  154.           BSP_MotorControl_SetMaxSpeed(0,25);  
  155.           break;  
  156.       
  157.         case 4:
  158.           /*********** Step 4 ************/
  159.           /* Stop Motor 0 */
  160.           BSP_MotorControl_HardStop(0);   
  161.           break;         
  162.          case 5:
  163.           /*********** Step 5  ************/
  164.           /* Set speed of motor 0 to 25 % */
  165.           BSP_MotorControl_SetMaxSpeed(0,25);
  166.           /* start motor 0 to run backward */
  167.           BSP_MotorControl_Run(0, BACKWARD);
  168.           break;
  169.       
  170.          case 6:
  171.           /*********** Step 6  ************/
  172.           /* Set speed of motor 0 to 50 % */
  173.           BSP_MotorControl_SetMaxSpeed(0,50);
  174.           break;
  175.       
  176.         case 7:
  177.           /*********** Step 7 ************/
  178.           /* Set speed of motor 0 to 75 % */
  179.           BSP_MotorControl_SetMaxSpeed(0,75);   
  180.           break;      
  181.       
  182.         case 8:
  183.           /*********** Step 8 ************/
  184.           /* Set speed of motor 0 to 100 % */
  185.           BSP_MotorControl_SetMaxSpeed(0,100);   
  186.           break;  
  187.       
  188.         case 9:
  189.           /*********** Step 9 ************/
  190.           /* Stop motor and disable bridge */
  191.           BSP_MotorControl_CmdHardHiZ(0);   

  192.           break;           
  193.         case 10:
  194.           /*********** Step 10 ************/
  195.           /* Start motor to go forward*/
  196.           BSP_MotorControl_Run(0,FORWARD);   
  197.           break;                 
  198.         case 11:
  199.         default:
  200.           /*********** Step 11 ************/
  201.           /* Stop motor and put chip in standby mode */
  202.           BSP_MotorControl_Reset(0);   
  203.           break;            
  204.       }
  205.     }
  206.   }
  207. }

  208. /**
  209.   * @brief  This function is the User handler for the flag interrupt
  210.   * @param  None
  211.   * @retval None
  212.   */
  213. void MyFlagInterruptHandler(void)
  214. {
  215.   /* Code to be customised */
  216.   /************************/
  217.   /* Get the state of bridge  */
  218.   uint16_t bridgeState  = BSP_MotorControl_CmdGetStatus(0);
  219.   
  220.   if (bridgeState == 0)
  221.   {
  222.     if (BSP_MotorControl_GetDeviceState(0) != INACTIVE)
  223.     {
  224.       /* Bridges were disabled due to overcurrent or over temperature */
  225.       /* When  motor was running */
  226.         Error_Handler(0XBAD0);
  227.     }
  228.   }
  229. }

  230. /**
  231.   * @brief  This function is executed in case of error occurrence.
  232.   * @param  error number of the error
  233.   * @retval None
  234.   */
  235. void Error_Handler(uint16_t error)
  236. {
  237.   /* Backup error number */
  238.   gLastError = error;
  239.   
  240.   /* Infinite loop */
  241.   while(1)
  242.   {
  243.   }
  244. }

  245. /**
  246.   * @brief  This function is executed when the Nucleo User button is pressed
  247.   * @param  error number of the error
  248.   * @retval None
  249.   */
  250. void ButtonHandler(void)
  251. {
  252.   gButtonPressed = TRUE;
  253.   
  254.   /* Let 300 ms before clearing the IT for key debouncing */
  255.   HAL_Delay(300);
  256.   __HAL_GPIO_EXTI_CLEAR_IT(KEY_BUTTON_PIN);
  257.   HAL_NVIC_ClearPendingIRQ(KEY_BUTTON_EXTI_IRQn);
  258. }   

  259. #ifdef  USE_FULL_ASSERT

  260. /**
  261.   * @brief  Reports the name of the source file and the source line number
  262.   *         where the assert_param error has occurred.
  263.   * @param  file: pointer to the source file name
  264.   * @param  line: assert_param error line source number
  265.   * @retval None
  266.   */
  267. void assert_failed(uint8_t* file, uint32_t line)
  268. {
  269.   /* User can add his own implementation to report the file name and line number,
  270.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  271.   /* Infinite loop */
  272.   while (1)
  273.   {
  274.   }
  275. }
  276. #endif

  277. /**
  278.   * @}
  279.   */


  280. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
低压有刷直流马达驱动-适合stm32全系列.zip (12.5 MB, 下载次数: 34)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表