目录
一、主要功能
二、硬件资源
三、程序编程
四、实现现象
一、主要功能
基于STC89C52单片机,DHT11温湿度采集温湿度,滑动变阻器连接ADC0832数模转换器模拟水位传感器检测水位,通过LCD1602显示信息,然后在程序里设置好是否有水的判断阈值,比如100,当水位没超过100,则判断没水,此时蜂鸣器报警,当水位超过100时候,蜂鸣器不报警,此时四×四按键矩阵设置湿度的下限,然后判断湿度是否低于下限,若低于下限,则继电器驱动电机转动启动加湿器,蜂鸣器报警。
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
编辑
三、程序编程
#include <REGX52.H>#include<intrins.h>#include<stdio.h>#include "Delay.h"#include "LCD1602.h"#include "MatrixKey.h"#define uchar unsigned char#define uint unsigned intsbit led1 = P2^4;
sbit beep = P3^0; //蜂鸣器引脚sbit DS = P3^1; //DS18B20温度传感器sbit CS=P2^5; //adc0832引脚sbit CLK=P2^6;
sbit DIO=P2^7;
sbit CS1=P3^2; //adc0832引脚sbit CLK1=P3^3;
sbit DIO1=P3^4;
sbit CS2=P3^5; //adc0832引脚sbit CLK2=P3^6;
sbit DIO2=P3^7;unsigned char KeyNum;static uchar u,U,R,u1,U1,R1,u2,U2,R2; //定义心率 血氧 加速度static uint temp;static float ftemp = 0.0f;//温度转变uint temp;static unsigned char num; //计时变量unsigned int password,count; //初始化阈值,次数,一共四位static int wdyz,xlyz,xyyz,jsyz;static int mode=0;static int number=0;static int count1=0;static int jsflag=0;//计数标志位static int mode1 = 0;void tmpchange();uint tmp();void beep\_warning();void cshq();void cslsz();void csxs();void Time0\_Init() //定时器初始化{
TMOD = 0x01; //定时器0工作在方式1 IE = 0x82;
TH0 = 0xfe;
TL0 = 0x33; //11.0592MZ晶振,0.5msTR0=1; //定时器开始EA=1;
}void Time0_Int() interrupt 1 //中断程序{TH0 = 0xfe; //重新赋值TL0 = 0x33;num++; if(num==200){ tmpchange(); //让18b20开始转换温度temp = tmp(); //读取温度ftemp = temp/10.0f; //转换温度num=0;} if(jsflag == 1){count1++; if(count1==1000){number++; LCD_ShowNum(1,14,number,3);count1=0;}}}uchar get\_AD\_Res() //ADC0832启动读取函数 心率{uchar i, data1=0, data2=0;CS=0;CLK=0;DIO=1;\_nop\_();CLK=1;\_nop\_();CLK=0;DIO=1;\_nop\_(); CLK=1;\_nop\_();CLK=0;DIO=0;\_nop\_();CLK=1;\_nop\_();CLK=0;DIO=1;\_nop\_(); for(i=0; i<8; i++){CLK=1;\_nop\_();CLK=0;\_nop\_();data1=(data1<<1)|(uchar)DIO; } for(i=0; i<8; i++){data2=data2|(uchar)DIO<<i;CLK=1;\_nop\_();CLK=0;\_nop\_();}CS=1; return(data1 == data2)?data1:0;
}uchar get\_AD\_Res1() //ADC0832启动读取函数 血氧{uchar i, data1=0, data2=0;CS1=0;CLK1=0;DIO1=1;\_nop\_();CLK1=1;\_nop\_();CLK1=0;DIO1=1;\_nop\_(); CLK1=1;\_nop\_();CLK1=0;DIO1=0;\_nop\_();CLK1=1;\_nop\_();CLK1=0;DIO1=1;\_nop\_(); for(i=0; i<8; i++){CLK1=1;\_nop\_();CLK1=0;\_nop\_();data1=(data1<<1)|(uchar)DIO1; } for(i=0; i<8; i++){data2=data2|(uchar)DIO1<<i;CLK1=1;\_nop\_();CLK1=0;\_nop\_();}CS1=1; return(data1 == data2)?data1:0;
}uchar get\_AD\_Res2() //ADC0832启动读取函数 加速度{uchar i, data1=0, data2=0;CS2=0;CLK2=0;DIO2=1;\_nop\_();CLK2=1;\_nop\_();CLK2=0;DIO2=1;\_nop\_(); CLK2=1;\_nop\_();CLK2=0;DIO2=0;\_nop\_();CLK2=1;\_nop\_();CLK2=0;DIO2=1;\_nop\_(); for(i=0; i<8; i++){CLK2=1;\_nop\_();CLK2=0;\_nop\_();data1=(data1<<1)|(uchar)DIO2; } for(i=0; i<8; i++){data2=data2|(uchar)DIO2<<i;CLK2=1;\_nop\_();CLK2=0;\_nop\_();}CS2=1; return(data1 == data2)?data1:0;
}void dsreset(void) //发出命令{uint i;DS=0; i=103; //将总线拉低480us~960uswhile(i>0)i--;DS=1; //然后拉高总线,若DS18B20做出反应会将在15us~60us后将总线拉低i=4; //15us~60us等待while(i>0)i--; //while(DS);}bit tmpreadbit(void) //读取数据{uint i;bit dat;DS=0;i++; //i++ for delayDS=1;i++;i++;dat=DS;i=8;while(i>0)i--; return (dat);
}uchar tmpread(void) //读取数据{uchar i,j,dat;dat=0; for(i=1;i<=8;i++){j=tmpreadbit();dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里} return(dat);
}void tmpwritebyte(uchar dat) //传输数据给DS18B20{uint i;uchar j;bit testb; for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1; if(testb) //write 1{DS=0;i++;i++;DS=1;i=8;while(i>0)i--;} else{DS=0; //write 0i=8;while(i>0)i--;DS=1;i++;i++;}}
}void tmpchange(void) //DS18B20开始工作{ dsreset(); Delay(1); tmpwritebyte(0xcc); tmpwritebyte(0x44);
}
uint tmp() //获得温度{ float tt;uchar a,b; dsreset(); Delay(1); tmpwritebyte(0xcc); tmpwritebyte(0xbe);a=tmpread();//低八位b=tmpread();//高八位temp=b;temp<<=8; //two byte compose a int variabletemp=temp|a;tt=temp*0.0625; //算出来的是测到的温度,数值可到小数点后两位temp=tt*10+0.5; //为了显示温度后的小数点后一位并作出四舍五入,因为取值运算不能取小数点后的数return temp;
}void beep_warning() //温度传感器蜂鸣器警报并且电机转动{ if(ftemp>wdyz){beep = 1;led1 = 1;} if(R>xlyz){beep = 1;led1 =1;} if(R1>xyyz){beep = 1;led1 = 1;} if(R2>jsyz){beep = 1;led1 = 1;} if(ftemp<wdyz && R< xlyz && R1<xyyz && R2<jsyz){beep = 0;led1 = 0;}
}void main() //主函数{ led1 = 0;beep = 0; //蜂鸣器关掉LCD_Init(); //显示屏初始化LCD\_ShowString(1,1,"----shouhuan----"); Delay(1000); LCD\_Init(); //显示屏初始化Time0_Init();ET0=1; while(1){ cshq(); //参数获取cslsz();//按键设置if(mode == 5){ beep_warning(); } if(mode1 == 0){ csxs();//参数显示}}
}void csxs() //参数显示{ LCD\_ShowString(1,1,"wd:"); LCD\_ShowNum(1,4,ftemp,3); LCD\_ShowString(1,7,"xl:"); LCD\_ShowNum(1,10,R,3); LCD\_ShowString(2,1,"xy:"); LCD\_ShowNum(2,4,R1,3); LCD\_ShowString(2,8,"jsd:"); LCD\_ShowNum(2,12,R2,3);
}void cshq() //参数获取{u=get\_AD\_Res();U=(250*u)/128; //此处将数字信号转化为模拟信号,要根据上拉电阻阻值来确定R=200*U/250; //获取心率u1=get\_AD\_Res1();U1=(250*u1)/128; //此处将数字信号转化为模拟信号,要根据上拉电阻阻值来确定R1=200*U1/250; //获取血氧u2=get\_AD\_Res2();U2=(250*u2)/128; //此处将数字信号转化为模拟信号,要根据上拉电阻阻值来确定R2=200*U2/250; //获取加速度}void cslsz()//按键判断{KeyNum = MatrixKey();//键盘输入的值进行传递if(KeyNum){ if(KeyNum <= 10) //把按键的范围定义在0~9{ if(count < 3){password*=10; //出水量左移一位password += KeyNum % 10; //获取一位出水量count++; //计次++,对应出水量位数} LCD_ShowNum(1,4,password,3); //LCD更新显示} //确认键if(KeyNum == 11) //把11表示确认,对阈值进行确认{ LCD_ShowNum(1,4,password,3); //LCD更新显示switch(mode){ case 1:wdyz = password;LCD\_ShowNum(2,1,wdyz,3);break; case 2:xlyz = password;LCD\_ShowNum(2,5,xlyz,3);break; case 3:xyyz = password;LCD\_ShowNum(2,9,xyyz,3);break; case 4:jsyz = password;LCD\_ShowNum(2,13,jsyz,3);break;}} //取消键if(KeyNum == 12){password = 0;count = 0; LCD_ShowNum(1,4,password,3); //LCD更新显示} if(KeyNum == 13) //q切换{mode++;password = 0;count = 0; LCD_ShowNum(1,4,password,3); //LCD更新显示switch(mode){ case 1:LCD\_ShowString(1,1,"wd:");break; case 2:LCD\_ShowString(1,1,"xl:");break; case 3:LCD\_ShowString(1,1,"xy:");break; case 4:LCD\_ShowString(1,1,"js:");break;}password = 0;count = 0; if(mode>5){mode = 0;}} if(KeyNum == 14){jsflag++; if(jsflag>1){jsflag = 0;}} if(KeyNum==15){ LCD_Init(); //显示屏初始化mode1++; if(mode1>1){mode1=0;}}} }
四、实现现象
具体动态效果看B站演示视频:
B站演示视频
基于单片机的手环设计
全部资料(源程序、仿真文件、安装包、演示视频):
百度网盘资料下载