[uCOS/RTOS] 计数型信号量的问题

[复制链接]
 楼主| 比神乐 发表于 2024-12-9 14:06 | 显示全部楼层 |阅读模式
代码:
  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. /** @addtogroup UTILITIES_examples
  8.   * @{
  9.   */
  10. SemaphoreHandle_t CountSemaphore;
  11. //unsigned char flag=0;
  12. /** @addtogroup FreeRTOS_demo
  13.   * @{
  14.   */
  15. button_type key=1;       
  16. //extern QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount,
  17.                                                  //const UBaseType_t uxInitialCount );

  18. static void vPeriodicTassk(void *pvParamters)
  19. {
  20.         unsigned int i;
  21.         UBaseType_t err;
  22.     while(1)
  23.     {       
  24.                                 key=at32_adatin_state();
  25.                         if(key==0)
  26.                         {       
  27.                                         for(i=0;i<3000;i++)
  28.                                         {
  29.                                                 key=at32_adatin_state();
  30.                                                 if(key==0)
  31.                                                 {
  32.                                                         while(1)
  33.                                                         {
  34.                                                                 key=at32_adatin_state();
  35.                                                                 if(key==1)
  36.                                                                         break;
  37.                                                         }
  38.                                         if(CountSemaphore!=NULL)
  39.                                         {
  40.                                                 err=xSemaphoreGive(CountSemaphore);
  41.                                                
  42.                                                 if(err==pdFALSE)
  43.                                                 {
  44.                                                         printf("Sem Rele Failed!\r\n");
  45.                                                         //flag=0;
  46.                                                 }
  47.                                                 else
  48.                                                 {
  49.                                                         printf("Sem Role Succeed!\r\n");
  50.                                                         //flag=1;
  51.                                                 }
  52.                                         }
  53.                                 }
  54.                         }
  55.                                
  56.         
  57.                                 //LED0_TOGGLE();
  58.         //vTaskDelay(10);
  59.                                 }/* 延时1000ticks */
  60.     }
  61. }
  62. static void vHandlerTask(void *pvParamters)
  63. {
  64.         unsigned int i;
  65.         UBaseType_t semavalue=0;
  66.     while(1)
  67.     {
  68.                        
  69.                         if(CountSemaphore!=NULL)
  70.                         {
  71.                                 //if(flag==1)
  72.                                 //{
  73.                                 //        flag=0;
  74.                                 xSemaphoreTake(CountSemaphore,portMAX_DELAY);
  75.                                 semavalue=uxSemaphoreGetCount(CountSemaphore);
  76.                                        
  77.                                         printf("semavalue value=%d\r\n",(int)semavalue);
  78.                                 //}
  79.                         }
  80.                         //else
  81.                         //{
  82.                         //        for(i=0;i<1000;i++);
  83.                         //}
  84.                                 //printf("Task2 Running!\r\n");
  85.                                 //vTaskDelay(1000);
  86.                         for(i=0;i<60000;i++);
  87.     }
  88. }
  89. /**
  90.   * [url=home.php?mod=space&uid=247401]@brief[/url]  main function.
  91.   * @param  none
  92.   * @retval none
  93.   */
  94. int main(void)
  95. {
  96.   system_clock_config();
  97.         at32_board_init();
  98.   /* init led2 and led3 */
  99.   at32_led_init(LED2);
  100.   at32_led_init(LED3);

  101.   /* init usart1 */
  102.   uart_print_init(115200);
  103.        
  104.   /* enter critical */
  105.   taskENTER_CRITICAL();
  106.         CountSemaphore=xSemaphoreCreateCounting(10,10);
  107.                 if(CountSemaphore==NULL)
  108.                 {
  109.                         printf("create CountSemaphore failed!\r\n");
  110.                 }
  111.   /* create led2 task */
  112.   xTaskCreate((TaskFunction_t )vPeriodicTassk,
  113.                  (const char*    )"vTask1",
  114.                  (uint16_t       )128,
  115.                  (void*          )NULL,
  116.                  (UBaseType_t    )2,
  117.                  (TaskHandle_t*  )NULL);
  118.   
  119.   /* create led3 task */
  120.   xTaskCreate((TaskFunction_t )vHandlerTask,
  121.                  (const char*    )"vTask2",
  122.                  (uint16_t       )128,
  123.                  (void*          )NULL,
  124.                  (UBaseType_t    )2,
  125.                  (TaskHandle_t*  )NULL) ;
  126.   

  127.   /* exit critical */
  128.   taskEXIT_CRITICAL();

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


  132. /**
  133.   * @}
  134.   */

  135. /**
  136.   * @}
  137.   */
这个程序,一运行就收到信号量

到零后,按下按键,发送成功,可是收不到信号量。

请问高手,这个程序有什么问题?谢谢


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

470

主题

3535

帖子

7

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

470

主题

3535

帖子

7

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