收藏官网首页
查看: 12772|回复: 0

【一起来玩RTOS系列】之RT-Thread 软件定时器-静态方法

321

主题

1054

帖子

4502

积分

论坛元老

Rank: 8Rank: 8

积分
4502
QQ
跳转到指定楼层
楼主
发表于 2017-11-18 21:41:06 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
注册成为机智云开发者,手机加虚拟设备快速开发

除了上篇涉及的动态定时器创建方法,RT-Thread还提供了一个静态的定时器创建方法,使用示例如下:

  1. /**
  2.   ****************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ****************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2017 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ****************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "STM32f1xx_hal.h"
  41. #include "usart.h"
  42. #include "gpio.h"

  43. /* USER CODE BEGIN Includes */
  44. #include "rtthread.h"
  45. /* USER CODE END Includes */

  46. /* Private variables ---------------------------------------------------------*/

  47. /* USER CODE BEGIN PV */
  48. /* Private variables ---------------------------------------------------------*/

  49. /* USER CODE END PV */

  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);

  52. /* USER CODE BEGIN PFP */
  53. /* Private function prototypes -----------------------------------------------*/

  54. /* USER CODE END PFP */

  55. /* USER CODE BEGIN 0 */
  56. //重映射串口1到rt_kprintf
  57. void rt_hw_console_output(const char *str)
  58. {
  59.     /* empty console output */
  60.         char aa='\r';
  61.           rt_enter_critical();

  62.                 while(*str!='\0')
  63.                 {
  64.                         if(*str=='\n')
  65.                         {
  66.                                 HAL_UART_Transmit(&huart1, (uint8_t *)&aa, 1, 10);
  67.                         }
  68.                                 HAL_UART_Transmit(&huart1, (uint8_t *)(str++), 1, 10);
  69.                 }
  70.                
  71.                 rt_exit_critical();
  72. }
  73. //线程LED1
  74. static void led1_thread_entry(void* parameter)
  75. {
  76.        while(1)
  77.        {
  78.              //LED1_Toggle();
  79.              rt_thread_delay(500);   //延时
  80.                                                  rt_kprintf("this is thread 1\r\n");
  81.        }
  82. }
  83. //线程LED2
  84. static void led2_thread_entry(void* parameter)
  85. {
  86.        while(1)
  87.        {
  88.              //LED2_Toggle();
  89.              rt_thread_delay(100);   //延时
  90.                                                  rt_kprintf("this is thread 2\r\n");
  91.        }
  92. }

  93. /*
  94. * 程序清单:定时器控制接口示例
  95. *
  96. * 这个例程会创建1个动态周期型定时器对象,然后控制它进行更改定时器的时间长度。
  97. */
  98. #include <rtthread.h>

  99. /* 定时器的控制块 */
  100. struct rt_timer timer1;
  101. static rt_uint8_t count;

  102. /* 定时器超时函数 */
  103. static void timeout1(void* parameter)
  104. {
  105.     rt_kprintf("periodic timer is timeout\n");

  106.     count ++;
  107.        
  108.         LED3_Toggle();
  109.         LED4_Toggle();
  110.     /* 当超过4次时,更改定时器的超时长度 */
  111.     if (count > 4)
  112.     {
  113.         int timeout_value = 100;
  114.         /* 控制定时器更改定时器超时时间长度 */
  115.         rt_timer_control(&timer1, RT_TIMER_CTRL_SET_TIME, (void*)&timeout_value);
  116.         count = 0;
  117.     }
  118. }


  119. /* USER CODE END 0 */

  120. int main(void)
  121. {

  122.   /* USER CODE BEGIN 1 */

  123.   /* USER CODE END 1 */

  124.   /* MCU Configuration----------------------------------------------------------*/

  125. //  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  126. //  HAL_Init();

  127. //  /* USER CODE BEGIN Init */

  128. //  /* USER CODE END Init */

  129. //  /* Configure the system clock */
  130. //  SystemClock_Config();

  131. //  /* USER CODE BEGIN SysInit */

  132. //  /* USER CODE END SysInit */

  133. //  /* Initialize all configured peripherals */
  134. //  MX_GPIO_Init();
  135. //  MX_USART1_UART_Init();

  136.   /* USER CODE BEGIN 2 */
  137.        
  138.         rt_thread_t tid1=RT_NULL;//线程句柄
  139.         rt_thread_t tid2=RT_NULL;//线程句柄
  140.        
  141.         //创建动态线程
  142.         tid1=rt_thread_create("led1",//线程名字
  143.                              led1_thread_entry,//线程入口函数
  144.                              RT_NULL,//线程参数
  145.                              256,//线程栈大小
  146.                              3,//线程优先级
  147.                              20);//线程时间片
  148.         //启动线程       
  149.         rt_thread_startup(tid1);

  150.         //创建动态线程
  151.         tid2=rt_thread_create("led2",//线程名字
  152.                              led2_thread_entry,//线程入口函数
  153.                              RT_NULL,//线程参数
  154.                              256,//线程栈大小
  155.                              4,//线程优先级
  156.                              20);//线程时间片
  157.         //启动线程                                                                                                         
  158.         rt_thread_startup(tid2);
  159.        
  160.             /* 创建定时器1 */
  161.    rt_timer_init(&timer1,"timer1",  /* 定时器名字是 timer1 */
  162.                             timeout1, /* 超时时回调的处理函数 */
  163.                             RT_NULL, /* 超时函数的入口参数 */
  164.                             1000, /* 定时长度,以OS Tick为单位,即1000个OS Tick */
  165.                             RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
  166.     /* 启动定时器 */
  167.      rt_timer_start(&timer1);



  168.   /* USER CODE END 2 */

  169.   /* Infinite loop */
  170.   /* USER CODE BEGIN WHILE */
  171. //  while (1)
  172. //  {
  173.   /* USER CODE END WHILE */

  174.   /* USER CODE BEGIN 3 */
  175. //               
  176. //  }
  177.   /* USER CODE END 3 */

  178. }

  179. /** System Clock Configuration
  180. */
  181. void SystemClock_Config(void)
  182. {

  183.   RCC_OscInitTypeDef RCC_OscInitStruct;
  184.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  185.     /**Initializes the CPU, AHB and APB busses clocks
  186.     */
  187.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  188.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  189.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  190.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  191.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  192.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  193.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  194.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  195.   {
  196.     _Error_Handler(__FILE__, __LINE__);
  197.   }

  198.     /**Initializes the CPU, AHB and APB busses clocks
  199.     */
  200.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  201.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  202.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  203.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  204.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  205.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  206.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  207.   {
  208.     _Error_Handler(__FILE__, __LINE__);
  209.   }

  210.     /**Configure the Systick interrupt time
  211.     */
  212.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  213.     /**Configure the Systick
  214.     */
  215.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  216.   /* SysTick_IRQn interrupt configuration */
  217.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  218. }

  219. /* USER CODE BEGIN 4 */

  220. /* USER CODE END 4 */

  221. /**
  222.   * @brief  This function is executed in case of error occurrence.
  223.   * @param  None
  224.   * @retval None
  225.   */
  226. void _Error_Handler(char * file, int line)
  227. {
  228.   /* USER CODE BEGIN Error_Handler_Debug */
  229.   /* User can add his own implementation to report the HAL error return state */
  230.   while(1)
  231.   {
  232.   }
  233.   /* USER CODE END Error_Handler_Debug */
  234. }

  235. #ifdef USE_FULL_ASSERT

  236. /**
  237.    * @brief Reports the name of the source file and the source line number
  238.    * where the assert_param error has occurred.
  239.    * @param file: pointer to the source file name
  240.    * @param line: assert_param error line source number
  241.    * @retval None
  242.    */
  243. void assert_failed(uint8_t* file, uint32_t line)
  244. {
  245.   /* USER CODE BEGIN 6 */
  246.   /* User can add his own implementation to report the file name and line number,
  247.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  248.   /* USER CODE END 6 */

  249. }

  250. #endif

  251. /**
  252.   * @}
  253.   */

  254. /**
  255.   * @}
  256. */

  257. /**************** (C) COPYRIGHT STMicroelectronics ****END OF FILE***/
复制代码
创建定时器后gokit板载LED每1S闪烁一次,4次后修改定时器为100ms闪烁一次,可以看到实验现象与预期一致:



源码下载:
Gokit_RT-Thread.zip (8.63 MB, 下载次数: 3, 售价: 1 金钱)


1、机智云QQ群:G1群:104975951 G2群:491509598 G3群:287087942
机智云爱好者-APP开发群: 599735135
QQ群目前非常活跃,欢迎大家参与进来,交流,讨论,答疑,解惑~~
2、机智云微信公众号: 机智云 gizwits、 机智云智能宠物屋go-iot
关注机智云Gizwits官方公众号随时掌握最新资讯和活动信息
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

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