本人用的是keil4,源程序如下:
- #include<reg51.h>
- #include"key_contrl.h"
- #include"lcd1602.h"
- #define LCM_Data P1
- #define Busy 0x80 //用于检测LCM状态字中的Busy标识
- extern int temp_value,humi_value;
- extern int flag;
- extern int temp;
- sbit red=P2^6;
- sbit blue=P2^7;
- sbit LCM_RW= P2^4; //定义引脚
- sbit LCM_RS= P2^5;
- sbit LCM_E= P2^3;
- sbit beep = P0^6;//beep = 0时候发声
- extern inttemph,templ,disp_mode,huih,huil;
- extern chartel,teh,hh,hl;
- //写数据
- void alarm()
- {
- beep=0;
- Delay5Ms();
- Delay5Ms();
- beep=1;
- Delay5Ms();
- Delay5Ms();
- }
- voidWriteDataLCM(unsigned char WDLCM)
- {
- ReadStatusLCM(); //检测忙
- LCM_Data = WDLCM;
- LCM_RS = 1;
- LCM_RW = 0;
- LCM_E = 0; //若晶振速度太高可以在这后加小的延时
- LCM_E = 0; //延时
- LCM_E = 1;
- }
- //写指令
- voidWriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC为0时忽略忙检测
- {
- if (BuysC)ReadStatusLCM(); //根据需要检测忙
- LCM_Data = WCLCM;
- LCM_RS = 0;
- LCM_RW = 0;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- }
- //读数据
- unsigned charReadDataLCM(void)
- {
- LCM_RS = 1;
- LCM_RW = 1;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- return(LCM_Data);
- }
- //读状态
- unsigned charReadStatusLCM(void)
- {
- LCM_Data = 0xFF;
- LCM_RS = 0;
- LCM_RW = 1;
- LCM_E = 0;
- LCM_E = 0;
- LCM_E = 1;
- while (LCM_Data &Busy); //检测忙信号
- return(LCM_Data);
- }
- void LCMInit(void)//LCM初始化
- {
- LCM_Data = 0;
- WriteCommandLCM(0x38,0);//三次显示模式设置,不检测忙信号
- Delay5Ms();
- WriteCommandLCM(0x38,0);
- Delay5Ms();
- WriteCommandLCM(0x38,0);
- Delay5Ms();
- WriteCommandLCM(0x38,1);//显示模式设置,开始要求每次检测忙信号
- WriteCommandLCM(0x08,1);//关闭显示
- WriteCommandLCM(0x01,1);//显示清屏
- WriteCommandLCM(0x06,1);// 显示光标移动设置
- WriteCommandLCM(0x0C,1);// 显示开及光标设置
- }
- //按指定位置显示一个字符
- voidDisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
- {
- Y &= 0x1;
- X &= 0xF; //限制X不能大于15,Y不能大于1
- if (Y) X |= 0x40; //当要显示第二行时地址码 0x40;
- X |= 0x80; // 算出指令码
- WriteCommandLCM(X, 0);//这里不检测忙信号,发送地址码
- WriteDataLCM(DData);
- }
- //按指定位置显示一串字符
- voidDisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
- {
- unsigned charListLength;
- ListLength = 0;
- Y &= 0x1;
- X &= 0xF; //限制X不能大于15,Y不能大于1
- while(DData[ListLength]>0x20) //若到达字串尾则退出
- {
- if (X <= 0xF) //X坐标应小于0xF
- {
- DisplayOneChar(X, Y,DData[ListLength]); //显示单个字符
- ListLength++;
- X++;
- }
- }
- }
- //5ms延时
- void Delay5Ms(void)
- {
- unsigned int TempCyc =5552;
- while(TempCyc--);
- }
- /*voidDelay_xMs(unsigned int x)
- {
- unsigned int i,j;
- for( i =0;i < x;i++ )
- {
- for( j =0;j<500;j++ );
- }
- }
- */
- void display_temp(void)
- {
- DisplayOneChar(0,0,' ');
- DisplayOneChar(1,0,' ');
- DisplayOneChar(2,0,'T');
- DisplayOneChar(3,0,'e');
- DisplayOneChar(4,0,'m');
- DisplayOneChar(5,0,'p');
- DisplayOneChar(6,0,':');
- DisplayOneChar(10,0,'C');
- DisplayOneChar(9,0,0xdf);
- DisplayOneChar(7,0,temp_value/100+0x30);
- DisplayOneChar(8,0,(temp_value/10)-(temp_value/100)*10+0x30);
- DisplayOneChar(11,0,' ');
- DisplayOneChar(12,0,' ');
- DisplayOneChar(13,0,' ');
- DisplayOneChar(14,0,' ');
- DisplayOneChar(15,0,' ');
- DisplayOneChar(0,1,' ');
- DisplayOneChar(1,1,' ');
- DisplayOneChar(2,1,'H');
- DisplayOneChar(3,1,'u');
- DisplayOneChar(4,1,'m');
- DisplayOneChar(5,1,'i');
- DisplayOneChar(6,1,'d');
- DisplayOneChar(7,1,'i');
- DisplayOneChar(8,1,'t');
- DisplayOneChar(9,1,'y');
- DisplayOneChar(10,1,':');
- DisplayOneChar(11,1,humi_value/100+0x30);
- DisplayOneChar(12,1,(humi_value/10)-(humi_value/100)*10+0x30);
- DisplayOneChar(13,1,'%');
- DisplayOneChar(14,1,' ');
- DisplayOneChar(15,1,' ');
- }
- voiddisplay_temp1(void)
- {
- DisplayOneChar(0,0,' ');
- DisplayOneChar(1,0,'T');
- DisplayOneChar(2,0,'h');
- DisplayOneChar(3,0,':');
- if(flag&teh)
- {
- DisplayOneChar(4,0,0x20);
- DisplayOneChar(5,0,0x20);
- }
- else
- {
- DisplayOneChar(4,0,temph/10+0x30);
- DisplayOneChar(5,0,temph%10+0x30);
- }
- DisplayOneChar(6,0,0xdf);
- DisplayOneChar(7,0,'C');
- DisplayOneChar(8,0,' ');
- DisplayOneChar(9,0,'T');
- DisplayOneChar(10,0,'h');
- DisplayOneChar(11,0,':');
- if(flag&tel)
- {
- DisplayOneChar(12,0,0x20);
- DisplayOneChar(13,0,0x20);
- }
- else
- {
- DisplayOneChar(12,0,templ/10+0x30);
- DisplayOneChar(13,0,templ%10+0x30);
- }
- DisplayOneChar(14,0,0xdf);
- DisplayOneChar(15,0,'C');
- DisplayOneChar(0,1,' ');
- DisplayOneChar(1,1,'H');
- DisplayOneChar(2,1,'h');
- DisplayOneChar(3,1,':');
- if(flag&hh)
- {
- DisplayOneChar(4,1,0x20);
- DisplayOneChar(5,1,0x20);
- }
- else
- {
- DisplayOneChar(4,1,huih/10+0x30);
- DisplayOneChar(5,1,huih%10+0x30);
- }
- DisplayOneChar(6,1,'%');
- DisplayOneChar(7,1,' ');
- DisplayOneChar(8,1,' ');
- DisplayOneChar(9,1,'H');
- DisplayOneChar(10,1,'l');
- DisplayOneChar(11,1,':');
- if(flag&hl)
- {
- DisplayOneChar(12,1,0x20);
- DisplayOneChar(13,1,0x20);
- }
- else
- {
- DisplayOneChar(12,1,huil/10+0x30);
- DisplayOneChar(13,1,huil%10+0x30);
- }
- DisplayOneChar(14,1,'%');
- DisplayOneChar(15,1,' ');
- }
- void displayfun1()
- {
- display_temp();
- }
- void displayfun2()
- {
- display_temp1();
- }
- void display()
- {
- if((((temp_value/10)%100)<templ)||(((humi_value/10)%100)<huil))
- {beep=0;red=0;} //温度低于下限值,报警
- elseif(((temp_value/10)%100)>=temph || ((humi_value/10)%100)>=huih)
- {beep=0;blue=0;} //温度高于或等于上限值,报警
- else{beep=1;red=1;blue=1;}
- // if(((humi_value/10)%100)<huil){beep=0;led=0;} //温度低于下限值,报警
- // elseif(((humi_value/10)%100)>=huih)beep=0; //温度高于或等于上限值,报警
- // else{beep=1;led=1;}
- if(!disp_mode)
- displayfun1(); //显示正常
- else
- displayfun2(); //设置界面
- }
- #include"dht11.h"
- #include<reg52.h>
- //请根据自己的dht11接的IO 口来改动位定义
- sbit dht11 = P3^0;
- //防止在与硬件通信时发生死循环的计数范围
- #define NUMBER 20
- #define SIZE 5
- static unsigned charstatus;
- //存放五字节数据的数组
- static unsigned charvalue_array[SIZE];
- /*可在其他的文件引用温湿度值,实际是温度的整数的10 倍
- 如dht11 读回的温度是26,则temp_value = 260, 湿度同理*/
- int temp_value,humi_value;
- static unsigned charReadValue(void);
- void Delay_1ms(unsignedint ms)
- {
- unsigned int x, y;
- for(x = ms; x > 0; x--)
- {
- for(y = 124; y > 0; y--);
- }
- }
- static voidDHT11_Delay_10us(void)
- {
- unsigned char i;
- i--;
- i--;
- i--;
- i--;
- i--;
- i--;
- }
- /*读一个字节的数据*/
- static unsigned charDHT11_ReadValue(void)
- {
- unsigned char count, value = 0, i;
- status = OK; //设定标志为正常状态
- for(i = 8; i > 0; i--)
- {
- //高位在先
- value <<= 1;
- count = 0;
- //每一位数据前会有一个50us 的低电平时间.等待50us低电平结束
- while(dht11 == 0 && count++ < NUMBER);
- if(count >= NUMBER)
- {
- status = ERROR; //设定错误标志
- return 0; //函数执行过程发生错误就退出函数
- }
- //26-28us 的高电平表示该位是0,为70us高电平表该位1
- DHT11_Delay_10us();
- DHT11_Delay_10us();
- DHT11_Delay_10us();
- //延时30us 后检测数据线是否还是高电平
- if(dht11 != 0)
- {
- //进入这里表示该位是1
- value++;
- //等待剩余(约40us)的高电平结束
- while(dht11 != 0 && count++ < NUMBER)
- {
- dht11 = 1;
- }
- if(count >= NUMBER)
- {
- status = ERROR; //设定错误标志
- return 0;
- }
- }
- }
- return (value);
- }
- //读温度和湿度函数,读一次的数据,共五字节,读出成功函数返回OK, 错误返回ERROR
- unsigned charDHT11_ReadTempAndHumi(void)
- {
- unsigned char i = 0, check_value = 0,count = 0;
- EA = 0;
- dht11 = 0; //拉低数据线大于18ms 发送开始信号
- Delay_1ms(20); //需大于18 毫秒
- dht11 = 1; //释放数据线,用于检测低电平的应答信号
- //延时20-40us,等待一段时间后检测应答信号,应答信号是从机拉低数据线80us
- DHT11_Delay_10us();
- DHT11_Delay_10us();
- DHT11_Delay_10us();
- DHT11_Delay_10us();
- if(dht11 != 0) //检测应答信号,应答信号是低电平
- {
- //没应答信号
- EA = 1;
- return ERROR;
- }
- else
- {
- //有应答信号
- while(dht11 == 0 && count++ < NUMBER); //等待应答信号结束
- if(count >= NUMBER) //检测计数器是否超过了设定的范围
- {
- dht11 = 1;
- EA = 1;
- return ERROR; //读数据出错,退出函数
- }
- count = 0;
- dht11 = 1;//释放数据线
- //应答信号后会有一个80us 的高电平,等待高电平结束
- while(dht11 != 0 && count++ < NUMBER);
- if(count >= NUMBER)
- {
- dht11 = 1;
- EA = 1;
- return ERROR; //退出函数
- }
- //读出湿.温度值
- for(i = 0; i < SIZE; i++)
- {
- value_array[ i] = DHT11_ReadValue();
- if(status == ERROR)//调用ReadValue()读数据出错会设定status 为ERROR
- {
- dht11 = 1;
- EA = 1;
- return ERROR;
- }
- //读出的最后一个值是校验值不需加上去
- if(i != SIZE - 1)
- {
- //读出的五字节数据中的前四字节数据和等于第五字节数据表示成功
- check_value += value_array[ i];
- }
- }//end for
- //在没用发生函数调用失败时进行校验
- if(check_value == value_array[SIZE - 1])
- {
- //将温湿度扩大10 倍方便分离出每一位
- humi_value = value_array[0] * 10;
- temp_value = value_array[2] * 10;
- dht11 = 1;
- EA = 1;
- return OK; //正确的读出dht11 输出的数据
- }
- else
- {
- //校验数据出错
- EA = 1;
- return ERROR;
- }
- }
- }
- #include"lcd1602.h"
- #include"key_contrl.h"
- #include"dht11.h"
- #include<reg52.h>
- int temp;
- int temph = 40; //初始上限值
- int templ = 10; //初始下限值
- int huih = 90;
- int huil =10;
- inttimecount,flag=0,number=0,count=0;
- void t0(void) interrupt1 using 0
- { TH0=(65535-50000)/256; //50ms定时
- TL0=(65535-50000)%256;
- timecount++;//re_disp++;
- if(timecount>9){timecount=0;flag=~flag;} //50ms到了则反显
- //if(re_disp>200){re_disp=0;if(id)id=0;} //10秒到了则由调整模式自动进入正常显示模式
- }
- void t1(void) interrupt 3 using 0
- {
- TH1=(65535-50000)/256;//50ms定时
- TL1=(65535-50000)%256;
- number++;
- if(number>2)
- {
- number=0;
- count++;
- //beep=0;
- }
- }
- void main(void)
- {
- TMOD=0x11; //初始化定时器t0方式1
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- TH1=(65535-50000)/256;
- TL1=(65535-50000)%256;
- EA=1;
- ET0=1;
- ET1=1;
- TR0=1;
- TR1=1;
- LCMInit(); //LCM初始化
- Delay5Ms(); //延时片刻
-
- while(1)
- {
- if(count==2)
- {
- DHT11_ReadTempAndHumi();
- count=0;
- }
- delay_LCM(300);
- Scan_Key();
- }
-
- }
复制代码
这里是出现的各种各样的问题
Build target '目标 1'
compiling 1.c...
1.c(3): warning C318: can't open file 'key_contrl.h'
1.c(4): warning C318: can't open file 'lcd1602.h'
1.C(34): warning C206: 'Delay5Ms': missing function-prototype
1.C(46): warning C206: 'ReadStatusLCM': missing function-prototype
1.C(77): error C231: 'ReadStatusLCM': redefinition
1.C(86): error C231: 'ReadStatusLCM': redefinition
1.C(131): error C231: 'Delay5Ms': redefinition
1.C(134): error C231: 'Delay5Ms': redefinition
1.c(296): warning C318: can't open file 'dht11.h'
D:\秃驴爹\C51\INC\REG52.H(13): error C231: 'P0': redefinition
D:\秃驴爹\C51\INC\REG52.H(14): error C231: 'P1': redefinition
D:\秃驴爹\C51\INC\REG52.H(15): error C231: 'P2': redefinition
D:\秃驴爹\C51\INC\REG52.H(16): error C231: 'P3': redefinition
D:\秃驴爹\C51\INC\REG52.H(17): error C231: 'PSW': redefinition
D:\秃驴爹\C51\INC\REG52.H(18): error C231: 'ACC': redefinition
D:\秃驴爹\C51\INC\REG52.H(19): error C231: 'B': redefinition
D:\秃驴爹\C51\INC\REG52.H(20): error C231: 'SP': redefinition
D:\秃驴爹\C51\INC\REG52.H(21): error C231: 'DPL': redefinition
D:\秃驴爹\C51\INC\REG52.H(22): error C231: 'DPH': redefinition
D:\秃驴爹\C51\INC\REG52.H(23): error C231: 'PCON': redefinition
D:\秃驴爹\C51\INC\REG52.H(24): error C231: 'TCON': redefinition
D:\秃驴爹\C51\INC\REG52.H(25): error C231: 'TMOD': redefinition
D:\秃驴爹\C51\INC\REG52.H(26): error C231: 'TL0': redefinition
D:\秃驴爹\C51\INC\REG52.H(27): error C231: 'TL1': redefinition
D:\秃驴爹\C51\INC\REG52.H(28): error C231: 'TH0': redefinition
D:\秃驴爹\C51\INC\REG52.H(29): error C231: 'TH1': redefinition
D:\秃驴爹\C51\INC\REG52.H(30): error C231: 'IE': redefinition
D:\秃驴爹\C51\INC\REG52.H(31): error C231: 'IP': redefinition
D:\秃驴爹\C51\INC\REG52.H(32): error C231: 'SCON': redefinition
D:\秃驴爹\C51\INC\REG52.H(33): error C231: 'SBUF': redefinition
D:\秃驴爹\C51\INC\REG52.H(45): error C231: 'CY': redefinition
D:\秃驴爹\C51\INC\REG52.H(46): error C231: 'AC': redefinition
D:\秃驴爹\C51\INC\REG52.H(47): error C231: 'F0': redefinition
D:\秃驴爹\C51\INC\REG52.H(48): error C231: 'RS1': redefinition
D:\秃驴爹\C51\INC\REG52.H(49): error C231: 'RS0': redefinition
D:\秃驴爹\C51\INC\REG52.H(50): error C231: 'OV': redefinition
D:\秃驴爹\C51\INC\REG52.H(51): error C231: 'P': redefinition
D:\秃驴爹\C51\INC\REG52.H(54): error C231: 'TF1': redefinition
D:\秃驴爹\C51\INC\REG52.H(55): error C231: 'TR1': redefinition
D:\秃驴爹\C51\INC\REG52.H(56): error C231: 'TF0': redefinition
D:\秃驴爹\C51\INC\REG52.H(57): error C231: 'TR0': redefinition
D:\秃驴爹\C51\INC\REG52.H(58): error C231: 'IE1': redefinition
D:\秃驴爹\C51\INC\REG52.H(59): error C231: 'IT1': redefinition
D:\秃驴爹\C51\INC\REG52.H(60): error C231: 'IE0': redefinition
D:\秃驴爹\C51\INC\REG52.H(61): error C231: 'IT0': redefinition
D:\秃驴爹\C51\INC\REG52.H(64): error C231: 'EA': redefinition
D:\秃驴爹\C51\INC\REG52.H(66): error C231: 'ES': redefinition
D:\秃驴爹\C51\INC\REG52.H(67): error C231: 'ET1': redefinition
D:\秃驴爹\C51\INC\REG52.H(68): error C231: 'EX1': redefinition
D:\秃驴爹\C51\INC\REG52.H(69): error C231: 'ET0': redefinition
D:\秃驴爹\C51\INC\REG52.H(70): error C231: 'EX0': redefinition
D:\秃驴爹\C51\INC\REG52.H(74): error C231: 'PS': redefinition
D:\秃驴爹\C51\INC\REG52.H(75): error C231: 'PT1': redefinition
D:\秃驴爹\C51\INC\REG52.H(76): error C231: 'PX1': redefinition
D:\秃驴爹\C51\INC\REG52.H(77): error C231: 'PT0': redefinition
D:\秃驴爹\C51\INC\REG52.H(78): error C231: 'PX0': redefinition
D:\秃驴爹\C51\INC\REG52.H(81): error C231: 'RD': redefinition
D:\秃驴爹\C51\INC\REG52.H(82): error C231: 'WR': redefinition
D:\秃驴爹\C51\INC\REG52.H(83): error C231: 'T1': redefinition
D:\秃驴爹\C51\INC\REG52.H(84): error C231: 'T0': redefinition
D:\秃驴爹\C51\INC\REG52.H(85): error C231: 'INT1': redefinition
D:\秃驴爹\C51\INC\REG52.H(86): error C231: 'INT0': redefinition
D:\秃驴爹\C51\INC\REG52.H(87): error C231: 'TXD': redefinition
D:\秃驴爹\C51\INC\REG52.H(88): error C231: 'RXD': redefinition
D:\秃驴爹\C51\INC\REG52.H(91): error C231: 'SM0': redefinition
D:\秃驴爹\C51\INC\REG52.H(92): error C231: 'SM1': redefinition
D:\秃驴爹\C51\INC\REG52.H(93): error C231: 'SM2': redefinition
D:\秃驴爹\C51\INC\REG52.H(94): error C231: 'REN': redefinition
D:\秃驴爹\C51\INC\REG52.H(95): error C231: 'TB8': redefinition
D:\秃驴爹\C51\INC\REG52.H(96): error C231: 'RB8': redefinition
D:\秃驴爹\C51\INC\REG52.H(97): error C231: 'TI': redefinition
D:\秃驴爹\C51\INC\REG52.H(98): error C231: 'RI': redefinition
1.C(345): error C202: 'OK': undefined identifier
1.C(355): error C202: 'ERROR': undefined identifier
1.C(374): error C202: 'ERROR': undefined identifier
1.C(398): error C202: 'ERROR': undefined identifier
1.C(408): error C202: 'ERROR': undefined identifier
1.C(418): error C202: 'ERROR': undefined identifier
1.C(424): error C202: 'ERROR': undefined identifier
1.C(428): error C202: 'ERROR': undefined identifier
1.C(445): error C202: 'OK': undefined identifier
1.C(451): error C202: 'ERROR': undefined identifier
1.c(455): warning C318: can't open file 'lcd1602.h'
1.c(457): warning C318: can't open file 'key_contrl.h'
1.c(458): warning C318: can't open file 'dht11.h'
目标未创建
|