基于STM32大棚DHT11温湿度监测的Proteus仿真
生活随笔
收集整理的這篇文章主要介紹了
基于STM32大棚DHT11温湿度监测的Proteus仿真
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本設計僅供學習參考
基于STM32大棚DHT11溫濕度監測的Proteus仿真 (代碼+仿真+原理圖+PCB)
原理圖:Altium Designer
仿真圖protues 8.9
程序編譯器:keil 5
編程語言:C語言
編號C0032
資料下載鏈接
主要功能:
1.液晶實時顯示DHT11溫度和濕度值;
2.具有溫濕度超上限閾值報警功能;
3.溫濕度上限值閾值可通過按鍵調節。
仿真圖(提供源文件):
電路圖:
PCB(未做實物驗證,僅供參考):
程序(提供源文件源碼):
main函數
獲取溫度函數
#include "ds18b20.h" #include "delay.h"//復位DS18B20 void DS18B20_Rst(void) { DS18B20_IO_OUT(); //設置為輸出DS18B20_DQ_OUT(0); //拉低DQdelay_us(740); //拉低750usDS18B20_DQ_OUT(1); //DQ=1 delay_us(15); //15US }//等待DS18B20的回應 //返回1:未檢測到DS18B20的存在 //返回0:存在 uint8_t DS18B20_Check(void) { uint8_t retry = 0;DS18B20_IO_IN(); //設置為輸入 while (DS18B20_DQ_IN&&retry < 200){retry ++;delay_us(2);}; if(retry >= 200)return 1;else retry = 0;while (!DS18B20_DQ_IN&&retry < 240){retry++;delay_us(2);};if(retry>=240)return 1; return 0; }//從DS18B20讀取一個位 //返回值:1/0 uint8_t DS18B20_Read_Bit(void) {uint8_t data = 0;DS18B20_IO_OUT(); //設置為輸出DS18B20_DQ_OUT(0); delay_us(2);DS18B20_DQ_OUT(1); DS18B20_IO_IN(); //設置為輸入delay_us(13);if(DS18B20_DQ_IN)data=1;else data=0;delay_us(50);return data; }//從DS18B20讀取一個字節 //返回值:讀到的數據 uint8_t DS18B20_Read_Byte(void) { uint8_t i, j = 0, dat = 0;for (i=1;i<=8;i++) {j=DS18B20_Read_Bit();dat=(j<<7)|(dat>>1);} return dat; }//寫一個字節到DS18B20 //dat:要寫入的字節 void DS18B20_Write_Byte(uint8_t dat) { uint8_t j;uint8_t testb = 0;DS18B20_IO_OUT(); //設置為輸出for (j=1;j<=8;j++) {testb = dat&0x01;dat = dat>>1;if(testb) // 寫1{DS18B20_DQ_OUT(0);delay_us(2); DS18B20_DQ_OUT(1);delay_us(49); }else //寫0{DS18B20_DQ_OUT(0);delay_us(49); DS18B20_DQ_OUT(1);delay_us(2); }} }//開始溫度轉換 void DS18B20_Start(void) { DS18B20_Rst(); DS18B20_Check(); DS18B20_Write_Byte(0xcc);// skip romDS18B20_Write_Byte(0x44);// convert }//初始化DS18B20的IO口 DQ 同時檢測DS的存在 //返回1:不存在 //返回0:存在 uint8_t DS18B20_Init(void) {GPIO_InitTypeDef GPIO_Initure;DS18B20_DATA_CLK(); //開啟GPIOA時鐘GPIO_Initure.Pin=DS18B20_DATA_PIN; //PA0GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //推挽輸出GPIO_Initure.Pull=GPIO_PULLUP; //上拉GPIO_Initure.Speed=GPIO_SPEED_FREQ_LOW;//高速HAL_GPIO_Init(DS18B20_DATA_PORT,&GPIO_Initure); //初始化DS18B20_Rst();return DS18B20_Check(); }//從ds18b20得到溫度值 //精度:0.1C //返回值:溫度值 (-550~1250) short DS18B20_Get_Temp(void) {uint8_t temp = 0;uint8_t TL = 0,TH = 0;short tem = 0;DS18B20_Start (); //開始轉換DS18B20_Rst();DS18B20_Check(); DS18B20_Write_Byte(0xcc); // skip romDS18B20_Write_Byte(0xbe); // convert TL=DS18B20_Read_Byte(); // LSB TH=DS18B20_Read_Byte(); // MSB if(TH > 7){TH = ~TH;TL = ~TL; temp = 0;//溫度為負 }else temp = 1;//溫度為正 tem = TH; //獲得高八位tem <<= 8; tem += TL;//獲得底八位tem=(double)tem*0.625;//轉換 if(temp)return tem; //返回溫度值else return -tem; }LCD1602顯示
#include "lcd1602.h"#define DELAY_2N 0void lcd_delay_us(unsigned int t) {unsigned int i, j;for(i = 10; i > 0; i--)for(j = t; j > 0; j--); }void lcd_delay_ms(unsigned int t) { unsigned int i;for(i = t; i > 0; i--)lcd_delay_us(10); }//================================================== void LCD_Init(void) {GPIO_InitTypeDef GPIO_Initure; LCD_CTRL_CLK();LCD_DATA_CLK();GPIO_Initure.Pin = LCD_RS_PIN|LCD_RW_PIN|LCD_EN_PIN; GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_Initure.Pull = GPIO_PULLUP; GPIO_Initure.Speed = GPIO_SPEED_FREQ_MEDIUM; HAL_GPIO_Init(LCD_CTRL_PORT, &GPIO_Initure);GPIO_Initure.Pin = LCD_DATA0_PIN|LCD_DATA1_PIN|LCD_DATA2_PIN|LCD_DATA3_PIN|LCD_DATA4_PIN|LCD_DATA5_PIN|LCD_DATA6_PIN|LCD_DATA7_PIN; HAL_GPIO_Init(LCD_DATA_PORT, &GPIO_Initure);LCD_RW(0); //讀寫位直接低電平,只寫不讀/*********************液晶初始化**************************/ lcd_delay_us(340); LCD_RS(0);LCD_write_cmd(0x38); // 8bit顯示模式,2行,5x7字體lcd_delay_ms(4); LCD_write_cmd(0x08); // 顯示關閉 lcd_delay_ms(4); LCD_write_cmd(0x01); // 顯示清屏 lcd_delay_ms(4); LCD_write_cmd(0x06); // 顯示光標移動設置 lcd_delay_ms(4);LCD_write_cmd(0x0c); // 顯示開,光標開,光標閃爍lcd_delay_ms(4);LCD_write_cmd(0x01); //清屏lcd_delay_ms(4); } /*-------------------------------------------------- 函數說明:寫命令到液晶---------------------------------------------------*/ void LCD_write_cmd(unsigned char cmd) {LCD_RS(0);LCD_Write_byte(cmd);lcd_delay_us(340); } /*-------------------------------------------------- 函數說明:寫數據到液晶---------------------------------------------------*/ void LCD_write_data(unsigned char w_data) {LCD_RS(1);LCD_Write_byte(w_data);lcd_delay_us(340); } /*-------------------------------------------------- 函數說明:寫4bit到液晶 --------------------------------------------------*/ void LCD_Write_byte(unsigned char num) { if (num&0x01)data0(1);elsedata0(0);if (num&0x02)data1(1);elsedata1(0);if (num&0x04)data2(1);elsedata2(0);if (num&0x08)data3(1);elsedata3(0);if (num&0x10)data4(1);elsedata4(0);if (num&0x20)data5(1);elsedata5(0);if (num&0x40)data6(1);elsedata6(0);if (num&0x80)data7(1);elsedata7(0);lcd_delay_us(340);LCD_EN(1);lcd_delay_us(340);LCD_EN(0); lcd_delay_us(340); }/*---------------------------------------------------- LCD_set_xy : 設置LCD顯示的起始位置 輸入參數:x、y : 顯示字符串的位置,X:0-15,Y:0-1 -----------------------------------------------------*/ void LCD_set_xy( unsigned char x, unsigned char y ) {unsigned char address = 0;if (y==0) {address=0x80+x;}else {address=0xc0+x;} // y ? (address=0xc0+x): (address=0x80+x) ;LCD_write_cmd(address); } /*--------------------------------------------------- LCD_write_string : 英文字符串顯示函數 輸入參數:*s :英文字符串指針;X、Y : 顯示字符串的位置 ---------------------------------------------------*/ void LCD_write_string(unsigned char X,unsigned char Y, char *s) {LCD_set_xy(X,Y); while (*s != NULL) {LCD_write_data(*s);s++;} }//======================================================= void LCD_wstring(unsigned char X,unsigned char *s) {LCD_write_cmd(X); while (*s) {LCD_write_data(*s);s++;} }總結
以上是生活随笔為你收集整理的基于STM32大棚DHT11温湿度监测的Proteus仿真的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 和时间做朋友2
- 下一篇: 虚幻4C++编程入门(搬运1)