当前位置:   article > 正文

基于STM32智能垃圾桶设计(1)-按键+舵机控制部分_基于stm32的智能垃圾桶控制系统设计

基于stm32的智能垃圾桶控制系统设计

本次选用的单片机型号是STM32F103C8T6最小系统,也是淘宝上就轻便的那一款。

首先打开STM32CUBEMX新建一个工程,看图如下:

到此我们就创建完成了工程,接下来需要配置引脚和时钟完成基本的32配置。

像上图配置,开启调试引脚,由于这款最小系统板没有设计串口模块,所以后头我们需要使用ST-LINKV2进行程序烧录,如果有串口模块也可以使用串口模块进行程序烧录。

接下来就是开启时钟,选用的是外部的8Mhz晶振作为时钟。

开启完晶振后,我们需要下一步配置时钟树,此处配置72Mhz工作,下面的PWM输出也是结合这个频率计算,时钟树的配置如下。

那么接下来是要进行GPIO口和PWM模式的配置,此处选用4个舵机和4个按键进行控制,配置和控制的原理一致,大家可以结合自己设计的电路和原理图分析映射到对应的引脚配置即可。

                                                 

首先按键的电路设计采用是一端接地,另一端接IO的形式,只需要将IO口配置成输入模式,读取引脚的电平即可判断按键是否按下,由于此处一端已经接地了,我们需要将IO口配置成输入上拉模式,让他初始读取电平保持高电平,这样子按键按下后就接通地读取到低电平。如果自己的开发板是一端接IO口,另外一端接VCC,此处就需要考虑设置成下拉输入。

此次我用到4个按键,分别是PB6、PB7、PB8、PB9,下面以PB6为例进行配置;

还可以在下图中对引脚重命名,方便程序调用,例如我命名PB6为KEY1

下图为4个完成配置的图片,根据自己的电路配置完成即可。

本小节的任务是完成按键控制舵机任务,使用一个按键控制一个舵机,那么接下来需要对舵机的PWM进行配置,本次选用的4个舵机IO口为PA0,PA1,PA2,PA3。下面会以PA0举例配置,其余几个依次类推接口。

现在市面常见的舵机控制均使用PWM,需要使用的是20ms周期的波形,占空比为0.5ms-2.5ms,映射过去对应0-180°,此处选用的舵机型号是SG90小小的蓝色舵机,具体的原理和细节可以看其它博主的描述,很详细的,此处不再赘述。

下图为开启PWM通道的设置,它们均是定时器2的通道,所以我这里一起开了,如果使用其它引脚只需要打开IO口对应映射定时器的PWM通道即可。

那么20ms其实对应的是50hz波形,我们需要计算分频系数和,定时器计数值来得到50hz周期波形。

此处设置PSC分频系数为719(720-1),计数值为1999(2000-1)

72000000/720=100000hz

100000/2000=50hz

如下图即可:

上面的脉冲值我们设置为50,默认初始化后舵机在0°位置,因为2000对应20ms,那么50对应0.5ms刚好是0°位置,比较重要的一步是不能忘记开启定时器中断,如下所示

完成最后几步配置输出keil5工程

点击右上角生成,如下图就生成工程成功。

下面就是程序书写部分,4个按键4个舵机,写一个1对1按键控制舵机程序。

