|
問題截圖.png (23.24 KB, 下載次數(shù): 44)
下載附件
這是仿真圖
2017-11-19 22:20 上傳
- #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忙碌狀態(tài)返回值為布爾型數(shù)值真或假'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循環(huán)大概需要8個多機(jī)器周期一個機(jī)器周期為1us晶振為12MHz也就是說本函數(shù)延時8us多此延時函數(shù)必須德稍微精確一點(diǎn)
- {
- 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) //寫數(shù)據(jù)//
- {
- 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); //顯示模式設(shè)置,設(shè)置16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口
- delay(2);
- write_com(0x0c); //顯示開關(guān)及光標(biāo)設(shè)置
- delay(2);
- write_com(0x06); //讀一個字符后地址指針加一
- delay(2);
- write_com(0x01); //清屏
- delay(2);
- }
- void display(uchar addr,uchar q)//在某一地址上顯示內(nèi)容adder表示的是地址偏移量q表示顯示的字符或數(shù)字//
- {
- delay(10);
- write_addr(addr);
- write_byte(q);
- delay(1);//修改此時間?梢愿淖僉CD上數(shù)值跳變的數(shù)度
-
- }
-
-
- void start()//開始信號
- {
- io=1;
- delay1();
- io=0;
- delay(25);// 主機(jī)把總線拉低必須大于18ms保證DHT11能檢測到起始信號
- io=1; //發(fā)送開始信號結(jié)束后拉高電平延時20-40us
- delay1();//以下三個延時函數(shù)差不多為24us符合要求
- delay1();
- delay1();
- }
- uchar receive_byte()//接收一個字節(jié)//
- {
- uchar i,temp;
- for(i=0;i<8;i++)//接收8bit的數(shù)據(jù)
- {
- while(!io);//等待50us的低電平開始信號結(jié)束
- delay1();//開始信號結(jié)束之后延時26us-28us以下三個延時函數(shù)
- delay1();
- delay1();
-
- delay1();
- delay1();
- delay1();
- delay1();
- temp=0;//時間為26us-28us表示接收的為數(shù)據(jù)'0'
- delay1();
- delay1();
- delay1();
- delay1();
- delay1();
-
- if(io==1)
- temp=1; //如果26us-28us之后還為高電平則表示接收的數(shù)據(jù)為'1'
- data_byte<<=1;//接收的數(shù)據(jù)為高位在前右移
-
- data_byte|=temp;
- }
- return data_byte;
- }
- void receive()//接收數(shù)據(jù)//
- {
- uchar T_H,T_L,R_H,R_L,check,num_check,i;
- start();//開始信號//
- io=1; //主機(jī)設(shè)為輸入判斷從機(jī)DHT11響應(yīng)信號
- if(!io)//判斷從機(jī)是否有低電平響應(yīng)信號//
- {
- while(!io);//判斷從機(jī)發(fā)出 80us 的低電平響應(yīng)信號是否結(jié)束//
- while(io);//判斷從機(jī)發(fā)出 80us 的高電平是否結(jié)束如結(jié)束則主機(jī)進(jìn)入數(shù)據(jù)接收狀態(tài)
- R_H=receive_byte();//濕度高位
- R_L=receive_byte();//濕度低位
- T_H=receive_byte();//溫度高位
- T_L=receive_byte();//溫度低位
- check=receive_byte();//校驗(yàn)位
- io=0; //當(dāng)最后一bit數(shù)據(jù)接完畢后從機(jī)拉低電平50us//
- for(i=0;i<7;i++)//差不多50us的延時
- delay1();
- io=1;//總線由上拉電阻拉高進(jìn)入空閑狀態(tài)
- num_check=R_H+R_L+T_H+T_L;
- if(num_check==check)//判斷讀到的四個數(shù)據(jù)之和是否與校驗(yàn)位相同
- {
- RH=R_H;
- RL=R_L;
- TH=T_H;
- TL=T_L;
- check=num_check;
- }
- }
- }
- void main()//主函數(shù)模塊//
- {
- lcd_init();//初始化LCD
- while(1)
- {
- receive();//接收數(shù)據(jù)
- display(0x00,'R');//LCD的第一行顯示
- display(0x01,':');
- display(0x02,RH/10+0x30); //0x30表示帶字庫的LCD1602中0x30的位置放有數(shù)字0RH/10+0x30即表示濕度的十位數(shù)字在字庫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);
- }
- }
復(fù)制代碼
|
|