温度报警限制上下值
由oled显示
制作出来的实物图如下:
单片机源程序如下:
- #include<reg52.h>
- #include "oled.h"
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char set=0;
- unsigned char code table[]="0123456789";
- uchar shi,bai,ge,xiaoshu;
- sbit DSPORT=P3^7;
- sbit k1=P2^7;
- sbit k2=P2^6;
- sbit k3=P3^2;
- bit N_flag;
- int shangxian=22;
- int xiaxian=10;
- int temp;
- sbit beep=P1^5;
- sbit alam=P1^6;
- uchar H1,H2,L1,L2;
- void delay(uint c)
- {
- uint x,y;
- for(x=c;x>0;x--)
- for(y=123;y>0;y--);
- }
- void Delay1ms(unsigned int y)
- {
- unsigned int x;
- for( ; y>0; y--)
- {
- for(x=110; x>0; x--);
- }
- }
- uchar Ds18b20Init()
- {
- uchar i;
- DSPORT = 0; //将总线拉低480us~960us
- i = 70;
- 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)
- {
- unsigned int i, j;
- for(j=0; j<8; j++)
- {
- DSPORT = 0; //每写入一位数据之前先把总线拉低1us
- i++;
- DSPORT = dat & 0x01; //然后写入一个数据,从最低位开始
- i=6;
- while(i--); //延时68us,持续时间最60us
- DSPORT = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
- dat >>= 1;
- }
- }
- uchar Ds18b20ReadByte()
- {
- uchar byte, bi;
- unsigned int i, j;
- for(j=8; j>0; j--)
- {
- DSPORT = 0;//先将总线拉低1us
- i++;
- DSPORT = 1;//然后释放总线
- i++;
- i++;//延时6us等待数据稳定
- bi = DSPORT; //读取数据,从最低位开始读取
- /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
- byte = (byte >> 1) | (bi << 7);
- i = 4; //读取完之后等待48us再接着读取下一个数
- while(i--);
- }
- return byte;
- }
- void Ds18b20ChangTemp()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0x44); //温度转换命令
- //Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
-
- }
- void Ds18b20ReadTempCom()
- {
- Ds18b20Init();
- Delay1ms(1);
- Ds18b20WriteByte(0xcc); //跳过ROM操作命令
- Ds18b20WriteByte(0xbe); //发送读取温度命令
- }
- uint Ds18b20ReadTemp()
- {
- int temp = 0;
- uchar tmh, tml;
- Ds18b20ChangTemp(); //先写入转换命令
- Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
- tml = Ds18b20ReadByte(); //读取温度值共16位,先读低字节
- tmh = Ds18b20ReadByte(); //再读高字节
- temp = tmh;
- temp <<= 8;
- temp |= tml;
- if(temp>0x0fff)
- {
- N_flag=1;
- temp=temp-1;
- temp=~temp;
- }
- else
- {
- N_flag=0;
- }
- temp=temp*0.625;
-
- bai=temp/1000;
- shi=temp%1000/100;
- ge=temp%1000%100/10;
- xiaoshu=temp%10;
- return (temp);
- }
- void keyscan()
- {
-
- if(set==0)
- {
- if(k1==0)
- {
- delay(10);
- if(k1==0)shangxian++;
- }
- if(k2==0)
- {
- delay(10);
- if(k2==0)shangxian--;
- }
- }
- if(set==1)
- {
- if(k1==0)
- {
- delay(10);
- if(k1==0)xiaxian++;
- }
- if(k2==0)
- {
- delay(10);
- if(k2==0)xiaxian--;
- }
- }
- H1=shangxian/10;
- H2=xiaxian/10;
- L1=shangxian%10;
- L2=xiaxian%10;
- }
- void alarm()
- {
- temp=Ds18b20ReadTemp();
- if(temp/10>=shangxian)
- {
- beep=0;
- alam=0;
- delay(10);
- beep=1;
- alam=1;
- delay(10);
- }
- else
- {
- beep=1;
- alam=1;
- }
- if(temp/10<=xiaxian)
- {
- beep=~beep;
- alam=~alam;
- delay(10);
- }
- else
- {
- beep=1;
- alam=1;
- }
- }
- void main(void)
- { IT0=1;
- EX0=1;
- EA=1;
- OLED_Init(); //初始化OLED
- OLED_Clear();
- while(1)
- {
- Ds18b20ReadTemp();
- keyscan();
- alarm();
- OLED_ShowCHinese(0,0,0);
- OLED_ShowCHinese(16,0,1);
- OLED_ShowChar(32,0,':');
- if(temp<0)
- {
- OLED_ShowChar(40,0,'-');
- }
- else
- {
- OLED_ShowChar(40,0,'+');
- }
-
- OLED_ShowNum(48,0,bai,1,8);
- OLED_ShowNum(56,0,shi,1,8);
- OLED_ShowNum(64,0,ge,1,8);
- OLED_ShowChar(72,0,'.');
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
oled的温度显示.rar
(159.05 KB, 下载次数: 172)
|