main.c代码

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "tim.h"
  22. #include "gpio.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. /* USER CODE BEGIN PV */
  37. /* USER CODE END PV */
  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. /* USER CODE BEGIN PFP */
  41. /* USER CODE END PFP */
  42. /* Private user code ---------------------------------------------------------*/
  43. /* USER CODE BEGIN 0 */
  44. /* USER CODE END 0 */
  45. /**
  46. * @brief The application entry point.
  47. * @retval int
  48. */
  49. int main(void)
  50. {
  51. /* USER CODE BEGIN 1 */
  52. uint8_t key_val;
  53. static _Bool s1=0,s2=0,s3=0,s4=0;//表示4个舵机状态
  54. uint8_t state=1;
  55. /* USER CODE END 1 */
  56. /* MCU Configuration--------------------------------------------------------*/
  57. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  58. HAL_Init();
  59. /* USER CODE BEGIN Init */
  60. /* USER CODE END Init */
  61. /* Configure the system clock */
  62. SystemClock_Config();
  63. /* USER CODE BEGIN SysInit */
  64. /* USER CODE END SysInit */
  65. /* Initialize all configured peripherals */
  66. MX_GPIO_Init();
  67. MX_TIM2_Init();
  68. /* USER CODE BEGIN 2 */
  69. HAL_TIM_Base_Start(&htim2);
  70. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
  71. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
  72. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
  73. HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);
  74. /* USER CODE END 2 */
  75. /* Infinite loop */
  76. /* USER CODE BEGIN WHILE */
  77. while (1)
  78. {
  79. key_val = KEY_Scan(0);
  80. if(key_val==1)
  81. {
  82. if(state==1)
  83. {
  84. state=0;
  85. s1=!s1;
  86. }
  87. if(s1==0)TIM2->CCR4=50;
  88. if(s1==1)TIM2->CCR4=150;
  89. }
  90. else if(key_val==2)
  91. {
  92. if(state==1)
  93. {
  94. state=0;
  95. s2=!s2;
  96. }
  97. if(s2==0)TIM2->CCR3=50;
  98. if(s2==1)TIM2->CCR3=150;
  99. }
  100. else if(key_val==3)
  101. {
  102. if(state==1)
  103. {
  104. state=0;
  105. s3=!s3;
  106. }
  107. if(s3==0)TIM2->CCR2=50;
  108. if(s3==1)TIM2->CCR2=150;
  109. }
  110. else if(key_val==4)
  111. {
  112. if(state==1)
  113. {
  114. state=0;
  115. s4=!s4;
  116. }
  117. if(s4==0)TIM2->CCR1=50;
  118. if(s4==1)TIM2->CCR1=150;
  119. }
  120. else if(key_val==0)
  121. {
  122. state=1;
  123. }
  124. /* USER CODE END WHILE */
  125. /* USER CODE BEGIN 3 */
  126. }
  127. /* USER CODE END 3 */
  128. }
  129. /**
  130. * @brief System Clock Configuration
  131. * @retval None
  132. */
  133. void SystemClock_Config(void)
  134. {
  135. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  136. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  137. /** Initializes the RCC Oscillators according to the specified parameters
  138. * in the RCC_OscInitTypeDef structure.
  139. */
  140. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  141. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  142. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  143. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  144. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  145. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  146. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  147. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  148. {
  149. Error_Handler();
  150. }
  151. /** Initializes the CPU, AHB and APB buses clocks
  152. */
  153. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  154. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  155. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  156. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  157. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  158. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  159. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  160. {
  161. Error_Handler();
  162. }
  163. }
  164. /* USER CODE BEGIN 4 */
  165. /* USER CODE END 4 */
  166. /**
  167. * @brief This function is executed in case of error occurrence.
  168. * @retval None
  169. */
  170. void Error_Handler(void)
  171. {
  172. /* USER CODE BEGIN Error_Handler_Debug */
  173. /* User can add his own implementation to report the HAL error return state */
  174. __disable_irq();
  175. while (1)
  176. {
  177. }
  178. /* USER CODE END Error_Handler_Debug */
  179. }
  180. #ifdef USE_FULL_ASSERT
  181. /**
  182. * @brief Reports the name of the source file and the source line number
  183. * where the assert_param error has occurred.
  184. * @param file: pointer to the source file name
  185. * @param line: assert_param error line source number
  186. * @retval None
  187. */
  188. void assert_failed(uint8_t *file, uint32_t line)
  189. {
  190. /* USER CODE BEGIN 6 */
  191. /* User can add his own implementation to report the file name and line number,
  192. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  193. /* USER CODE END 6 */
  194. }
  195. #endif /* USE_FULL_ASSERT */

