开发环境:CubeMX Version:5.6.1
Keil:Version:V5.31.0
FreeRTOS: V2.0参考API连接
1. 二值信号量的使用
功能:控制线程中按键按下,则控制释放信号量,显示线程中检测到信号量,数值++,显示数值
/* Definitions for NIXIE_TUBE */
osThreadId_t NIXIE_TUBEHandle;
const osThreadAttr_t NIXIE_TUBE_attributes = {
.name = "NIXIE_TUBE",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
/* Definitions for KEY_CONTROL */
osThreadId_t KEY_CONTROLHandle;
const osThreadAttr_t KEY_CONTROL_attributes = {
.name = "KEY_CONTROL",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
Key_PressedHandle = osSemaphoreNew(1, 1, &Key_Pressed_attributes);
/* USER CODE BEGIN Header_Nixie_Tube */
/**
* @brief Function implementing the NIXIE_TUBE thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Nixie_Tube */
void Nixie_Tube(void *argument)
{
/* USER CODE BEGIN Nixie_Tube */
/* Infinite loop */
for(;;)
{
osSemaphoreAcquire (Key_PressedHandle, -1);
TM1640_display(ADDRESS_TM1640_COM3,Nixie_Tube_Num/2);
TM1640_display(ADDRESS_TM1640_COM7,Nixie_Tube_Num/2);
Nixie_Tube_Num++;
if(Nixie_Tube_Num>9)
Nixie_Tube_Num=0;
// LL_mDelay(1000);
// osDelay(1);
}
/* USER CODE END Nixie_Tube */
}
/* Create the thread(s) */
/* creation of defaultTask */
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* creation of NIXIE_TUBE */
NIXIE_TUBEHandle = osThreadNew(Nixie_Tube, NULL, &NIXIE_TUBE_attributes);
/* creation of KEY_CONTROL */
KEY_CONTROLHandle = osThreadNew(Key_Control, NULL, &KEY_CONTROL_attributes);
/* USER CODE BEGIN Header_Key_Control */
/**
* @brief Function implementing the KEY_CONTROL thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Key_Control */
void Key_Control(void *argument)
{
/* USER CODE BEGIN Key_Control */
/* Infinite loop */
for(;;)
{
EncodeKey=ctl_encoder_handler();
if(EncodeKey != NULL)
{
printf("%d
",EncodeKey->KeyValue);
osSemaphoreRelease (Key_PressedHandle);
}
PressKey=ctl_key_handler();
if(PressKey->click)
{
printf("%d
",PressKey->KeyValue);
PressKey->click = 0;
}
//osDelay(1);
}
/* USER CODE END Key_Control */
}
总结:
/*显示任务中请求信号量,信号量未释放,则任务处于阻塞状态*/
osSemaphoreAcquire (Key_PressedHandle, -1);
/*第一个参数为二值信号量的句柄,第二个参数,-1,则死等,直至对应的信号量释放,正数数值,例如1000,为时间片*1000时间后解除阻塞*/
/*控制任务中释放信号量,当有按键按下时,则释放信号量,显示任务中检测到信号量的释放,数值++,并显示*/
osSemaphoreRelease (Key_PressedHandle);
/*参数为对应信号量的句柄*/
2. 计数信号量的使用:
功能:控制线程中,按键按下一次,计数信号量++,显示线程中,获取信号量计数,当信号量计数==某个值,执行线程
/* Definitions for CS_PressTimes */
osSemaphoreId_t CS_PressTimesHandle;
const osSemaphoreAttr_t CS_PressTimes_attributes = {
.name = "CS_PressTimes"
};
/* creation of CS_PressTimes */
CS_PressTimesHandle = osSemaphoreNew(100, 1, &CS_PressTimes_attributes);
/* USER CODE BEGIN Header_Nixie_Tube */
/**
* @brief Function implementing the NIXIE_TUBE thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Nixie_Tube */
void Nixie_Tube(void *argument)
{
/* USER CODE BEGIN Nixie_Tube */
/* Infinite loop */
for(;;)
{
//osSemaphoreAcquire (CS_PressTimesHandle, -1);
if(osSemaphoreGetCount (CS_PressTimesHandle)%10 == 0)
{
TM1640_display(ADDRESS_TM1640_COM3,Nixie_Tube_Num/2);
TM1640_display(ADDRESS_TM1640_COM7,Nixie_Tube_Num/2);
Nixie_Tube_Num++;
if(Nixie_Tube_Num>9)
Nixie_Tube_Num=0;
}
// LL_mDelay(1000);
// osDelay(1);
}
/* USER CODE END Nixie_Tube */
}
/* USER CODE BEGIN Header_Key_Control */
/**
* @brief Function implementing the KEY_CONTROL thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Key_Control */
void Key_Control(void *argument)
{
/* USER CODE BEGIN Key_Control */
/* Infinite loop */
for(;;)
{
EncodeKey=ctl_encoder_handler();
if(EncodeKey != NULL)
{
printf("%d
",EncodeKey->KeyValue);
osSemaphoreRelease (CS_PressTimesHandle);
printf("osSemaphoreGetCount (CS_PressTimesHandle):%d
",osSemaphoreGetCount (CS_PressTimesHandle));
}
PressKey=ctl_key_handler();
if(PressKey->click)
{
printf("%d
",PressKey->KeyValue);
PressKey->click = 0;
}
//osDelay(1);
}
/* USER CODE END Key_Control */
}
总结:
/*二值信号量其实就是特殊的计数信号量,计数信号量释放可设置,二值信号量释放只有0-1*/
CS_PressTimesHandle = osSemaphoreNew(100, 1, &CS_PressTimes_attributes);
//函数原型:osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr)
/*第一个参数:计数信号量最大计数到多少,第二个参数,计数信号量的初始值,*/
/*每释放一次信号量,信号量的计数在初始值的基础上+1,所以,CubeMX只初始化最大计数,用户自己修改初始值*/
/*通过osSemaphoreGetCount (CS_PressTimesHandle)获取信号量的计数值,当达到某个计数值的时候处理任务*/