- #include<reg52.h>
- void LcdDisplay(int);
- void DigDisplay();
- void chaomax();
- void chaomin();
- void guomax();
- void guomin();
- //存储上下限变量
- int tempmax=30,tempmin=20,tempt,tempmode=0;
- int jileimax=0,jileimin=0,jileimid=0;
- void Delay1ms(unsigned char x)
- {
- unsigned char i,j;
- for(i=0;i<x;i++)
- for(j=0;j<112;j++);
- }
- uchar Ds18b20Init()
- {
- uchar i;
- DSPORT = 0; //将总线拉低480us~960us
- //i = 70;
- i = 900;
- while(i--);//延时642us
- DSPORT = 1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
- i = 0;
- while(DSPORT) //等待DS18B20拉低总线
- {
- Delay1ms(1);
- i++;
- if(i>5)//等待>5MS
- {
- return 0;//初始化失败
- }
-
- }
- return 1;//初始化成功
- }
- void Ds18b20WriteByte(uchar dat)//????????
- {
- uint i, j;
- for(j=0; j<8; j++)
- {
- DSPORT = 0; //每写入一位数据之前先把总线拉低1us
- i++;
- DSPORT = dat & 0x01; //然后写入一个数据,从最低位开始
- i=8;
- while(i--); //延时8us
- DSPORT = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
- dat >>= 1;
- }
- }
- [u][i][b]uchar Ds18b20ReadByte()[/b][/i][/u]
- {
- uchar byte, bi;
- uint i, j;
- for(j=8; j>0; j--)
- {
- DSPORT = 0;//先将总线拉低1us
- i++;
- DSPORT = 1;//然后释放总线
- i++;
- i++;//延时2us等待数据稳定
- bi = DSPORT; //读取数据,从最低位开始读取
- /*将byte左移一位,然后或上右移7位后的bi,注意移动之后移掉那位补0。*/
- byte = (byte >> 1) | (bi << 7);
- i = 4; //读取完之后等待4us再接着读取下一个数
- while(i--);
- }
- return byte;
- }
- void Ds18b20ChangTemp()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0x44); //温度转换命令
- Delay1ms(20); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
- }
- void Ds18b20ReadTempCom()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0xbe); //发送读取温度命令
- }
- int Ds18b20ReadTemp()
- {
- int temp = 0;
- uchar tmh, tml;
- Ds18b20ChangTemp(); //先写入转换命令
- Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
- tml = Ds18b20ReadByte(); //读取温度值共16位,先读低字节
- tmh = Ds18b20ReadByte(); //再读高字节
- temp = tmh;
- temp <<= 8;
- temp |= tml;
- return temp;
- }
- void LcdDisplay(int temp) //lcd显示
- float tp;
- if(temp< 0) //当温度值为负数
- {
- DisplayData[0] = 0x40;
- //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
- temp=temp-1;
- temp=~temp;
- tp=temp;
- temp=tp*0.0625*100+0.5;
- //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
- //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
- //算加上0.5,还是在小数点后面。
- }
- else
- {
- DisplayData[0] = 0x00;
- tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
- //如果温度是正的那么,那么正数的原码就是补码它本身
- temp=tp*0.0625*100+0.5;
- //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
- //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
- //算加上0.5,还是在小数点后面。
- }
- DisplayData[0] = DIG_CODE[tempmax / 10];
- DisplayData[1] = DIG_CODE[tempmax % 10];
- DisplayData[4] = DIG_CODE[tempmin / 10];
- DisplayData[5] = DIG_CODE[tempmin % 10];
- DisplayData[2] = DIG_CODE[temp % 10000 / 1000];
- DisplayData[3] = DIG_CODE[temp % 1000/100];
- DigDisplay(); //扫描显示
- void DigDisplay()
- {
- unsigned char i;
- unsigned int j;
- for(i=0;i<6;i++)
- {
- switch(i) //位选,选择点亮的数码管
- {
-
- case(0):
- LS1=0;LS2=1;LS3=1;LS4=1;LS5=1;LS6=1; break;//显示第0位
- case(1):
- LS1=1;LS2=0;LS3=1;LS4=1;LS5=1;LS6=1; break;//显示第1位
-
- case(2):
- LS1=1;LS2=1;LS3=0;LS4=1;LS5=1;LS6=1; break;//显示第2位
- case(3):
- LS1=1;LS2=1;LS3=1;LS4=0;LS5=1;LS6=1; break;//显示第3位
-
- case(4):
- LS1=1;LS2=1;LS3=1;LS4=1;LS5=0;LS6=1; break;//显示第4位
- case(5):
- LS1=1;LS2=1;LS3=1;LS4=1;LS5=1;LS6=0; break;//显示第5位
- }
- GPIO_DIG=DisplayData[i];//发送段码
- j=200; //扫描间隔时间设定
- while(j--);
- GPIO_DIG=0x00;//消隐
- }
- }
- void main()
- {
- buzzer=1;
-
- while(1)
- {
- LcdDisplay(Ds18b20ReadTemp());
- }
- }
- //#include"temp.h"
- void chaomax()
- {
- buzzer=1;
-
- }
- if(key1==0)
- {
- Delay1ms(300);//按键防抖
- if(key1==0)
- {
- tempmax++;
- }
- }
- if(key2==0)
- {
- Delay1ms(300);
- if(key2==0)
- {
- tempmax--;
- }
- }
- if(key3==0)
- {
- Delay1ms(300);
- if(key3==0)
- {
- tempmin++;
- }
- }
- if(key4==0)
- {
- Delay1ms(300);
- if(key4==0)
- {
- tempmin--;
- }
- }
- if(temp<(tempmin*100))//*100是为了使当前值和最小值在同一数量级比较
- {
- jileimin++;
- jileimid=0;
- if(jileimin>3)//扫描3次以确定已经低于下限值
- {
- tempmode=1; //区分当前值是否是原值还是恢复值,原值为0,由上限值恢复是2,由下限值恢复时1
- chaomin();
- jileimin=0;//积累值置位
- }
- }
- if(temp>(tempmax*100))
- {
- jileimax++;
- jileimid=0;
- if(jileimax>3)
- {
- tempmode=2;
- chaomax();
- jileimax=0;
- }
- }
-
- if((tempmin*100)<temp&&temp<(tempmax*100))
- {
- jileimax=0;
- jileimin=0;
- jileimid++;
- if(jileimid>3)
- {
- switch(tempmode)
- {
- case 0:buzzer=0;break;
- case 1:guomin();break;
- case 2:guomax();break;
- }
- tempmode=0;
- buzzer=0;
- jileimid=0;
- }
- }
- //三声短鸣表示低于温度下限
- void chaomin()
- {
- buzzer=1;
- Delay1ms(200);
- buzzer=0;
- Delay1ms(200);
- buzzer=1;
- Delay1ms(200);
- buzzer=0;
- Delay1ms(200);
- buzzer=1;
- Delay1ms(200);
- buzzer=0;
- Delay1ms(1000);
- }
- //三声短鸣表示超过温度上限
- void guomax()
- {
- buzzer=0;
- Delay1ms(200);
- buzzer=1;
- Delay1ms(200);
- buzzer=0;
- Delay1ms(200);
- buzzer=1;
- Delay1ms(200);
- buzzer=0;
- Delay1ms(200);
- buzzer=1;
- Delay1ms(200);
- buzzer=0;
- Delay1ms(200);
- }
- //2秒长鸣表示超过温度下限
- void guomin()
- {
- buzzer=1;
- Delay1ms(2000);
- buzzer=0;
- }
复制代码
|