简介
使用UCOS管理OLED+DHT11温湿度任务 将DHT11读取到的温湿度在OLED上显示 OLED以及DHT11的头文件及源文件可参考之前的博文 DHT11模块:DHT11 OLED模块:OLED
main函数
#include "stm32f10x.h"
#include <stdio.h>
#include "includes.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "dht11.h"
#include "oled.h"
OS_TCB Start_Task_TCB;
#define START_TASK_PRIO 3
void Start_Task(void *parg);
#define START_STK_SIZE 512
CPU_STK START_TASK_STK[START_STK_SIZE];
OS_TCB OLED_Task_TCB;
#define OLED_TASK_PRIO 7
void OLED_Task(void *parg);
#define OLED_STK_SIZE 128
CPU_STK OLED_TASK_STK[OLED_STK_SIZE];
OS_TCB DHT11_Task_TCB;
#define DHT11_TASK_PRIO 6
void DHT11_Task(void *parg);
#define DHT11_STK_SIZE 128
CPU_STK DHT11_TASK_STK[DHT11_STK_SIZE];
OS_MUTEX mutex_dht11;
OS_MUTEX mutex_oled;
int main(void)
{
OS_ERR err;
delay_init();
Usart_Init();
OSInit(&err);
OSTaskCreate((OS_TCB * )&Start_Task_TCB,
(CPU_CHAR * )"Start_Task",
(OS_TASK_PTR )Start_Task,
(void * )0,
(OS_PRIO )START_TASK_PRIO,
(CPU_STK * )&START_TASK_STK[0],
(CPU_STK_SIZE)START_STK_SIZE/10,
(CPU_STK_SIZE)START_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err );
OSStart(&err);
while(1)
{
printf("System Star Err!!!\r\n");
}
}
void Start_Task(void *parg)
{
u8 re=0;
OS_ERR err;
OLED_Init();
re=DHT11_Init();
if(re)
{
printf("DHT11 Init Err!\r\n");
}else
{
printf("OLED DHT11 Init Success!!\r\n");
}
OSMutexCreate ( &mutex_dht11,"mutex_dht11",&err);
OSMutexCreate ( &mutex_oled,"mutex_oled",&err);
OSTaskCreate((OS_TCB * )&OLED_Task_TCB,
(CPU_CHAR * )"OLED_Task",
(OS_TASK_PTR )OLED_Task,
(void * )0,
(OS_PRIO )OLED_TASK_PRIO,
(CPU_STK * )&OLED_TASK_STK[0],
(CPU_STK_SIZE)OLED_STK_SIZE/10,
(CPU_STK_SIZE)OLED_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_NONE,
(OS_ERR * )&err);
OSTaskCreate((OS_TCB * )&DHT11_Task_TCB,
(CPU_CHAR * )"DHT11_Task",
(OS_TASK_PTR )DHT11_Task,
(void * )0,
(OS_PRIO )DHT11_TASK_PRIO,
(CPU_STK * )&DHT11_TASK_STK[0],
(CPU_STK_SIZE)DHT11_STK_SIZE/10,
(CPU_STK_SIZE)DHT11_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_NONE,
(OS_ERR * )&err);
delay_ms(500);
OSTaskDel((OS_TCB*)0,&err);
}
void OLED_Task(void *parg)
{
OS_ERR err;
CPU_SR_ALLOC();
while(1)
{
CPU_CRITICAL_ENTER();
printf("OLED task run!\r\n");
CPU_CRITICAL_EXIT();
delay_ms(1000);
}
}
void DHT11_Task(void *parg)
{
OS_ERR err;
u8 humi=0;
u8 temp=0;
u8 rt=0;
while(1)
{
OSMutexPend(&mutex_dht11,0,OS_OPT_PEND_BLOCKING,NULL,&err);
delay_ms(1000);
rt = DHT11_Read_Data(&temp,&humi);
printf("dht11 task run\r\n");
if(!rt)
{
OSMutexPend(&mutex_oled,0,OS_OPT_PEND_BLOCKING,NULL,&err);
OLED_ShowChinese(0,0,3,16,1);
OLED_ShowChinese(16,0,4,16,1);
OLED_ShowChar(32,0,':',16,1);
OLED_ShowNum(48,0,temp,2,16);
OLED_ShowChinese(0,16,5,16,1);
OLED_ShowChinese(16,16,4,16,1);
OLED_ShowChar(32,16,':',16,1);
OLED_ShowNum(48,16,humi,2,16);
OLED_Refresh_Gram();
OSMutexPost (&mutex_oled,OS_OPT_POST_NONE,&err);
}
OSMutexPost (&mutex_dht11,OS_OPT_POST_NONE,&err);
delay_ms(4000);
}
}
实验结果
串口打印:
OLED显示:
|