可设定限制温度,超过则报警,绿灯灭,在限制温度内绿灯亮
制作出来的实物图如下:
电路原理图如下:
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- /*********************************************************************************
- * 【作 者】: 清翔电子:03
- * 【版 本】: 1.0
- * 【实验平台】: QX-MCS51 单片机开发板
- * 【外部晶振】: 11.0592mhz
- * 【主控芯片】: STC89C52
- * 【编译环境】: Keil μVisio3
- * 【程序功能】: 1602液晶显示18B20温度
- * 【使用说明】:
- * 说明:免费开源,不提供源代码分析.
- **********************************************************************************/
- #include <reg52.H>
- #include <intrins.H>
- #include <math.H>
- #define uchar unsigned char
- #define uint unsigned int
- //sbit dula = P2^6;
- //sbit wela = P2^7;
- sbit rw = P3^6;
- sbit RS = P3^5;
- sbit LCDEN = P3^4;
-
- sbit green=P1^0;
- sbit beep=P2^3;
-
- sbit K1=P3^0;
- sbit K2=P3^1;
-
-
- uchar str[5];//uchartostr函数转换的字符串
- float H=380;
-
-
- void delayUs()
- {
- _nop_();
- }
- void delayMs(uint a)
- {
- uint i, j;
- for(i = a; i > 0; i--)
- for(j = 100; j > 0; j--);
- }
- void writeComm(uchar comm)
- {
- RS = 0;
- P0 = comm;
- LCDEN = 1;
- delayUs();
- LCDEN = 0;
- delayMs(1);
- }
- //写数据:RS=1, RW=0;
- void writeData(uchar dat)
- {
- RS = 1;
- P0 = dat;
- LCDEN = 1;
- delayUs();
- LCDEN = 0;
- delayMs(1);
- }
- void init()
- {
- rw = 0;
- // dula = wela = 0;
- writeComm(0x38);
- writeComm(0x0c);
- writeComm(0x06);
- writeComm(0x01);
- }
- void writeString(uchar * str, uchar length)
- {
- uchar i;
- for(i = 0; i < length; i++)
- {
- writeData(str[i]);
- }
- }
-
- /**//*****************************DS18B20*******************************/
- sbit ds = P2^2;
- void dsInit()
- {
-
- unsigned int i;
- ds = 0;
- i = 100;
- while(i>0) i--;
- ds = 1;
- i = 4;
- while(i>0) i--;
- }
-
- void dsWait()
- {
- unsigned int i;
- while(ds);
- while(~ds);
- i = 4;
- while(i > 0) i--;
- }
- bit readBit()
- {
- unsigned int i;
- bit b;
- ds = 0;
- i++;
- ds = 1;
- i++; i++;
- b = ds;
- i = 8;
- while(i>0) i--;
- return b;
- }
- unsigned char readByte()
- {
- unsigned int i;
- unsigned char j, dat;
- dat = 0;
- for(i=0; i<8; i++)
- {
- j = readBit();
-
- dat = (j << 7) | (dat >> 1);
- }
- return dat;
- }
- void writeByte(unsigned char dat)
- {
- unsigned int i;
- unsigned char j;
- bit b;
- for(j = 0; j < 8; j++)
- {
- b = dat & 0x01;
- dat >>= 1;
-
- if(b)
- {
- ds = 0; i++; i++;
- ds = 1;
- i = 8; while(i>0) i--;
- }
- else
- {
- ds = 0;
- i = 8; while(i>0) i--;
- ds = 1;
- i++; i++;
- }
- }
- }
- void sendChangeCmd()
- {
- dsInit();
- dsWait();
- delayMs(1);
- writeByte(0xcc);
- writeByte(0x44);
- }
- void sendReadCmd()
- {
- dsInit();
- dsWait();
- delayMs(1);
- writeByte(0xcc);
- writeByte(0xbe);
- }
- int getTmpValue()
- {
- unsigned int tmpvalue;
- int value;
- float t;
- unsigned char low, high;
- sendReadCmd();
-
- low = readByte();
- high = readByte();
-
- tmpvalue = high;
- tmpvalue <<= 8;
- tmpvalue |= low;
- value = tmpvalue;
-
- \
- t = value * 0.0625;
- \
- value = t * 100 + (value > 0 ? 0.5 : -0.5); //大于0加0.5, 小于0减0.5
- return value;
- }
- void display(int v)
- {
- unsigned char count;
- unsigned char datas[] = {0, 0, 0, 0, 0};
- unsigned int tmp = abs(v);
- datas[0] = tmp / 10000;
- datas[1] = tmp % 10000 / 1000;
- datas[2] = tmp % 1000 / 100;
- datas[3] = tmp % 100 / 10;
- datas[4] = tmp % 10;
- writeComm(0xc0+3);
- if(v < 0)
- {
- writeString("- ", 2);
- }
- else
- {
- writeString("+ ", 2);
- }
- if(datas[0] != 0)
- {
- writeData('0'+datas[0]);
- }
- for(count = 1; count != 5; count++)
- {
- writeData('0'+datas[count]);
- if(count == 2)
- {
- writeData('.');
- }
- }
- }
- void lcd_dis(uchar X,uchar Y,uchar *dis) //显示数据数组
- {
- uchar pos;
- switch(X)
- {
- case 0:X=0x00;
- break;
- case 1:X=0x40;
- break;
- break;
- default:break;
- }
- pos =0x80+X+Y;
- writeComm(pos); //显示地址
- while(*dis!='\0')
- {
- writeData(*dis++);
- }
- }
- uchar *uchartostr(float num) //将一个字节的数据转换为字符串 或10进制
- {
- uchar x2,x1,x0,i;
- x2=(int)num/100;
- x1=(int)num%100/10;
- x0=(int)num%100%10;
- i=0;
-
- str[i]=x2+48;
- i++;
-
-
- str[i]=x1+48;
- i++;
-
-
- str[i]='.';
- i++;
-
- str[i]=x0+48;
- i++;
- str[i]='\0';
- return str;
- }
- uchar *mtostr(uchar num) //将一个字节的数据转换为字符串 或10进制
- {
- int i=0;
- str[i]=num+48;
- i++;
- str[i]='\0';
- return str;
- }
- uchar key(void)
- {
-
-
- if(K1==0)
- {
- delayMs(20);
- if(K1==0)
- {
- if(H<1000)
- H++;
- }
- }
-
- if(K2==0)
- {
- delayMs(20);
- if(K2==0)
- {
- if(H>0)
- H--;
- }
- }
-
-
-
-
- }
- /**//*****************************DS18B20*******************************/
- void main()
- {
- green=0;
-
- sendChangeCmd();
- init();
- writeComm(0x80);
- writeComm( 0x80 + 4);
- writeString("thermometre",10);
- delayMs(1500);
- writeComm( 0x01);
- delayMs(15);
- writeComm( 0x80 + 3);
- //writeData( "" );
- writeString("LIMIT",5);
-
- while(1)
- {
- delayMs(100); //温度转换时间需要750ms以上
- writeComm(0xc0);
- lcd_dis( 0,9,uchartostr(H));
- display(getTmpValue());
- sendChangeCmd();
-
- if(getTmpValue()>H*10)
- {
- green=1;
- beep=0;
-
- }
- else
- {
- green=0;
- beep=1;
- }
-
- key();
- }
- }
复制代码
所有资料51hei提供下载:
单片机课程设计.zip
(317.92 KB, 下载次数: 143)
|