计数型信号量简介:
计数型信号量的创建:
计数型信号量动态创建函数:
释放和获取信号量(与二值信号量相同)
释放信号量:
获取信号量:
测试实验
用按键来模拟事件,按键按下后表示有事件发生,则释放计数型信号量。
创建两个任务,任务1用于按键检测和释放信号量,按键2用于获取信号量。
测试程序如下所示:
void start_task(void *pvParameters)
count_semphr = xSemaphoreCreateCounting(5, 0);
if (count_semphr != NULL)
xTaskCreate((TaskFunction_t ) task1_task,
(uint16_t ) TASK1_TASK_SIZE,
(UBaseType_t ) TASK1_TASK_PRIO,
(TaskHandle_t * ) &Task1_Handle);
xTaskCreate((TaskFunction_t )task2_task,
(uint16_t )TASK2_TASK_SIZE,
(UBaseType_t )TASK2_TASK_PRIO,
(TaskHandle_t * )&Task2_Handle);
vTaskDelete(Start_Task_Handle);
void task1_task(void *pvParameters)
if (key_scan(KEY1_GPIO_Port, KEY1_Pin) == KEY_ON)
if (count_semphr != NULL)
error_state = xSemaphoreGive(count_semphr);
if (error_state == pdTRUE)
void task2_task(void *pvParameters)
UBaseType_t current_semphr_value;
if (key_scan(KEY2_GPIO_Port, KEY2_Pin) == KEY_ON)
if (count_semphr != NULL)
error_state = xSemaphoreTake(count_semphr, portMAX_DELAY);
if (error_state == pdTRUE)
current_semphr_value = uxSemaphoreGetCount(count_semphr);
printf("当前信号量计数值为%d\n", (int)current_semphr_value);
测试结果:
转载于:https://blog.csdn.net/dingyc_ee/article/details/104114161
文章评论(0条评论)
登录后参与讨论