单片机源程序如下:
- #include<reg51.h>
- #include<intrins.h> //包含头文件
- #define uchar unsigned char
- #define uint unsigned int //宏定义
- #include "eeprom52.h"
- ////////////////////
- sbit dj=P1^0;
- sbit dq=P1^1;
- sbit DQ=P1^6;
- sbit bj=P1^7;
- //////////按键接口/////////////////////////////////
- sbit key1=P3^5;//
- sbit key2=P3^6;//
- sbit key3=P3^7;//
- //////////////////////////////////////////////////////
- sbit w1=P2^4;
- sbit w2=P2^5;
- sbit w3=P2^6;
- sbit w4=P2^7; //
- /////共阴数码管段选//////////////////////////////////////////////
- uchar table[22]=
- {0x3F,0x06,0x5B,0x4F,0x66,
- 0x6D,0x7D,0x07,0x7F,0x6F,
- 0x77,0x7C,0x39,0x5E,0x79,0x71,
- 0x40,0x38,0x76,0x00,0xff,0x37};//'-',
- uint wen_du;
- uint shang,xia; //
- uchar dang;//
- uchar flag;
- uchar d1,d2,d3;//
- uchar m;
- uchar js;
- /******************把数据保存到单片机内部eeprom中******************/
- void write_eeprom()
- {
- SectorErase(0x2000);
- byte_write(0x2000, shang);
- byte_write(0x2001, xia);
- byte_write(0x2060, a_a);
- }
- /******************把数据从单片机内部eeprom中读出来*****************/
- void read_eeprom()
- {
- shang = byte_read(0x2000);
- xia = byte_read(0x2001);
- a_a = byte_read(0x2060);
- }
- /**************开机自检eeprom初始化*****************/
- void init_eeprom()
- {
- read_eeprom();
- if(a_a != 1)
- {
- shang = 30;
- xia = 20;
- a_a = 1;
- write_eeprom();
- }
- }
- void delay(uint ms) //延时函数,大约延时1ms
- {
- uchar x;
- for(ms;ms>0;ms--)
- for(x=121;x>0;x--);
- }
- /***********ds18b20延迟子函数(晶振12MHz )*******/
- void delay_18B20(uint i)
- {
- while(i--);
- }
- /**********ds18b20初始化函数**********************/
- void Init_DS18B20()
- {
- uchar x=0;
- DQ=1;
- delay_18B20(8);
- DQ=0;
- delay_18B20(80);
- DQ=1;
- delay_18B20(14);
- x=DQ;
- delay_18B20(20);
- }
- /***********ds18b20读一个字节**************/
- uchar ReadOneChar()
- {
- uchar i=0;
- uchar dat=0;
- for (i=8;i>0;i--)
- {
- DQ=0; //
- dat>>=1;
- DQ=1; //
- if(DQ)
- dat|=0x80;
- delay_18B20(4);
- }
- return(dat);
- }
- /*************ds18b20写一个字节****************/
- void WriteOneChar(uchar dat)
- {
- uchar i=0;
- for (i=8;i>0;i--)
- {
- DQ=0;
- DQ=dat&0x01;
- delay_18B20(5);
- DQ=1;
- dat>>=1;
- }
- }
- /**************读取ds18b20当前温度************/
- void ReadTemperature()
- {
- uchar a=0;
- uchar b=0;
- uchar t=0;
- Init_DS18B20();
- WriteOneChar(0xCC);
- WriteOneChar(0x44);
- delay_18B20(100);
- Init_DS18B20();
- WriteOneChar(0xCC);
- WriteOneChar(0xBE);
- delay_18B20(100);
- a=ReadOneChar();
- b=ReadOneChar();
- wen_du=((b*256+a)>>4);
- }
- void zi_keyscan()
- {
- if(key1==0)
- {
- delay(80);
- if(key1==0)
- {
- flag=1;
- while(key1==0);
- }
- }
- while(flag==1)
- {
- d1=18;d2=shang/10;d3=shang%10;
- if(key1==0)
- {
- delay(80);
- if(key1==0)
- {
- flag=2;
- while(key1==0);
- }
- }
- if(key2==0)
- {
- delay(80);
- if(key2==0)
- {
- shang+=1;
- if(shang>=100)shang=100;
- while(key2==0);
- }
- }
- if(key3==0)
- {
- delay(80);
- if(key3==0)
- {
- shang-=1;
- if(shang<=10)shang=10;
- while(key3==0);
- }
- }
- }
- while(flag==2)
- {
- d1=17;d2=xia/10;d3=xia%10;
- if(key1==0)
- {
- delay(80);
- if(key1==0)
- {
- flag=0;
- while(key1==0);
- write_eeprom();
- }
- }
- if(key2==0)
- {
- delay(80);
- if(key2==0)
- {
- xia+=1;
- if(xia>=99)xia=99;
- while(key2==0);
- }
- }
- if(key3==0)
- {
- delay(80);
- if(key3==0)
- {
- xia-=1;
- if(xia<=0)xia=0;
- while(key3==0);
- }
- }
- }
- }
- void init()
- {
- TMOD=0x01;
- TH0=0xf8;
- TL0=0x30;
- ET0=1;
- TR0=1;
- EA=1;
- }
- void zi_dong()
- {
- d1=dang;d2=wen_du/10;d3=wen_du%10;
- zi_keyscan();
- if(wen_du<xia){dang=0;}_du>=xia)&&(wen_du<=shang))
- {dang=1;}
- if(wen_du>shang){dang=2;}}
- void main() //主函数
- {
- uchar j;
- dj=0;
- shang=30;
- xia=20;
- init_eeprom();
- for(j=0;j<80;j++)
- ReadTemperature();
- init();
- while(1)
- {
- if(js>=50)
- {
- ReadTemperature();
- js=0;
- }
-
- zi_dong();}
- }
- void T0_TIME() interrupt 1
- {
- TH0=0xf8;
- TL0=0x30;
- m++; switch(dang)
- {
- case 0:dj=0;break;
- case 1:if(m<=3)dj=1;else dj=0;break;
- case 2:dj=1;break;
- default:;
- }
- switch(m)
- {
- case 1:
- w4=1;P0=~table[d1];w1=0; break;
- case 2:
- w1=1;P0=~table[16];w2=0; break;
- case 3:
- w2=1;P0=~table[d2];w3=0; break;
- case 4:
- w3=1;P0=~table[d3];w4=0;m=0;if(js<50)js++;break; //
-
- default:;
- }
- if(m>4) {
- m=0;
- }
- }
复制代码
|