找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 39361|回复: 142
收起左侧

STM32开源闭环步进电机源程序+PCB原理图全套资料分享

  [复制链接]
ID:520801 发表于 2019-10-27 14:27 | 显示全部楼层 |阅读模式
这个闭环电机是我转过来的,自己打板验证过稳定 电机版权归开发者,侵删, 上传的源码为开源版本

1.JPG 12.jpg
Altium Designer画的原理图和PCB图如下:(51hei附件中可下载工程文件)
0.png 0.png 0.png

单片机源程序如下:
  1. /* Define to prevent recursive inclusion -------------------------------------*/
  2. #ifndef __STM32F0xx_HAL_CONF_H
  3. #define __STM32F0xx_HAL_CONF_H

  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif

  7. #include "main.h"
  8. /* Exported types ------------------------------------------------------------*/
  9. /* Exported constants --------------------------------------------------------*/

  10. /* ########################## Module Selection ############################## */
  11. /**
  12.   * @brief This is the list of modules to be used in the HAL driver
  13.   */
  14. #define HAL_MODULE_ENABLED  
  15. /*#define HAL_ADC_MODULE_ENABLED   */
  16. /*#define HAL_CRYP_MODULE_ENABLED   */
  17. /*#define HAL_CAN_MODULE_ENABLED   */
  18. /*#define HAL_CEC_MODULE_ENABLED   */
  19. /*#define HAL_COMP_MODULE_ENABLED   */
  20. /*#define HAL_CRC_MODULE_ENABLED   */
  21. /*#define HAL_CRYP_MODULE_ENABLED   */
  22. /*#define HAL_TSC_MODULE_ENABLED   */
  23. /*#define HAL_DAC_MODULE_ENABLED   */
  24. /*#define HAL_I2S_MODULE_ENABLED   */
  25. /*#define HAL_IWDG_MODULE_ENABLED   */
  26. /*#define HAL_LCD_MODULE_ENABLED   */
  27. /*#define HAL_LPTIM_MODULE_ENABLED   */
  28. /*#define HAL_RNG_MODULE_ENABLED   */
  29. /*#define HAL_RTC_MODULE_ENABLED   */
  30. /*#define HAL_SPI_MODULE_ENABLED   */
  31. /*#define HAL_TIM_MODULE_ENABLED   */
  32. /*#define HAL_UART_MODULE_ENABLED   */
  33. /*#define HAL_USART_MODULE_ENABLED   */
  34. /*#define HAL_IRDA_MODULE_ENABLED   */
  35. /*#define HAL_SMARTCARD_MODULE_ENABLED   */
  36. /*#define HAL_SMBUS_MODULE_ENABLED   */
  37. /*#define HAL_WWDG_MODULE_ENABLED   */
  38. /*#define HAL_PCD_MODULE_ENABLED   */
  39. #define HAL_CORTEX_MODULE_ENABLED
  40. #define HAL_DMA_MODULE_ENABLED
  41. #define HAL_FLASH_MODULE_ENABLED
  42. #define HAL_GPIO_MODULE_ENABLED
  43. #define HAL_PWR_MODULE_ENABLED
  44. #define HAL_RCC_MODULE_ENABLED
  45. #define HAL_I2C_MODULE_ENABLED

  46. /* ########################## HSE/HSI Values adaptation ##################### */
  47. /**
  48.   * @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
  49.   *        This value is used by the RCC HAL module to compute the system frequency
  50.   *        (when HSE is used as system clock source, directly or through the PLL).  
  51.   */
  52. #if !defined  (HSE_VALUE)
  53.   #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
  54. #endif /* HSE_VALUE */

  55. /**
  56.   * @brief In the following line adjust the External High Speed oscillator (HSE) Startup
  57.   *        Timeout value
  58.   */
  59. #if !defined  (HSE_STARTUP_TIMEOUT)
  60.   #define HSE_STARTUP_TIMEOUT    ((uint32_t)100)   /*!< Time out for HSE start up, in ms */
  61. #endif /* HSE_STARTUP_TIMEOUT */

  62. /**
  63.   * @brief Internal High Speed oscillator (HSI) value.
  64.   *        This value is used by the RCC HAL module to compute the system frequency
  65.   *        (when HSI is used as system clock source, directly or through the PLL).
  66.   */
  67. #if !defined  (HSI_VALUE)
  68.   #define HSI_VALUE    ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/
  69. #endif /* HSI_VALUE */

  70. /**
  71.   * @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup
  72.   *        Timeout value
  73.   */
  74. #if !defined  (HSI_STARTUP_TIMEOUT)
  75. #define HSI_STARTUP_TIMEOUT   ((uint32_t)5000) /*!< Time out for HSI start up */
  76. #endif /* HSI_STARTUP_TIMEOUT */  

  77. /**
  78.   * @brief Internal High Speed oscillator for ADC (HSI14) value.
  79.   */
  80. #if !defined  (HSI14_VALUE)
  81. #define HSI14_VALUE ((uint32_t)14000000) /*!< Value of the Internal High Speed oscillator for ADC in Hz.
  82.                                              The real value may vary depending on the variations
  83.                                              in voltage and temperature.  */
  84. #endif /* HSI14_VALUE */

  85. /**
  86.   * @brief Internal High Speed oscillator for USB (HSI48) value.
  87.   */
  88. #if !defined  (HSI48_VALUE)
  89. #define HSI48_VALUE ((uint32_t)48000000) /*!< Value of the Internal High Speed oscillator for USB in Hz.
  90.                                              The real value may vary depending on the variations
  91.                                              in voltage and temperature.  */
  92. #endif /* HSI48_VALUE */

  93. /**
  94.   * @brief Internal Low Speed oscillator (LSI) value.
  95.   */
  96. #if !defined  (LSI_VALUE)
  97. #define LSI_VALUE  ((uint32_t)40000)   
  98. #endif /* LSI_VALUE */                      /*!< Value of the Internal Low Speed oscillator in Hz
  99.                                              The real value may vary depending on the variations
  100.                                              in voltage and temperature.  */
  101. /**
  102.   * @brief External Low Speed oscillator (LSI) value.
  103.   */
  104. #if !defined  (LSE_VALUE)
  105. #define LSE_VALUE  ((uint32_t)32768)    /*!< Value of the External Low Speed oscillator in Hz */
  106. #endif /* LSE_VALUE */     

  107. #if !defined  (LSE_STARTUP_TIMEOUT)
  108.   #define LSE_STARTUP_TIMEOUT    ((uint32_t)5000)   /*!< Time out for LSE start up, in ms */
  109. #endif /* LSE_STARTUP_TIMEOUT */

  110. /* Tip: To avoid modifying this file each time you need to use different HSE,
  111.    ===  you can define the HSE value in your toolchain compiler preprocessor. */

  112. /* ########################### System Configuration ######################### */
  113. /**
  114.   * @brief This is the HAL system configuration section
  115.   */     
  116. #define  VDD_VALUE                    ((uint32_t)3300) /*!< Value of VDD in mv */           
  117. #define  TICK_INT_PRIORITY            ((uint32_t)0)    /*!< tick interrupt priority (lowest by default)  */            
  118.                                                                               /*  Warning: Must be set to higher priority for HAL_Delay()  */
  119.                                                                               /*  and HAL_GetTick() usage under interrupt context          */
  120. #define  USE_RTOS                     0     
  121. #define  PREFETCH_ENABLE              1              
  122. #define  INSTRUCTION_CACHE_ENABLE     0
  123. #define  DATA_CACHE_ENABLE            0
  124. /* ########################## Assert Selection ############################## */
  125. /**
  126.   * @brief Uncomment the line below to expanse the "assert_param" macro in the
  127.   *        HAL drivers code
  128.   */
  129. /* #define USE_FULL_ASSERT   1U */

  130. /* ################## SPI peripheral configuration ########################## */

  131. /* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver
  132. * Activated: CRC code is present inside driver
  133. * Deactivated: CRC code cleaned from driver
  134. */

  135. #define USE_SPI_CRC                     0U

  136. /* Includes ------------------------------------------------------------------*/
  137. /**
  138.   * @brief Include module's header file
  139.   */

  140. #ifdef HAL_RCC_MODULE_ENABLED
  141. #include "stm32f0xx_hal_rcc.h"
  142. #endif /* HAL_RCC_MODULE_ENABLED */

  143. #ifdef HAL_GPIO_MODULE_ENABLED
  144. #include "stm32f0xx_hal_gpio.h"
  145. #endif /* HAL_GPIO_MODULE_ENABLED */

  146. #ifdef HAL_DMA_MODULE_ENABLED
  147.   #include "stm32f0xx_hal_dma.h"
  148. #endif /* HAL_DMA_MODULE_ENABLED */

  149. #ifdef HAL_CORTEX_MODULE_ENABLED
  150. #include "stm32f0xx_hal_cortex.h"
  151. #endif /* HAL_CORTEX_MODULE_ENABLED */

  152. #ifdef HAL_ADC_MODULE_ENABLED
  153. #include "stm32f0xx_hal_adc.h"
  154. #endif /* HAL_ADC_MODULE_ENABLED */

  155. #ifdef HAL_CAN_MODULE_ENABLED
  156. #include "stm32f0xx_hal_can.h"
  157. #endif /* HAL_CAN_MODULE_ENABLED */

  158. #ifdef HAL_CEC_MODULE_ENABLED
  159. #include "stm32f0xx_hal_cec.h"
  160. #endif /* HAL_CEC_MODULE_ENABLED */

  161. #ifdef HAL_COMP_MODULE_ENABLED
  162. #include "stm32f0xx_hal_comp.h"
  163. #endif /* HAL_COMP_MODULE_ENABLED */

  164. #ifdef HAL_CRC_MODULE_ENABLED
  165. #include "stm32f0xx_hal_crc.h"
  166. #endif /* HAL_CRC_MODULE_ENABLED */

  167. #ifdef HAL_DAC_MODULE_ENABLED
  168. #include "stm32f0xx_hal_dac.h"
  169. #endif /* HAL_DAC_MODULE_ENABLED */

  170. #ifdef HAL_FLASH_MODULE_ENABLED
  171. #include "stm32f0xx_hal_flash.h"
  172. #endif /* HAL_FLASH_MODULE_ENABLED */

  173. #ifdef HAL_I2C_MODULE_ENABLED
  174. #include "stm32f0xx_hal_i2c.h"
  175. #endif /* HAL_I2C_MODULE_ENABLED */

  176. #ifdef HAL_I2S_MODULE_ENABLED
  177. #include "stm32f0xx_hal_i2s.h"
  178. #endif /* HAL_I2S_MODULE_ENABLED */

  179. #ifdef HAL_IRDA_MODULE_ENABLED
  180. #include "stm32f0xx_hal_irda.h"
  181. #endif /* HAL_IRDA_MODULE_ENABLED */

  182. #ifdef HAL_IWDG_MODULE_ENABLED
  183. #include "stm32f0xx_hal_iwdg.h"
  184. #endif /* HAL_IWDG_MODULE_ENABLED */

  185. #ifdef HAL_PCD_MODULE_ENABLED
  186. #include "stm32f0xx_hal_pcd.h"
  187. #endif /* HAL_PCD_MODULE_ENABLED */

  188. #ifdef HAL_PWR_MODULE_ENABLED
  189. #include "stm32f0xx_hal_pwr.h"
  190. #endif /* HAL_PWR_MODULE_ENABLED */

  191. #ifdef HAL_RTC_MODULE_ENABLED
  192. #include "stm32f0xx_hal_rtc.h"
  193. #endif /* HAL_RTC_MODULE_ENABLED */

  194. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  195. #include "stm32f0xx_hal_smartcard.h"
  196. #endif /* HAL_SMARTCARD_MODULE_ENABLED */

  197. #ifdef HAL_SMBUS_MODULE_ENABLED
  198. #include "stm32f0xx_hal_smbus.h"
  199. #endif /* HAL_SMBUS_MODULE_ENABLED */

  200. #ifdef HAL_SPI_MODULE_ENABLED
  201. #include "stm32f0xx_hal_spi.h"
  202. #endif /* HAL_SPI_MODULE_ENABLED */

  203. #ifdef HAL_TIM_MODULE_ENABLED
  204. #include "stm32f0xx_hal_tim.h"
  205. #endif /* HAL_TIM_MODULE_ENABLED */

  206. #ifdef HAL_TSC_MODULE_ENABLED
  207. #include "stm32f0xx_hal_tsc.h"
  208. #endif /* HAL_TSC_MODULE_ENABLED */

  209. #ifdef HAL_UART_MODULE_ENABLED
  210. #include "stm32f0xx_hal_uart.h"
  211. #endif /* HAL_UART_MODULE_ENABLED */

  212. #ifdef HAL_USART_MODULE_ENABLED
  213. #include "stm32f0xx_hal_usart.h"
  214. #endif /* HAL_USART_MODULE_ENABLED */

  215. #ifdef HAL_WWDG_MODULE_ENABLED
  216. #include "stm32f0xx_hal_wwdg.h"
  217. #endif /* HAL_WWDG_MODULE_ENABLED */

  218. /* Exported macro ------------------------------------------------------------*/
  219. #ifdef  USE_FULL_ASSERT
  220. /**
  221.   * @brief  The assert_param macro is used for function's parameters check.
  222.   * @param  expr: If expr is false, it calls assert_failed function
  223.   *         which reports the name of the source file and the source
  224.   *         line number of the call that failed.
  225.   *         If expr is true, it returns no value.
  226.   * @retval None
  227.   */
  228.   #define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
  229. /* Exported functions ------------------------------------------------------- */
  230.   void assert_failed(uint8_t* file, uint32_t line);
  231. #else
  232.   #define assert_param(expr) ((void)0U)
  233. #endif /* USE_FULL_ASSERT */   
  234.    
  235. #ifdef __cplusplus
  236. }
  237. #endif

  238. #endif /* __STM32F0xx_HAL_CONF_H */
