小言_互联网的博客

STM32单片机利用软件定时器实现分时多任务(使用HAL库编写)

420人阅读  评论(0)

概述

      很多时候,应用场景,是需要分时处理各个事件。本文章就编写点亮LED与打印输出的示例来讲解使用方法。在这直接进入正题。(在此不做创建项目的步骤介绍)

1、STM32CubeIDE 配置

  1. LED管脚配置

  2. 串口1配置(也就是debug串口)

2、代码

  1. usart.c文件
    
        
    1. /* USER CODE BEGIN 0 */
    2. #include "stdio.h"
    3. /* USER CODE END 0 */
    4. .
    5. .
    6. .
    7. /* USER CODE BEGIN 1 */
    8. #ifdef __GNUC__
    9. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
    10. PUTCHAR_PROTOTYPE
    11. {
    12. // 注意下面第一个参数是&huart1,因为cubemx配置了串口1自动生成的
    13. HAL_UART_Transmit(&huart1, ( uint8_t*)&ch, 1, HAL_MAX_DELAY);
    14. return ch;
    15. }
    16. #endif
    17. /* USER CODE END 1 */

     

  2. 创建software_Time.c文件
    
        
    1. /*
    2. * software_Time.c
    3. *
    4. * Created on: 2020年4月10日
    5. * Author: champion
    6. */
    7. #include "stm32l0xx.h"
    8. #include "software_Time.h"
    9. void SetTime(TIME_TYPE_ST *TimeType,uint32_t TimeInter)
    10. {
    11. TimeType->TimeStart = HAL_GetTick(); //获取起始时间
    12. TimeType->TimeInter = TimeInter; //获取间隔时间
    13. }
    14. uint32_t CompareTime(TIME_TYPE_ST *TimeType) //每隔1毫秒,计数器就会增加1
    15. {
    16. return ((HAL_GetTick()-TimeType->TimeStart) >= TimeType->TimeInter);
    17. }

     

  3. 创建software_Time.h文件
    
        
    1. /*
    2. * software_Time.h
    3. *
    4. * Created on: 2020年4月10日
    5. * Author: champion
    6. */
    7. #ifndef INC_SOFTWARE_TIME_H_
    8. #define INC_SOFTWARE_TIME_H_
    9. #include "stdint.h"
    10. typedef struct
    11. {
    12. uint32_t TimeStart; //获取起始时间
    13. uint32_t TimeInter; //间隔时间
    14. }TIME_TYPE_ST;
    15. void SetTime(TIME_TYPE_ST *TimeType,uint32_t TimeInter); //打开超时定时器
    16. uint32_t CompareTime(TIME_TYPE_ST *TimeType); //比较函数
    17. #endif /* INC_SOFTWARE_TIME_H_ */

     

  4. main.c文件
    
        
    1. /* USER CODE END Header */
    2. /* Includes ------------------------------------------------------------------*/
    3. #include "main.h"
    4. #include "usart.h"
    5. #include "gpio.h"
    6. /* Private includes ----------------------------------------------------------*/
    7. /* USER CODE BEGIN Includes */
    8. #include "stdio.h"
    9. #include "software_Time.h"
    10. /* USER CODE END Includes */
    11. /* Private typedef -----------------------------------------------------------*/
    12. /* USER CODE BEGIN PTD */
    13. /* USER CODE END PTD */
    14. /* Private define ------------------------------------------------------------*/
    15. /* USER CODE BEGIN PD */
    16. /* USER CODE END PD */
    17. /* Private macro -------------------------------------------------------------*/
    18. /* USER CODE BEGIN PM */
    19. /* USER CODE END PM */
    20. /* Private variables ---------------------------------------------------------*/
    21. /* USER CODE BEGIN PV */
    22. static TIME_TYPE_ST TimeLED;
    23. static TIME_TYPE_ST TimePrint_1;
    24. static TIME_TYPE_ST TimePrint_2;
    25. /* USER CODE END PV */
    26. /* Private function prototypes -----------------------------------------------*/
    27. void SystemClock_Config(void);
    28. /* USER CODE BEGIN PFP */
    29. /* USER CODE END PFP */
    30. /* Private user code ---------------------------------------------------------*/
    31. /* USER CODE BEGIN 0 */
    32. /* USER CODE END 0 */
    33. /**
    34. * @brief The application entry point.
    35. * @retval int
    36. */
    37. int main(void)
    38. {
    39. /* USER CODE BEGIN 1 */
    40. /* USER CODE END 1 */
    41. /* MCU Configuration--------------------------------------------------------*/
    42. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    43. HAL_Init();
    44. /* USER CODE BEGIN Init */
    45. /* USER CODE END Init */
    46. /* Configure the system clock */
    47. SystemClock_Config();
    48. /* USER CODE BEGIN SysInit */
    49. /* USER CODE END SysInit */
    50. /* Initialize all configured peripherals */
    51. MX_GPIO_Init();
    52. MX_LPUART1_UART_Init();
    53. MX_USART1_UART_Init();
    54. /* USER CODE BEGIN 2 */
    55. /* USER CODE END 2 */
    56. /* Infinite loop */
    57. /* USER CODE BEGIN WHILE */
    58. while ( 1)
    59. {
    60. if(CompareTime(&TimeLED))
    61. {
    62. SetTime(&TimeLED, 500); //500ms
    63. HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    64. }
    65. if(CompareTime(&TimePrint_1))
    66. {
    67. SetTime(&TimePrint_1, 1000); //1s打印
    68. printf( "TimePrint_1s \r\n");
    69. }
    70. if(CompareTime(&TimePrint_2))
    71. {
    72. SetTime(&TimePrint_2, 2000); //2s打印
    73. printf( "TimePrint_2s \r\n");
    74. }
    75. /* USER CODE END WHILE */
    76. /* USER CODE BEGIN 3 */
    77. }
    78. /* USER CODE END 3 */
    79. }

     

  5. 运行效果



    总结:通过结果可以看出,是可以模拟出多任务处理的机制,其实也就是时间片轮法形式的多线程任务。

 


转载:https://blog.csdn.net/qq_36075612/article/details/105432947
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场