gpio.c

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file gpio.c
  5. * @brief This file provides code for the configuration
  6. * of all used GPIO pins.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "gpio.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. /*----------------------------------------------------------------------------*/
  25. /* Configure GPIO */
  26. /*----------------------------------------------------------------------------*/
  27. /* USER CODE BEGIN 1 */
  28. /* USER CODE END 1 */
  29. /** Configure pins as
  30. * Analog
  31. * Input
  32. * Output
  33. * EVENT_OUT
  34. * EXTI
  35. */
  36. void MX_GPIO_Init(void)
  37. {
  38. GPIO_InitTypeDef GPIO_InitStruct = {0};
  39. /* GPIO Ports Clock Enable */
  40. __HAL_RCC_GPIOD_CLK_ENABLE();
  41. __HAL_RCC_GPIOA_CLK_ENABLE();
  42. __HAL_RCC_GPIOB_CLK_ENABLE();
  43. /*Configure GPIO pins : PBPin PBPin PBPin PBPin */
  44. GPIO_InitStruct.Pin = KEY1_Pin|KEY2_Pin|KEY3_Pin|KEY4_Pin;
  45. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  46. GPIO_InitStruct.Pull = GPIO_PULLUP;
  47. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  48. }
  49. /* USER CODE BEGIN 2 */
  50. uint8_t KEY_Scan(uint8_t mode)
  51. {
  52. static uint8_t key_up=1;
  53. if(mode==1) key_up=1;//支持连续按
  54. if(key_up==1&&(KEY1==0||KEY2==0||KEY3==0||KEY4==0))
  55. {
  56. HAL_Delay(10);//延时,防抖
  57. key_up=0;//标记这次key已经按下
  58. if(KEY1==0)return 1;
  59. else if(KEY2==0)return 2;
  60. else if(KEY3==0)return 3;
  61. else if(KEY4==0)return 4;
  62. }else if(KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1)key_up=1;
  63. return 0;
  64. }
  65. /* USER CODE END 2 */

gpio.h

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file gpio.h
  5. * @brief This file contains all the function prototypes for
  6. * the gpio.c file
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef __GPIO_H__
  22. #define __GPIO_H__
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* Includes ------------------------------------------------------------------*/
  27. #include "main.h"
  28. /* USER CODE BEGIN Includes */
  29. /* USER CODE END Includes */
  30. /* USER CODE BEGIN Private defines */
  31. #define KEY1 HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)
  32. #define KEY2 HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)
  33. #define KEY3 HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)
  34. #define KEY4 HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)
  35. /* USER CODE END Private defines */
  36. void MX_GPIO_Init(void);
  37. /* USER CODE BEGIN Prototypes */
  38. uint8_t KEY_Scan(uint8_t mode);
  39. /* USER CODE END Prototypes */
  40. #ifdef __cplusplus
  41. }
  42. #endif
  43. #endif /*__ GPIO_H__ */

main.h

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.h
  5. * @brief : Header for main.c file.
  6. * This file contains the common defines of the application.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef __MAIN_H
  22. #define __MAIN_H
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* Includes ------------------------------------------------------------------*/
  27. #include "stm32f1xx_hal.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. /* USER CODE END Includes */
  31. /* Exported types ------------------------------------------------------------*/
  32. /* USER CODE BEGIN ET */
  33. /* USER CODE END ET */
  34. /* Exported constants --------------------------------------------------------*/
  35. /* USER CODE BEGIN EC */
  36. /* USER CODE END EC */
  37. /* Exported macro ------------------------------------------------------------*/
  38. /* USER CODE BEGIN EM */
  39. /* USER CODE END EM */
  40. /* Exported functions prototypes ---------------------------------------------*/
  41. void Error_Handler(void);
  42. /* USER CODE BEGIN EFP */
  43. /* USER CODE END EFP */
  44. /* Private defines -----------------------------------------------------------*/
  45. #define KEY1_Pin GPIO_PIN_6
  46. #define KEY1_GPIO_Port GPIOB
  47. #define KEY2_Pin GPIO_PIN_7
  48. #define KEY2_GPIO_Port GPIOB
  49. #define KEY3_Pin GPIO_PIN_8
  50. #define KEY3_GPIO_Port GPIOB
  51. #define KEY4_Pin GPIO_PIN_9
  52. #define KEY4_GPIO_Port GPIOB
  53. /* USER CODE BEGIN Private defines */
  54. /* USER CODE END Private defines */
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #endif /* __MAIN_H */

不懂得使用STLINKV2接线烧录的可以参考我的这篇文章 

https://blog.csdn.net/anyi66622/article/details/130321309?spm=1001.2014.3001.5501

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/560124
推荐阅读
相关标签
  

闽ICP备14008679号