基于单片机的鱼缸温度自动控制系统设计_第1页
基于单片机的鱼缸温度自动控制系统设计_第2页
基于单片机的鱼缸温度自动控制系统设计_第3页
基于单片机的鱼缸温度自动控制系统设计_第4页
基于单片机的鱼缸温度自动控制系统设计_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

附录一仿真图附录二电路原理图附录三主程序清单//程序头函数#include<reg52.h>//显示函数#include<display.h>#include<intrins.h>#include<1302.h>#include<eeprom52.h>//宏定义#defineuintunsignedint#defineucharunsignedcharvoidjiemian_2();voidjiemian_1();intshi,fen,miao;uintwendu;//测量的温度值intup,down,q_shi,q_fen,z_shi,z_fen,k_1;//分别是温度上限温度下限起始时间终止时间//管脚声明sbitjdq=P1^0; //继电器sbitFeng=P2^6;//蜂鸣器//按键sbitKey1=P1^4; //设置sbitKey2=P1^5; //加sbitKey3=P1^6; //减sbitKey4=P1^7; //确定sbitDQ=P2^2; //定义DS18B20总线I/Obitflag_jdq=0;/******************把数据保存到单片机内部eeprom中******************/voidwrite_eeprom(){ SectorErase(0x2000); byte_write(0x2061,up); byte_write(0x2062,down); byte_write(0x2063,q_shi); byte_write(0x2064,q_fen); byte_write(0x2065,z_shi); byte_write(0x2066,z_fen); byte_write(0x2060,a_a); }/******************把数据从单片机内部eeprom中读出来*****************/voidread_eeprom(){ up=byte_read(0x2061); down=byte_read(0x2062); q_shi=byte_read(0x2063); q_fen=byte_read(0x2064); z_shi=byte_read(0x2065); z_fen=byte_read(0x2066); a_a=byte_read(0x2060);}/**************开机自检eeprom初始化*****************/voidinit_eeprom(){ read_eeprom(); //先读 if(a_a!=1) //新的单片机初始单片机内问eeprom { up=39; down=35; q_shi=8; q_fen=0; z_shi=17; z_fen=0; a_a=1; write_eeprom(); //保存数据 } }voiddelay(uintz){ uinti,j; for(i=0;i<z;i++) for(j=0;j<121;j++);}/*****延时子程序*****/voidDelay_DS18B20(intnum){while(num--);}/*****初始化DS18B20*****/voidInit_DS18B20(void){unsignedcharx=0;DQ=1;//DQ复位Delay_DS18B20(8);//稍做延时DQ=0;//单片机将DQ拉低Delay_DS18B20(80);//精确延时,大于480usDQ=1;//拉高总线Delay_DS18B20(14);x=DQ;//稍做延时后,如果x=0则初始化成功,x=1则初始化失败Delay_DS18B20(20);}/*****读一个字节*****/unsignedcharReadOneChar(void){unsignedchari=0;unsignedchardat=0;for(i=8;i>0;i--){DQ=0;//给脉冲信号dat>>=1;DQ=1;//给脉冲信号if(DQ)dat|=0x80;Delay_DS18B20(4);}return(dat);}/*****写一个字节*****/voidWriteOneChar(unsignedchardat){unsignedchari=0;for(i=8;i>0;i--){DQ=0;DQ=dat&0x01;Delay_DS18B20(5);DQ=1;dat>>=1;}}/*****读取温度*****/unsignedintReadTemperature(void){unsignedchara=0;unsignedcharb=0;unsignedintt=0;floattt=0;Init_DS18B20();WriteOneChar(0xCC);//跳过读序号列号的操作WriteOneChar(0x44);//启动温度转换Init_DS18B20();WriteOneChar(0xCC);//跳过读序号列号的操作WriteOneChar(0xBE);//读取温度寄存器a=ReadOneChar();//读低8位b=ReadOneChar();//读高8位t=b;t<<=8;t=t|a;tt=t*0.0625;t=tt*10+0.5;//放大10倍输出并四舍五入return(t);}voidkey(){ //模式选择 if(Key1==0) { delay(5); if(Key1==0) { Feng=0; delay(100); Feng=1; k_1++; if(k_1==4) k_1=5; if(k_1==1) { write_com(0x87); write_com(0x0f); } elseif(k_1==2) write_com(0x8a); elseif(k_1==3) write_com(0x8d); elseif(k_1==4) write_com(0xcf); elseif(k_1==5) { jiemian_2(); write_com(0x84); } elseif(k_1==6) write_com(0x8e); elseif(k_1==7) write_com(0xc3); elseif(k_1==8) write_com(0xc6); elseif(k_1==9) write_com(0xcc); elseif(k_1==10) write_com(0xcf); elseif(k_1>10) { ds1302write(0x8e,0x00); ds1302write(0x80,miao); ds1302write(0x82,fen); ds1302write(0x84,shi); ds1302write(0x8e,0x80); jiemian_1(); write_com(0x87); k_1=1; } while(Key1==0); } } if(Key2==0&&k_1!=0) { delay(5); if(Key2==0&&k_1!=0) { Feng=0; delay(100); Feng=1; if(k_1==1) { shi=shi/16*10+shi%16; shi++; if(shi==24) shi=0; write_com(0x86); write_data(shi/10+0x30); write_data(shi%10+0x30); write_com(0x87); shi=shi/10*16+shi%10; } if(k_1==2) { fen=fen/16*10+fen%16; fen++; if(fen==60) fen=0; write_com(0x89); write_data(fen/10+0x30); write_data(fen%10+0x30); write_com(0x8a); fen=fen/10*16+fen%10; } if(k_1==3) { miao=miao/16*10+miao%16; miao++; if(miao==60) miao=0; write_com(0x8c); write_data(miao/10+0x30); write_data(miao%10+0x30); write_com(0x8d); miao=miao/10*16+miao%10; } if(k_1==5&&up<99) { up++; write_com(0x83); write_data(up/10+0x30); write_data(up%10+0x30); write_com(0x84); } if(k_1==6&&up-1>down) { down++; write_com(0x8d); write_data(down/10+0x30); write_data(down%10+0x30); write_com(0x8e); } if(k_1==7) { q_shi++; if(q_shi>23) q_shi=0; write_com(0xc2); write_data(q_shi/10+0x30); write_data(q_shi%10+0x30); write_com(0xc3); } if(k_1==8) { q_fen++; if(q_fen>59) q_fen=0; write_com(0xc5); write_data(q_fen/10+0x30); write_data(q_fen%10+0x30); write_com(0xc6); } if(k_1==9) { z_shi++; if(z_shi>23) z_shi=0; write_com(0xcb); write_data(z_shi/10+0x30); write_data(z_shi%10+0x30); write_com(0xcc); } if(k_1==10) { z_fen++; if(z_fen>59) z_fen=0; write_com(0xce); write_data(z_fen/10+0x30); write_data(z_fen%10+0x30); write_com(0xcf); } while(Key2==0); } } if(Key3==0&&k_1!=0) { delay(5); if(Key3==0&&k_1!=0) { Feng=0; delay(100); Feng=1; if(k_1==1) { shi=shi/16*10+shi%16; shi--; if(shi<0) shi=23; write_com(0x86); write_data(shi/10+0x30); write_data(shi%10+0x30); write_com(0x87); shi=shi/10*16+shi%10; } if(k_1==2) { fen=fen/16*10+fen%16; fen--; if(fen<0) fen=59; write_com(0x89); write_data(fen/10+0x30); write_data(fen%10+0x30); write_com(0x8a); fen=fen/10*16+fen%10; } if(k_1==3) { miao=miao/16*10+miao%16; miao--; if(miao<0) miao=59; write_com(0x8c); write_data(miao/10+0x30); write_data(miao%10+0x30); write_com(0x8d); miao=miao/10*16+miao%10; } if(k_1==5&&up-1>down) { up--; write_com(0x83); write_data(up/10+0x30); write_data(up%10+0x30); write_com(0x84); } if(k_1==6&&down>0) { down--; write_com(0x8d); write_data(down/10+0x30); write_data(down%10+0x30); write_com(0x8e); } if(k_1==7) { q_shi--; if(q_shi<0) q_shi=23; write_com(0xc2); write_data(q_shi/10+0x30); write_data(q_shi%10+0x30); write_com(0xc3); } if(k_1==8) { q_fen--; if(q_fen<0) q_fen=59; write_com(0xc5); write_data(q_fen/10+0x30); write_data(q_fen%10+0x30); write_com(0xc6); } if(k_1==9) { z_shi--; if(z_shi<0) z_shi=23; write_com(0xcb); write_data(z_shi/10+0x30); write_data(z_shi%10+0x30); write_com(0xcc); } if(k_1==10) { z_fen--; if(z_fen<0) z_fen=59; write_com(0xce); write_data(z_fen/10+0x30); write_data(z_fen%10+0x30); write_com(0xcf); } while(Key3==0); } } if(Key4==0) { delay(5); if(Key4==0) { Feng=0; delay(100); Feng=1; write_com(0x0c); k_1=0; ds1302write(0x8e,0x00); ds1302write(0x80,miao); ds1302write(0x82,fen); ds1302write(0x84,shi); ds1302write(0x8e,0x80); jiemian_1(); write_eeprom(); while(Key4==0); } }}voidjiemian_1(){uchari;write_com(0xc0); write_data('T'); write_data('e'); write_data('m'); write_data('p'); write_data('e'); write_data('r'); write_data('a');write_data('t');write_data('u');write_data('r');write_data('e'); write_data(':'); //温度显示 i++; if(i>=10) { i=0; wendu=ReadTemperature(); } write_data('0'+wendu/100); write_data('0'+wendu/10%10); write_data(0xdf); write_data(0x43); fen=ds1302read(0x83); shi=ds1302read(0x85); miao=ds1302read(0x81); write_com(0x80); write_data('T'); write_data('i'); write_data('m'); write_data('e'); write_data(''); write_data(''); write_data(shi/16+0x30); write_data(shi%16+0x30); write_data('-'); write_data(fen/16+0x30); write_data(fen%16+0x30); write_data('-'); write_data(miao/16+0x30); write_data(miao%16+0x30); write_data(''); write_data('');}voidjiemian_2(){write_com(0x80);write_data('U');write_data('p');write_data(':');write_data(up/10+0x30);write_data(up%10+0x30);write_data('');write_data('');write_data('');write_data('D');write_data('o');write_data('w');write_data('n');write_data(':');write_data(down/10+0x30);write_data(down%10+0x30);write_com(0xc0);write_data('Q');write_data(':');write_data(q_shi/10+0x30);write_data(q_shi%10+0x30);write_data('-');write_data(q_fen/10+0x30);write_data(q_fen%10+0x30);write_data('');write_data('');write_da

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论