[AT32L021] 【AT-START-L021测评】FREERTOS之计数信号量

[复制链接]
974|0
 楼主| 比神乐 发表于 2024-12-9 17:33 | 显示全部楼层 |阅读模式
代码:
  1. #include "at32l021_board.h"
  2. #include "at32l021_clock.h"
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include "queue.h"
  6. #include "semphr.h"
  7. #include "stdio.h"
  8. /** @addtogroup UTILITIES_examples
  9.   * @{
  10.   */
  11. int cnt=0;
  12. QueueHandle_t CountSemaphore;
  13. /** @addtogroup FreeRTOS_demo
  14.   * @{
  15.   */
  16. button_type key=1;
  17. //unsigned char const mainSENDER_1=30;
  18. //unsigned char const mainSENDER_2=50;
  19. button_type at32_button_press(void);
  20. void task1(void *pvParameters)
  21. {
  22.     unsigned int i;
  23.         UBaseType_t err;
  24.     while(1)
  25.     {
  26.                                 key=at32_button_state();
  27.                         if(key==1)
  28.                         {
  29.                                         for(i=0;i<3000;i++);
  30.                                         key=at32_button_state();
  31.                         if(key==1)
  32.                         {
  33.                                 while(1)
  34.                                 {
  35.                                         key=at32_button_state();
  36.                                         if(key==0)
  37.                                                 break;
  38.                                 }
  39.                                 err=xSemaphoreGive(CountSemaphore);
  40.                                        
  41.                                         if(err==pdFALSE)
  42.                                         {
  43.                                                 printf("Sem Rele Failed!\r\n");
  44.                                                 //flag=0;
  45.                                         }
  46.                                         else
  47.                                         {
  48.                                                 printf("Sem Role Succeed!\r\n");
  49.                                                 //flag=1;
  50.                                         }
  51.                                 }
  52.                                 }/* 延时1000ticks */
  53.                         vTaskDelay(10);
  54.                                 taskYIELD();
  55.     }
  56. }

  57. /**
  58. * [url=home.php?mod=space&uid=247401]@brief[/url]       task2
  59. * @param       pvParameters : 传入参数(未用到)
  60. * @retval      无
  61. */

  62. void Keyprocess_task(void *pvParameters)
  63. {
  64.     unsigned int i=0;
  65.         UBaseType_t semavalue=0;
  66.     while(1)
  67.     {
  68.                        
  69.                         if(CountSemaphore!=NULL)
  70.                         {
  71.                                
  72.                                         xSemaphoreTake(CountSemaphore,portMAX_DELAY);
  73.                                 semavalue=uxSemaphoreGetCount(CountSemaphore);
  74.                                        
  75.                                         printf("semavalue value=%d\r\n",(int)semavalue);
  76.                        
  77.                                
  78.                         }
  79.                         else
  80.                         {
  81.                                 vTaskDelay(10);
  82.                         }
  83.                                 //printf("Task2 Running!\r\n");
  84.                                 vTaskDelay(1000);
  85. }


  86. }
  87. /**
  88.   * @brief  main function.
  89.   * @param  none
  90.   * @retval none
  91.   */
  92. int main(void)
  93. {
  94.   system_clock_config();
  95.         delay_init();
  96.   /* init led2 and led3 */
  97.   at32_led_init(LED2);
  98.   at32_led_init(LED3);
  99.         at32_button_init();
  100.   /* init usart1 */
  101.   uart_print_init(115200);
  102.         taskENTER_CRITICAL();
  103.         CountSemaphore=xSemaphoreCreateCounting(10,10);
  104.                 if(CountSemaphore==NULL)
  105.                 {
  106.                         printf("create CountSemaphore failed!\r\n");
  107.                 }
  108.   
  109.        
  110.        
  111.                 /* create led2 task */
  112.                 xTaskCreate(task1,
  113.                  "vTask1",
  114.                  128,
  115.                 (void *)30,
  116.                  
  117.                                                                  2,
  118.                 NULL);
  119.                
  120.                 xTaskCreate(Keyprocess_task,
  121.                  "vTask2",
  122.                  128,
  123.                  NULL,
  124.                  3,
  125.                  NULL);
  126.                
  127.          taskEXIT_CRITICAL();

  128.                 /* start scheduler */
  129.                 vTaskStartScheduler();
  130.        
  131. }

运行效果图:
2.jpg 3.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部