复制代码

20190917电机软硬件更新:软件跟前版基本一样只更改了几个参数以提高稳定性,增加了中文注释以方便学习理解;硬件加了防反接二极管,型号SL54,将原来的排针座子改为更美观高度更低的XH2.54座子,另外将串口和烧录口合二为一,方便用STLINKV2.1直接进行烧录和串口通信。   
    20190831软硬件更新:硬件电路板将光耦输入侧覆铜挖空防止干扰,传感器改为底层焊接,电路板设计为需要用尼龙垫圈跟电机隔开2mm左右以防止干扰;软件方面所有浮点数变量都改为定点变量,重新改写了程序以适应定点变量,单片机计算量大为降低,CPU仍然有3/5左右空闲时间可以用来进行其他功能的二次开发。位置环更新频率由5KHZ提高到10KHZ,控制性能加强,42最高转速提到1200RPM左右,57提到1000RPM左右。优化了定时器处理STEP/DIR信号的部分程序,即使是几百K乃至上M的高速脉冲都可以轻松处理而不丢脉冲。重载了stdio.h头文件里的fputc()和fgetc()两个函数,程序现在可以利用printf()和scanf()等标准输入输出函数编写RS232和RS485协议,另外加了一个简单的ASCII协议作为参考。
      20190810最新更新:硬件电路板换了新的LDO型号RS3005,彻底解决了稳压芯片过热停机的问题。另外输入信号CLK由原来的外部中断脚位改为定时器TIM1的外部计数输入脚,电路板改为同时支持STM32F103和STM32F030两种单片机。软件上对原来CLK信号的处理由外部中断改为定时器计数,这样就不需要耗费大量的处理器资源。原先用中断来处理CLK信号导致耗费大量的处理时间,也会导致程序对电机的测速不正确,32细分的情况下电机转速只要跑到1000RPM就可能使程序崩溃,想要使用128/256这样的高细分几乎不可能。

     这套一体化闭环步进电机是店主自己全新研发的,不是拿人家外国开源的项目直接山寨抄袭来骗小白的, 购买电机附带全套软硬件,包括源代码程序、42步进和57步进的原理图和PCB,可以买回去自己打板复制,也可以二次开发,店主会不定期更新软硬件并修复BUG,可以提供代码级技术支持。数量有限,都是平时抽空手工焊接做出来的,购买前请先咨询店主是否有货。
    电机软硬件性能大幅提高,硬件成本仅国外MECHADUINO及其山寨产品的1/2不到,最大驱动电流可达3.5A,是国外版的2倍,最大转速提高2.5倍(42电机1000RPM,57电机800RPM),振动及噪声减小2/3。按照工业级标准设计,加入高速隔离光耦、软件看门狗,可靠性大幅优化。自带开环闭环模式随意切换,细分选择、编码器校正全部拨码开关一键完成,无需连接上位机二次编程!

