|
智能小車上顯示
- /******************************************************
- 功 能:DH11溫濕度1602(6線驅動)顯示
- 單片機:STC12C5608AD
- 晶 振:11?0592M
- 作 者:蘇義江
- 時 間:2016-10-22
- ***************************************************/
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit DATA=P1^1;//dht11data端接單片機的P1^0口//
- uchar RHL,RHH,CL,CH;
- uint m,n;
- //很正要的延時
- void delayus(uchar n)
- {
- while(n--);
- }
- //很正要的延時
- void Delay10ms(void)//12M晶振下延時
- {
- uchar a,b;
- for(b=249;b>0;b--)
- for(a=17;a>0;a--);
- /*
- uchar i,j,k;
- for(i=5;i>0;i--)
- for(j=4;j>0;j--)
- {
- for(k=248;k>0;k--);
- }
- */
- }
- //粗延時
- void delay_ms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=614;y>0;y--);
- }
- //起始
- void Start(void)
- {
- DATA=1;
- delay_ms(10);
- DATA=0;
- Delay10ms(); //主機把總線拉低必須大于
- //18msDHT11能檢測到起始信號
- Delay10ms();
- Delay10ms();
- DATA=1;
- delay_ms(1);
- }
- uint integer(uchar x)
- {
- uchar h,l;
- uint n;
- l=x&0x0f;
- h=(x&0xf0)>>4;
- n=h*16+l;
- return n;
- }
- uchar readdht11_byte()
- {
- uchar i,dht;
- for(i=8;i>0;i--)
- {
- dht=dht<<1;
- while(!DATA); //等待50ms
- delayus(30); //延時29us
- if(DATA==1)
- {
- dht=dht|0x01;
- delayus(60);
- }
- else
- delayus(30); //延時29us
- }
- return(dht);
- }
- void read_dht11()
- {
- Start();
- DATA=0; //數據線拉低>18ms
- delay_ms(20); //延時>18ms
- DATA=1; //拉高等待
- delayus(30); //延時29us
- DATA=1;
- if(!DATA)
- {
- while(!DATA);//
- while(DATA);
- RHH=readdht11_byte();//濕度整數數據
- RHL=readdht11_byte();//濕度小數數據
- CH=readdht11_byte();//溫度整數數據
- CL=readdht11_byte();//溫度小數數據
- }
- }
- //////////////////////////////////////////////////
- //LCD1602接口定義 RW接地
- #define LCD_DB P2 // - - P2 = DB4~DB7
- sbit LCD_RS=P2^2; // - - p2.2 = RS
- sbit LCD_E=P2^3; // - - p2.3 = E
- void LCD_delay_10us(uint n) // - - 10微秒的延時子程序
- {
- uint i,j;
- for(i=n*10;i>0;i--) // - - 晶振及單片機修改設置
- for(j=2;j>0;j--);
- }
- void LCD_delay_50us(uint n) // - - 50微秒的延時子程序
- {
- uint i,j;
- for(i=n*10;i>0;i--) // - - 晶振及單片機修改設置
- for(j=22;j>0;j--);
- }
- // - - 向LCD1602寫指令
- void LCD_write_4bit_command(uchar dat);
- // - - 向LCD1602寫高四位指令
- void LCD_write_H4bit_command(uchar dat);
- // - - 向LCD1602寫低四位指令
- void LCD_write_L4bit_command(uchar dat);
- // - - 初始化LCD1602
- void LCD_init(void)
- {
- LCD_delay_50us(10);
- LCD_RS=0; // - - 指令
- LCD_E=0; // - - 使能
- LCD_write_L4bit_command(0x03); // - - 設置4位格式,2行,5x7
- LCD_delay_50us(10);
- LCD_write_L4bit_command(0x03); // - - 設置4位格式,2行,5x7
- LCD_delay_50us(10);
- LCD_write_L4bit_command(0x02); // - - 設置4位格式,2行,5x7
- LCD_delay_50us(10);
- LCD_write_4bit_command(0x28); // - - 設置4位格式,2行,5x7
- LCD_delay_10us(10);
- LCD_write_4bit_command(0x28); // - - 設置4位格式,2行,5x7
- LCD_delay_10us(5);
- LCD_write_4bit_command(0x0c); // - - 整體顯示,關光標,不閃爍
- LCD_delay_10us(5);
- LCD_write_4bit_command(0x06); // - - 設定輸入方式,增量不移位
- LCD_delay_10us(5);
- LCD_write_4bit_command(0x01); // - - 清除屏幕顯示
- LCD_delay_50us(10);
- }
- //********************************
- // - - 向LCD1602寫指令
- void LCD_write_4bit_command(uchar dat)
- {
- LCD_delay_10us(10);
- LCD_RS=0; // - - 指令
- LCD_write_H4bit_command(dat);
- LCD_write_L4bit_command(dat);
- }
- // - - 向LCD1602寫高四位指令
- void LCD_write_H4bit_command(uchar dat)
- {
- LCD_delay_10us(100);
- LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
- LCD_delay_10us(10);
- LCD_E=1; // - - 允許
- LCD_delay_10us(100);
- LCD_E=0;
- }
- // - - 向LCD1602寫低四位指令
- void LCD_write_L4bit_command(uchar dat)
- {
- dat<<=4;
- LCD_delay_10us(10);
- LCD_DB=(LCD_DB&0x0F)|(dat&0xF0);
- LCD_delay_10us(10);
- LCD_E=1; // - - 允許
- LCD_delay_10us(100);
- LCD_E=0;
- }
- // - - 向LCD1602寫數據
- void LCD_write_4bit_data(uchar dat)
- {
- LCD_delay_10us(10);
- LCD_RS=1; // - - 數據
- LCD_write_H4bit_command(dat);
- LCD_write_L4bit_command(dat);
- }
- // - - 設置顯示位置
- void LCD_set_xy(uchar x,uchar y)
- {
- uchar address;
- if(y==1)
- {
- address=0x80+x; // - - 第一行位置
- }
- else
- {
- address=0xc0+x; // - - 第二行位置
- }
- LCD_delay_10us(10);
- LCD_write_4bit_command(address);
- }
- // - - 顯示一個字符函數
- void LCD_disp_char(uchar x,uchar y,uchar dat)
- // - - LCD_disp_char(0,1,0x38); // - - 顯示8
- {
- LCD_set_xy(x,y);
- LCD_delay_10us(15);
- LCD_write_4bit_data(dat);
- }
- // - - 顯示一個字符串函數
- void LCD_disp_string(uchar x,uchar y,uchar *s)
- {
- LCD_set_xy(x,y);
- LCD_delay_10us(5);
- while(*s!='\0')
- {
- LCD_write_4bit_data(*s);
- s++;
- }
- }
- void main()//主函數模塊//
- {
-
- // lcd_init();//初始化LCD
- LCD_init();
- delay_ms(1500);
- while(1)
- {
- delay_ms(1500);
- read_dht11();
- m=integer(RHH);
- n=integer(CH);
-
- LCD_disp_string(0,0,"R:");
- LCD_disp_char(3,0,m/10+0x30) ;
- LCD_disp_char(4,0,m%10+0x30) ;
- LCD_disp_char(6,0,'%') ;
- LCD_disp_string(0,1,"T:");
- LCD_disp_char(3,1,n/10+0x30) ;
- LCD_disp_char(4,1,n%10+0x30) ;
-
- LCD_disp_char(6,1,0xdf) ;
- LCD_disp_char(7,1,0x43) ;
- }
- }
復制代碼
|
評分
-
查看全部評分
|