基于物联网技术的智慧病房管理系统(二)—— RTOS、AHT20 与 按钮功能实现
基于物聯網技術的智慧病房管理系統(二)—— RTOS、AHT20 與 按鈕功能實現
這里 RTOS 代碼修改,AHT20 驅動代碼移植可以參考前面博客內容:
STM32 —— IIC 讀取 ATH20(DTH20)溫度傳感器
STM32 —— RT-Thread Nano 移植
這里主要介紹使用和按鍵相關內容
溫濕度獲取代碼設計
首先我們需要引入溫濕度驅動頭文件:
#include "AHT20.h"然后我們需要在進程中對溫濕度傳感器進行初始化,這里在初始化之后,不能立即獲取溫濕度,需要等待 2 秒或以上,確保數據正確性:
MX_GPIO_Init(); MX_I2C1_Init(); MX_USART1_UART_Init(); uint32_t CT_data[2]={0,0}; // 用于獲取溫濕度數據 volatile int c1,t1; rt_thread_delay(50); AHT20_Init(); rt_thread_delay(2500);然后我們需要獲取經過 CRC 驗證的溫濕度數據,這里直接調用驅動函數即可:
while(1){AHT20_Read_CTdata_crc(CT_data); //經過CRC校驗,讀取AHT20的溫度和濕度數據 推薦每隔大于1S讀一次c1 = CT_data[0]*1000/1024/1024; //計算得到濕度值c1(放大了10倍)t1 = CT_data[1]*2000/1024/1024-500;//計算得到溫度值t1(放大了10倍)printf("正在檢測");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");printf("\r\n");printf("溫度:%d%d.%d",t1/100,(t1/10)%10,t1%10); // 這里需要對溫度進行計算后才能得到我們需要的溫度值printf("濕度:%d%d.%d",c1/100,(c1/10)%10,c1%10); // 這里同樣需要對適度進行計算printf("\r\n");printf("等待");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");printf("\r\n");}RTOS 進程設計
首先新建 app_rt_thread.c 文件,然后引入頭文件:
#include "rtthread.h" #include "main.h" #include "i2c.h" #include "usart.h" #include "gpio.h" #include "stdio.h" #include "AHT20.h"然后我們需要設計進程,這里主進程先設置為串口發送,子進程設置兩個,一個是獲取溫度參數,另一個是控制 PC13 引腳上的板載 LED ,后續會根據系統進程對進行設計:
//初始化線程函數 void MX_RT_Thread_Init(void) {//初始化LED1線程rt_thread_init(&led1_thread,"led1",led1_task_entry,RT_NULL,&rt_led1_thread_stack[0],sizeof(rt_led1_thread_stack),3,20);//開啟線程調度rt_thread_startup(&led1_thread);//初始化USART1線程rt_thread_init(&usart1_thread,"usart1",usart1_task_entry,RT_NULL,&rt_usart1_thread_stack[0],sizeof(rt_usart1_thread_stack),3,20);//開啟線程調度rt_thread_startup(&usart1_thread); }//主任務 void MX_RT_Thread_Process(void) {printf("Hello RT_Thread!!!\r\n");rt_thread_delay(2000); }//LED1任務 void led1_task_entry(void *parameter) {while(1){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_RESET);rt_thread_delay(500);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_SET);rt_thread_delay(500);} } //讀取溫度任務 void usart1_task_entry(void *parameter) {MX_GPIO_Init();MX_I2C1_Init();MX_USART1_UART_Init();uint32_t CT_data[2]={0,0}; //volatile int c1,t1;rt_thread_delay(50);AHT20_Init();rt_thread_delay(2500);while(1){AHT20_Read_CTdata_crc(CT_data); //經過CRC校驗,讀取AHT20的溫度和濕度數據 推薦每隔大于1S讀一次c1 = CT_data[0]*1000/1024/1024; //計算得到濕度值c1(放大了10倍)t1 = CT_data[1]*2000/1024/1024-500;//計算得到溫度值t1(放大了10倍)printf("正在檢測");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");printf("\r\n");printf("溫度:%d%d.%d",t1/100,(t1/10)%10,t1%10); // 這里需要對溫度進行計算后才能得到我們需要的溫度值printf("濕度:%d%d.%d",c1/100,(c1/10)%10,c1%10); // 這里同樣需要對適度進行計算printf("\r\n");printf("等待");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");printf("\r\n");} }這里直接給出完整代碼如下:
點擊查看完整 app_rt_thread.c 代碼 #include "rtthread.h" #include "main.h" #include "i2c.h" #include "usart.h" #include "gpio.h" #include "stdio.h" #include "AHT20.h"struct rt_thread led1_thread; struct rt_thread usart1_thread; rt_uint8_t rt_led1_thread_stack[128]; rt_uint8_t rt_usart1_thread_stack[256]; void led1_task_entry(void *parameter); void usart1_task_entry(void *parameter);int fputc(int ch,FILE *f) {HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xFFFF); //等待發送結束 while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)!=SET){} return ch; }//初始化線程函數 void MX_RT_Thread_Init(void) {//初始化LED1線程rt_thread_init(&led1_thread,"led1",led1_task_entry,RT_NULL,&rt_led1_thread_stack[0],sizeof(rt_led1_thread_stack),3,20);//開啟線程調度rt_thread_startup(&led1_thread);//初始化USART1線程rt_thread_init(&usart1_thread,"usart1",usart1_task_entry,RT_NULL,&rt_usart1_thread_stack[0],sizeof(rt_usart1_thread_stack),3,20);//開啟線程調度rt_thread_startup(&usart1_thread); }//主任務 void MX_RT_Thread_Process(void) {printf("Hello RT_Thread!!!\r\n");rt_thread_delay(2000); }//LED1任務 void led1_task_entry(void *parameter) {while(1){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_RESET);rt_thread_delay(500);HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_SET);rt_thread_delay(500);} } //讀取溫度任務 void usart1_task_entry(void *parameter) {MX_GPIO_Init();MX_I2C1_Init();MX_USART1_UART_Init();uint32_t CT_data[2]={0,0}; //volatile int c1,t1;rt_thread_delay(50);AHT20_Init();rt_thread_delay(2500);while(1){AHT20_Read_CTdata_crc(CT_data); //經過CRC校驗,讀取AHT20的溫度和濕度數據 推薦每隔大于1S讀一次c1 = CT_data[0]*1000/1024/1024; //計算得到濕度值c1(放大了10倍)t1 = CT_data[1]*2000/1024/1024-500;//計算得到溫度值t1(放大了10倍)printf("正在檢測");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");printf("\r\n");printf("溫度:%d%d.%d",t1/100,(t1/10)%10,t1%10); // 這里需要對溫度進行計算后才能得到我們需要的溫度值printf("濕度:%d%d.%d",c1/100,(c1/10)%10,c1%10); // 這里同樣需要對適度進行計算printf("\r\n");printf("等待");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");rt_thread_delay(100);printf(".");printf("\r\n");} }然后我們在主函數中引入 RT-Thread 必要的頭文件并引用函數:
#include "rtthread.h"extern void MX_RT_Thread_Init(void); extern void MX_RT_Thread_Process(void);最后我們直接在主函數中對進程進行初始化,并運行即可,這里直接給出完整主函數:
int main(void) {/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_I2C1_Init();MX_TIM3_Init();MX_USART1_UART_Init();MX_TIM2_Init();/* USER CODE BEGIN 2 */MX_RT_Thread_Init(); // 初始化線程/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){MX_RT_Thread_Process(); // 執行主進程/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */ }按鈕的使用與消抖
首先我們設計按鈕在 PA3 引腳,然后在設置 PA4 引腳上外接一個 LED ,這里先令按鈕按下反轉 PA4 引腳電平,即按下小燈泡點亮,再次按下,小燈泡熄滅,這里直接設計為一個新進程:
struct rt_thread btnclick_thread; rt_uint8_t rt_btnclick_thread_stack[128]; void btnclick_task_entry(void *parameter);void btnclick_task_entry(void *parameter){while(1){switch(KEY_Scan(0)){ case KEY1_PRES: HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_4);break;default:break;}} }初始化線程:
//初始化線程 rt_thread_init(&btnclick_thread,"btnclick",btnclick_task_entry,RT_NULL,&rt_btnclick_thread_stack[0],sizeof(rt_btnclick_thread_stack),3,20); //開啟線程調度 rt_thread_startup(&btnclick_thread);但是我們不進行按鍵消抖,這里按鍵非常容易誤觸,所以這里進行按鍵消抖:
#define KEY1 HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3) //讀取按鍵1 #define KEY1_PRES 1 //KEY1按下 uint8_t KEY_Scan(uint8_t mode) {static uint8_t key_up=1;//按鍵松開標志位if(key_up&&(KEY1==0)){HAL_Delay(10);//去抖動key_up=0;if(KEY1==0)return KEY1_PRES;}else if(KEY1==1)key_up=1;return 0;//無按鍵按下 }這樣我們就完成了對 RTOS 移植,AHT20 溫濕度數據獲取和按鍵功能的實現。
總結
以上是生活随笔為你收集整理的基于物联网技术的智慧病房管理系统(二)—— RTOS、AHT20 与 按钮功能实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宝宝成长记
- 下一篇: 教程 | 阿克曼结构移动机器人的gaze