更多细节和视频可以参考帖子:
https://www.bilibili.com/video/av53223504?from=search&seid=10269700708564966156
闭环步进参数:
主控芯片:意法半导体STM32F030C8T6
驱动芯片:两颗A4950(最大电流3.5A)
编码器芯片:英飞凌15位汽车工业级TLE5012B
高速光耦:东芝双通道TLP2168
工作电压:12-30V(推荐24V)
工作电流:额定1.3A(42步进)2.5A(57步进)最大3.5A
控制精度:小于0.08度
电子齿轮:4、8、16、32(可任意设置)

清单:包含一块电机驱动板+电机+专用径向磁铁
          +安装垫片,全部组装好测好到手能用!
11.jpg
10.jpg
9.jpg
8.jpg
7.jpg
6.jpg
5.jpg
4.jpg
3.jpg
13.jpg

电流设置

电流设置
2.jpg

STM32_FLASH的3种烧写方式.pdf

606.22 KB, 下载次数: 649, 下载积分: 黑币 -5

烧录

PID经典教程.pdf

343.56 KB, 下载次数: 716, 下载积分: 黑币 -5

pid调整

42步进电路图,57步进电路PCB文件图.7z

14.08 MB, 下载次数: 875, 下载积分: 黑币 -5

原理图pcb

4257通用固件.7z

2.7 MB, 下载次数: 1092, 下载积分: 黑币 -5

源码

评分

参与人数 6黑币 +108 收起 理由
lzh1762 + 6
hzd530 + 6 赞一个!
lxj199610 + 30 共享资料的黑币奖励!
accumulation + 10 很给力!
BigWheels + 6 赞一个!
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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