这是我们课程设计做的,温湿度检测应该没问题,co的话可以点烟试一下,也应该好使
单片机源码:
- #include <reg52.h>
- #include <wenshidu.h>
- #define LCD_DB P2
- unsigned char s1[5];
- unsigned char s2[5];
- typedef unsigned char U8;
- typedef unsigned int U16;
- U16 temp;
- //uchar count1=70,count2=35;
- //uchar moshicount=0;
- uchar TD=0;//定时次数
- U8 FLAG;
- U8 count,U8temp;
- U8 T_data_H,T_data_L,RH_data_H,RH_data_L,checkdata;
- U8 T_data_H_temp,T_data_L_temp,RH_data_H_temp,RH_data_L_temp,checkdata_temp;
- U8 comdata;
- void LCD_init(void);//初始化函数
- void LCD_write_command(uchar command); //写指令函数
- void LCD_write_data(uchar dat);//写数据函数
- void LCD_disp_char(uchar x,uchar y,uchar dat);//在某个屏幕位置上显示一个字符,X(0-16),y(1-2)
- void delay_n40us(uint n);//延时函数
- void sendit();
- void salve_init();
- void LCD_init(void)//液晶初始化函数
- {
- LCD_write_command(0x38);//设置 8 位格式,2 行,5x7
- LCD_write_command(0x0c);//整体显示,关光标,不闪烁
- LCD_write_command(0x06);//设定输入方式,增量不移位
- LCD_write_command(0x03);//清除屏幕显示
- delay_n40us(100);
- }
- void timerinit()//定时器初始化函数
- {
- TMOD=0x01;
- TH0=-50000/256;
- TL0=-50000%6;
- EA=1;
- ET0=1;
- }
- void LCD_write_command(uchar dat)//写指令函数
- {
- LCD_DB=dat;
- LCD_RS=0;//指令
- LCD_RW=0;//写入
- LCD_E=1;//允许
- delay_n40us(1);
- LCD_E=0;
- delay_n40us(1);
- }
- void LCD_write_data(uchar dat)//写数据函数
- {
- LCD_DB=dat;
- LCD_RS=1;//数据
- LCD_RW=0;//写入
- LCD_E=1;//允许
- delay_n40us(1);
- LCD_E=0;
- delay_n40us(1);
- }
- void LCD_disp_char(uchar x,uchar y,uchar dat)//显示一个字符函数
- {
- uchar address;
- if(y==1)
- address=0x80+x;
- else
- address=0xc0+x;
- LCD_write_command(address);
- LCD_write_data(dat);
- }
- void delay_n40us(uint n)
- {
- uint i;
- uchar j;
- for(i=n;i>0;i--)
- for(j=0;j<2;j++);
- }
- void Delay_10us(void)
- {
- U8 i;
- i--;
- i--;
- i--;
- i--;
- i--;
- i--;
- }
-
- void Delay(U16 j)
- {
- U8 i;
- for(;j>0;j--)
- for(i=0;i<27;i++);
- }
-
- //*******一字节数据传送函数*********
- void COM(void)
- {
- U8 i;
- for(i=0;i<8;i++)
- {
- FLAG=2;
- while((!io)&&FLAG++);
- Delay_10us();
- Delay_10us();
- Delay_10us();
- temp=0;
- if(io)temp=1;
- FLAG=2;
- while((io)&&FLAG++);
- //超时则跳出for循环
- if(FLAG==1)break;
- //判断数据位是0还是1
- // 如果高电平高过预定0高电平值则数据位为 1
- comdata<<=1;
- comdata|=temp;
- }
- }
- void RH(void)
- {
- //主机拉低18ms
- io=0;
- Delay(180);
- io=1;
- //总线由上拉电阻拉高 主机延时20us
- Delay_10us();
- Delay_10us();
- Delay_10us();
- Delay_10us();
- //主机设为输入判断从机响应信号
- io=1;
- //判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行
- if(!io) //T !
- {
- FLAG=2;
- //判断从机是否发出 80us 的低电平响应信号是否结束
- while((!io)&&FLAG++);
- FLAG=2;
- //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
- while((io)&&FLAG++);
- //数据接收状态
- COM();
- RH_data_H_temp=comdata;
- COM();
- RH_data_L_temp=comdata;
- COM();
- T_data_H_temp=comdata;
- COM();
- T_data_L_temp=comdata;
- COM();
- checkdata_temp=comdata;
- io=1;
- //数据校验
- temp=(T_data_H_temp+T_data_L_temp+RH_data_H_temp+RH_data_L_temp);
- if(temp==checkdata_temp)
- {
- RH_data_H=RH_data_H_temp;
- RH_data_L=RH_data_L_temp;
- T_data_H=T_data_H_temp;
- T_data_L=T_data_L_temp;
- checkdata=checkdata_temp;
- }
- //湿度整数部分
- s1[0] = (char)(0X30+RH_data_H/10);
- s1[1] = (char)(0X30+RH_data_H%10);
- //湿度小数部分
- s1[2] = (char)(0X30+RH_data_L/10);
- //温度整数部分
- s2[0] = (char)(0X30+T_data_H/10);
- s2[1] = (char)(0X30+T_data_H%10);
- //温度小数部分
- s2[2] = (char)(0X30+T_data_L/10);
- }
- }
- void disp()//液晶显示函数
- {
- /* LCD_disp_char(0,1,'s');
- LCD_disp_char(1,1,'h');
- LCD_disp_char(2,1,'i');
- LCD_disp_char(3,1,'d');
- LCD_disp_char(4,1,'u');
- LCD_disp_char(5,1,':');
- LCD_disp_char(6,1,s1[0]);
- LCD_disp_char(7,1,s1[1]);
- LCD_disp_char(8,1,'.');
- LCD_disp_char(9,1,s1[2]);
- LCD_disp_char(10,1,'%');
- LCD_disp_char(11,1,'R');
- LCD_disp_char(12,1,'H');
- LCD_disp_char(0,2,'w');
- LCD_disp_char(1,2,'e');
- LCD_disp_char(2,2,'n');
- LCD_disp_char(3,2,'d');
- LCD_disp_char(4,2,'u');
- LCD_disp_char(5,2,':');
- LCD_disp_char(6,2,s2[0]);
- LCD_disp_char(7,2,s2[1]);
- LCD_disp_char(8,2,'.');
- LCD_disp_char(9,2,s2[2]);
- LCD_disp_char(10,2,0xDF);
- LCD_disp_char(11,2,'C');
- */
- LCD_disp_char(0,1,'s');
- LCD_disp_char(1,1,'d');
- LCD_disp_char(2,1,':');
- LCD_disp_char(3,1,s1[0]);
- LCD_disp_char(4,1,s1[1]);
- LCD_disp_char(5,1,'%');
- LCD_disp_char(0,2,'w');
- LCD_disp_char(1,2,'d');
- LCD_disp_char(2,2,':');
- LCD_disp_char(3,2,s2[0]);
- LCD_disp_char(4,2,s2[1]);
- LCD_disp_char(5,2,0xDF);
- LCD_disp_char(6,2,'C');
- }
- //阈值设置函数
- /*void shezhi()
- {
- //初值
- s1[3] = (char)(0X30+count1/10);
- s1[4] = (char)(0X30+count1%10);
- //初值
- s2[3] = (char)(0X30+count2/10);
- s2[4] = (char)(0X30+count2%10);
- moshi=1;
- if(moshi==0)
- {
- Delay_10us();
- while(moshi==0);
- moshicount++;
- }
- switch(moshicount)
- {
- case 1:
- {
- INC=1;DEC=1;
- if(INC==0)
- {
- Delay_10us();
- while(INC==0);
- count1++;
- }
- else if(DEC==0)
- {
- Delay_10us();
- while(DEC==0);
- count1--;
- }
- LCD_disp_char(14,1,s1[3]);
- LCD_disp_char(15,1,s1[4]);
- LCD_disp_char(14,2,s2[3]);
- LCD_disp_char(15,2,s2[4]);
- }break;
- case 2:
- {
- //moshicount=0;
- INC=1;DEC=1;
- if(INC==0)
- {
- Delay_10us();
- while(INC==0);
- count2++;
- }
- else if(DEC==0)
- {
- Delay_10us();
- while(DEC==0);
- count2--;
- }
- LCD_disp_char(14,1,s1[3]);
- LCD_disp_char(15,1,s1[4]);
- LCD_disp_char(14,2,s2[3]);
- LCD_disp_char(15,2,s2[4]);
- }break;
- case 3:
- {
- moshicount=0;
- LCD_disp_char(14,1,' ');
- LCD_disp_char(15,1,' ');
- LCD_disp_char(14,2,' ');
- LCD_disp_char(15,2,' ');
- }break;
- default :break;
- }
- }
- */
- //定时器0中断程序,每次定时50ms
- timer0() interrupt 1
- {
- TD++;
- if(TD>=100)
- {
- TH0=-50000/256;
- TL0=-50000%6;
- TR0=0;
- TD=0;
- }
- TH0=-50000/256;
- TL0=-50000%6;
- }
- //主函数
- void main(void)
- {
- LCD_init();
- timerinit();
- Delay(4);
- while(1)
- {
- salve_init();
- sendit();
- RH();
- disp();
- TR0=1;
- /* while((TD>=1)&&(TD<=100))
- {
- shezhi();
- }*/
- }
- }
- void salve_init()
- {
- TMOD=0x20;
- TH1=0xfd;
- TL1=0xfd;
- PCON=0x00;
- TR1=1;
- SCON=0x50;
- }
- void sendit()
- {
- SBUF=s1[0];
- while(!TI);
- TI=0;
- SBUF=s1[1];
- while(!TI);
- TI=0;
- SBUF=s2[0];
- while(!TI);
- TI=0;
- SBUF=s2[1];
- while(!TI);
- TI=0;
- }
-
复制代码
所有资料51hei提供下载:
温湿度检测.rar
(12.86 KB, 下载次数: 22)
|