中国农业大学学士论文 附录程序
附录程序
#include
typedef unsigned char uchar ; typedef unsigned int uint ; #define uint unsigned int #define uchar unsigned char
sbit DATA = P3^7; //DS18B20接入口 sbit P10=P1^0; sbit P11=P1^1; sbit P12=P1^2; sbit P13=P1^3; sbit P30=P3^0; sbit beep=P3^1; sbit P35=P3^5;
uint upper,lower,count=0,settemp=0; uchar bai,shi,ge,fen;
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/*延时子函数子程序μs级*/ void delayo(uint numo) {
while(numo--) ; }
/*************DS18b20温度传感器函数*********************/ Init_DS18B20(void) {
uchar x=0;
DATA = 1; //但总线复位 delayo(10); //延时60μs左右 DATA = 0; //拉低总线 delayo(80); //延时大于480μs DATA = 1; //拉高总线 delayo(20);
33
//初始化DS18B20
中国农业大学学士论文 附录程序
x=DATA; //稍延时后,若x=0则初始化成功 ,若x=1则初始化失败 delayo(30);
}
//读一个字节 ReadOneChar(void) {
uchar i=0; uchar dat = 0; for (i=8;i>0;i--) { DATA = 0; // 发脉冲信号 dat>>=1;
DATA = 1; // 发脉冲信号 if(DATA)
dat|=0x80; delayo(8);
}
return(dat);
}
//写一个字节
WriteOneChar(uchar dat) { uchar i=0; for (i=8; i>0; i--) { DATA = 0;
DATA = dat&0x01;
delayo(10); DATA = 1; dat>>=1; }
delayo(8);
}
//读取温度10倍
uint ReadTemperature(void) {
uchar a=0;
34
中国农业大学学士论文 附录程序
uchar b=0; int t=0; uint tt=0; Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器)前两个就是温度 a=ReadOneChar();//读取温度低位 b=ReadOneChar();//读取温度高位 t=b;
tt=(t*256+a)*0.625;//读数结果扩大十倍 return(tt); }
void displayreal(uint aa) //显示实际温度 { P0=0x00;
P2=0xfb; //显示小数点 P0=0X80; delayo(250);
P0=0x00; P2=0xfe;
bai=aa/1000;//显示百位 P0=table[bai]; delayo(250);
P0=0x00; P2=0xfd;
shi=aa/100;//显示十位 P0=table[shi]; delayo(250);
P0=0x00; P2=0xfb;
ge=aa/10;//显示个位
35
中国农业大学学士论文 附录程序
P0=table[ge]; delayo(250);
P0=0x00; P2=0xf7;
fen=aa;//显示十分位 P0=table[fen]; delayo(250); }
//*****************延时子程序ms级*************************** void delay(uint i) { uint j; for(;i>0;i--) for(j=0;j<125;j++); }
//**************蜂鸣器警报程序*********************** void buzzer(uint aa,uint bb,uint cc) {
if(aa>bb||aa //***********温度控制子程序*********************** void control(uint kk,yy) { if(kk>yy) //实际值大于设定值 P30=0; //停止加热 else //实际值小于设定值 P30=1; //加热 } //**************矩阵键盘扫描子程序*********************** uint keyscan() { 36 中国农业大学学士论文 附录程序 uchar temp,keynum; P1=0xff; //P1口输出高电平 P10=0;//**************扫描第一行********************** temp=P1; //读取P1口状态 temp&=0xf0; //读取有或无键按下时temp的状态 if(temp!=0xf0) //判断是否有键按下 { delay(10); //有键按下时去抖 temp=P1; //读取P1口状态 temp&=0xf0; //读取有无键按下时temp的状态 if(temp!=0xf0) //再次确认是否有键按下 { temp=P1; //有键按下,读P1口状态 temp&=0xf0; //取按键按下时的数据 } } P1=0xff; P11=0;//**************扫描第二行********************** temp=P1; temp&=0xf0; if(temp!=0xf0) { delay(10); temp=P1; temp&=0xf0; 37 switch(temp) //确定键值 { case 0xe0:keynum=0;break; } while(temp!=0xf0) //等待按键释放 { temp=P1; //按键未释放读取P1口状态 } case 0xd0:keynum=1;break; case 0xb0:keynum=2;break; case 0x70:keynum=3;break; temp&=0xf0; //为while判断提供判断数据