FreeRTOS计数型值信号量

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "os_start.h"

#define START_TASK_PRIO		1
#define START_STK_SIZE 		128  
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);

#define KeyScan_TASK_PRIO		2
#define KeyScan_STK_SIZE 		50  
TaskHandle_t KeyScanTask_Handler;
void KeyScan_task(void *pvParameters);


#define KEYPROCESS_TASK_PRIO		3
#define KEYPROCESS_STK_SIZE 		50  
TaskHandle_t KeyprocessTask_Handler;
void Keyprocess_task(void *pvParameters);

#define KEYMSG_Q_LENGTH	1   //队列长度
#define RXMSG_Q_LENGTH	4

#define RX_BUF_NUM          30
uint8_t Rx_Buf[RX_BUF_NUM];
uint8_t Rx_HAL_Buff_Temp[2];	//HAL函数接收临时缓存

QueueHandle_t Key_Queue;			//按键队列
QueueHandle_t UartRX_Queue;			//消息队列
SemaphoreHandle_t CounterSemaphore;  //计数型信号量句柄

void os_start(void)
{
	User_SysTick_ReConfig(configTICK_RATE_HZ);
  HAL_UART_Receive_IT(&huart6,Rx_HAL_Buff_Temp,1);

	//创建开始任务
    xTaskCreate((TaskFunction_t )start_task,            //任务函数
                (const char*    )"start_task",          //任务名称
                (uint16_t       )START_STK_SIZE,        //任务堆栈大小
                (void*          )NULL,                  //传递给任务函数的参数
                (UBaseType_t    )START_TASK_PRIO,       //任务优先级
                (TaskHandle_t*  )&StartTask_Handler);   //任务句柄  
								
    vTaskStartScheduler();          //开启任务调度
	
}

//开始任务任务函数
void start_task(void *pvParameters)
{
    taskENTER_CRITICAL();           //进入临界区
	
	//创建队列
	Key_Queue = xQueueCreate(KEYMSG_Q_LENGTH,1);
	UartRX_Queue = xQueueCreate(RXMSG_Q_LENGTH,1);
  CounterSemaphore = xSemaphoreCreateCounting(10,0); //最大255个计数,初始值为0
	if(CounterSemaphore == NULL)
  {
    printf("CounterSemaphore Creat Fail!\r\n");           
  }    
  
  //创建LED1任务
  xTaskCreate((TaskFunction_t )KeyScan_task,     
              (const char*    )"led1_task",   
              (uint16_t       )KeyScan_STK_SIZE, 
              (void*          )NULL,
              (UBaseType_t    )KeyScan_TASK_PRIO,
              (TaskHandle_t*  )&KeyScanTask_Handler);        
  //创建key任务
  xTaskCreate((TaskFunction_t )Keyprocess_task,     	
              (const char*    )"keyprocess_task",   	
              (uint16_t       )KEYPROCESS_STK_SIZE, 
              (void*          )NULL,				
              (UBaseType_t    )KEYPROCESS_TASK_PRIO,	
              (TaskHandle_t*  )&KeyprocessTask_Handler);   

  vTaskDelete(StartTask_Handler); //删除开始任务
  taskEXIT_CRITICAL();            //退出临界区
}  

//KeyScan任务函数
void KeyScan_task(void *pvParameters)
{
  while(1)
  {
    //按键检测
    if(KeyScan((uint8_t *)&KeyValue) == 0)
    {
      if(KeyValue == 3)
      {
        if( CounterSemaphore != NULL )
        {
          if(xSemaphoreGive( CounterSemaphore ) == pdTRUE )
          {
            
          }
          else
          {
            printf("CounterSemaphore Send Fail!\r\n");           
          }
        }
        KeyValue = 0;
      }
    }
    vTaskDelay(10);
  }
}

//key任务函数
void Keyprocess_task(void *pvParameters)
{
  BaseType_t ret,semVal;
  
  while(1)
  {
    if(CounterSemaphore != NULL)
    {
      ret = xSemaphoreTake(CounterSemaphore,portMAX_DELAY);
      if(ret == pdTRUE)
      {
        semVal = uxSemaphoreGetCount(CounterSemaphore);
        printf("counter = %d\r\n",(uint8_t)semVal);
      }
    }
    else
    {
      vTaskDelay(1000);
    }
    vTaskDelay(1000);
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值