這是仿真圖
- #include <reg52.h>
- # include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit io = P1^0;
- sbit rs = P2^0;
- sbit rw = P2^1;
- sbit ep = P2^2;
- uchar data_byte ;
- uchar RH,RL,TH,TL;
- typedef bit BOOL;//此聲明一個布爾型變量即真或假
- BOOL lcd_bz()//測試lcd忙碌狀態返回值為布爾型數值真或假'1'.'0'
- {
- BOOL result;
- rs=0;
- rw=1;
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- result = (BOOL)(P0&0x80);
- ep=0;
- result ;
- }
- void delay(uchar ms) //延時模塊//
- {
- uchar i;
- while(ms--)
- for(i=0;i<100;i++);
- }
- void delay1()//一個for循環大概需要8個多機器周期一個機器周期為1us晶振為12MHz也就是說本函數延時8us多此延時函數必須德稍微精確一點
- {
- uchar i;
- for(i=0;i<1;i++);
- }
- void write_com(uchar com)//寫指令//
- {
-
- while (lcd_bz());
- rs=0;
- rw=0;
- ep=0;
- _nop_();
- _nop_();
- P0=com;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=0;
- }
- void write_addr(uchar addr)//寫地址//
- {
- write_com(addr+0x80);//LCD第一行的首地址為0x80第二行的首地址為0x80+0x40=0xc0
- }
- void write_byte(uchar dat) //寫數據//
- {
- while (lcd_bz());
- rs=1;
- rw=0;
- ep=0;
- _nop_();
- _nop_();
- P0=dat ;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- ep=0;
- }
- void lcd_init() //液晶初始化
- {
- ep=0;
- rw=0; //只寫不讀
- write_com(0x38); //顯示模式設置,設置16*2顯示,5*7點陣,8位數據接口
- delay(2);
- write_com(0x0c); //顯示開關及光標設置
- delay(2);
- write_com(0x06); //讀一個字符后地址指針加一
- delay(2);
- write_com(0x01); //清屏
- delay(2);
- }
- void display(uchar addr,uchar q)//在某一地址上顯示內容adder表示的是地址偏移量q表示顯示的字符或數字//
- {
- delay(10);
- write_addr(addr);
- write_byte(q);
- delay(1);//修改此時間可以改變LCD上數值跳變的數度
-
- }
-
-
- void start()//開始信號
- {
- io=1;
- delay1();
- io=0;
- delay(25);// 主機把總線拉低必須大于18ms保證DHT11能檢測到起始信號
- io=1; //發送開始信號結束后拉高電平延時20-40us
- delay1();//以下三個延時函數差不多為24us符合要求
- delay1();
- delay1();
- }
- uchar receive_byte()//接收一個字節//
- {
- uchar i,temp;
- for(i=0;i<8;i++)//接收8bit的數據
- {
- while(!io);//等待50us的低電平開始信號結束
- delay1();//開始信號結束之后延時26us-28us以下三個延時函數
- delay1();
- delay1();
-
- delay1();
- delay1();
- delay1();
- delay1();
- temp=0;//時間為26us-28us表示接收的為數據'0'
- delay1();
- delay1();
- delay1();
- delay1();
- delay1();
-
- if(io==1)
- temp=1; //如果26us-28us之后還為高電平則表示接收的數據為'1'
- data_byte<<=1;//接收的數據為高位在前右移
-
- data_byte|=temp;
- }
- return data_byte;
- }
- void receive()//接收數據//
- {
- uchar T_H,T_L,R_H,R_L,check,num_check,i;
- start();//開始信號//
- io=1; //主機設為輸入判斷從機DHT11響應信號
- if(!io)//判斷從機是否有低電平響應信號//
- {
- while(!io);//判斷從機發出 80us 的低電平響應信號是否結束//
- while(io);//判斷從機發出 80us 的高電平是否結束如結束則主機進入數據接收狀態
- R_H=receive_byte();//濕度高位
- R_L=receive_byte();//濕度低位
- T_H=receive_byte();//溫度高位
- T_L=receive_byte();//溫度低位
- check=receive_byte();//校驗位
- io=0; //當最后一bit數據接完畢后從機拉低電平50us//
- for(i=0;i<7;i++)//差不多50us的延時
- delay1();
- io=1;//總線由上拉電阻拉高進入空閑狀態
- num_check=R_H+R_L+T_H+T_L;
- if(num_check==check)//判斷讀到的四個數據之和是否與校驗位相同
- {
- RH=R_H;
- RL=R_L;
- TH=T_H;
- TL=T_L;
- check=num_check;
- }
- }
- }
- void main()//主函數模塊//
- {
- lcd_init();//初始化LCD
- while(1)
- {
- receive();//接收數據
- display(0x00,'R');//LCD的第一行顯示
- display(0x01,':');
- display(0x02,RH/10+0x30); //0x30表示帶字庫的LCD1602中0x30的位置放有數字0RH/10+0x30即表示濕度的十位數字在字庫RH/10+0x30的位置處放著
- display(0x03,RH%10+0x30);
- display(0X04,'%');
- display(0x40,'T');//LCD的第二行顯示
- display(0x41,':');
- display(0x42,TH/10+0x30);
- display(0x43,TH%10+0x30);
- display(0x44,0xdf);//以下兩個是溫度單位的處理
- display(0x45,0x43);
- }
- }
復制代碼
|