本帖是关于单片机的温度计显示
传感器用的是ds18b20,
采用数码管动态显示,可显示3位数的温度,第二位带小数点
本实验还包括蜂鸣器报警,温度高于某值,蜂鸣器报警
附件包括仿真原理图,proteus工程文件夹,txt格式的代码
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
熟练掌握C语言的设计和调试方法,了解与掌握数字温度传感器DS18B20的工作原理。 利用数字温度传感器DS18B20与单片机结合来测量温度控制器使用单片机AT89C51,测温传感器使用DS18B20,用3位LED数码管以串口传送数据,实现温度显示。且设置一个门限温度,当传感器温度达到门限温度后,发出提醒。 要求完成实验内容,LED数码管成功显示示数。设置报警门限,若温度超限蜂鸣器报警。 图1 硬件电路图
图 1未达门限温度截过图 图 2达到门限温度结果图 实验总结:通过这次实验,我了解了DS13B20的基本原理和编程方法,更加熟悉了多位数码管的动态显示,自己的编写代码的能力也增强了许多。但这次实验也反映了自己的一些问题,编写基本的代码还可以,稍微有些难度的代码就编写不出来了,以后会多加练习,克服这个问题。
单片机源程序如下:
- /* Main.c file generated by New Project wizard
- *
- * Created: 周一 7月 6 2020
- * Processor: AT89C52
- * Compiler: Keil for 8051
- */
- #include <reg51.h>
- #include <stdio.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit X=P2^6;
- sbit Y=P2^7;
- sbit Z=P2^3;
- sbit DQ=P2^2;
- uchar code Data[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delay(uint count) //delay 延时
- {
- uint i;
- while(count)
- {
- i=200;
- while(i>0)
- i--;
- count--;
- }
- }
- void dsreset(void) //重置、初始化命令
- {
- uint i;
- DQ=0; //赋值
- i=103; //延时480us-960us
- while(i>0)i--;
- DQ=1; //赋值
- i=4;
- while(i>0)i--; //延时15-60us
- }
- bit tmpreadbit(void) //读一位
- {
- uint i;
- bit dat;
- DQ=0;
- i++; //i++ for delay 延时1us-15us
- DQ=1;
- i++;i++; //释放总线延时
- dat=DQ; //读数赋值
- i=8;
- while(i>0)i--;
- return (dat);//返回所获得读数
- }
- uchar tmpread(void) //读一个字节 8Bit
- {
- uchar i,j,dat;
- dat=0;
- for(i=1;i<=8;i++)//连续读取8位,1字节
- {
- j=tmpreadbit(); //调用读1位的函数
- dat=(j<<7|(dat>>1)); //读出的数据最低位在最前面,动脑想如何设计
- }
- return(dat);//返回1字节的读数
- }
- void tmpwritebyte(uchar dat) //写一个字节
- {
- uint i;
- uchar j;
- bit testb;//写入的数
- for(j=1;j<=8;j++)//连续写8位
- {
- testb=dat&0x01;//思考如何设计每次读到的1bit
- dat=dat>>1;
- if(testb) //write 1
- {
- DQ=0;//赋值
- i++;i++;
- DQ=1;//赋值
- i=8;while(i>0)i--;
- }
- else //write 0
- {
- DQ=0;//赋值
- i=8;while(i>0)i--;
- DQ=1;//赋值
- i++;i++;
- }
- }
- }
- void tmpchange(void) //DS18B20 转换温度
- {
- dsreset();//初始化
- delay(1);
- tmpwritebyte(0xcc);//跳过ROM
- tmpwritebyte(0x44);//转换温度
- }
- void tempread(void)
- {
- dsreset(); //写入相关指令,思考要写什么指令
- delay(1);
- tmpwritebyte(0xcc);//跳过RON
- tmpwritebyte(0xbe);//读取暂存器
- }
- uint tmp() //获得温度
- {
- float tt;
- uchar a,b;
- uint temp;
- tmpchange();
- tempread();
- a=tmpread();
- b=tmpread();
- temp=b;
- temp<<=8; //two byte compose a int variable
- temp=temp|a;
- tt=temp*0.0625;
- temp=tt*10+0.5;
- return temp;
- }
- void display(uint temp) //显示程序
- {
- Y=0;
- P0=0xfe;
- Y=1;
- Y=0;
- X=0;
- P0=Data[temp/100];
- X=1;
- X=0;
- delay(1);
- Y=0;
- P0=0xfd;
- Y=1;
- Y=0;
- X=0;
- P0=Data[(temp%100)/10]+0x80;
- X=1;
- X=0;
- delay(1);
-
- Y=0;
- P0=0xfb;
- Y=1;
- Y=0;
- X=0;
- P0=Data[(temp%100)%10];
- X=1;
- X=0;
- delay(1);
- if(temp>=400)
- {
- Z=0;
- }
- else Z=1;
- }
- void main()
- {
- uchar a,temp;
- do
- {
- tmpchange();//温度转换
- for(a=10;a>0;a--) //避免闪烁
- {
- display(tmp());//调用显示函数
- }
- } while(1);
- }
复制代码
所有资料51hei提供下载:
温度计设计.zip
(247.08 KB, 下载次数: 36)
|