一、实验任务
读取温度。
二、原理分析
在这里不多做介绍,如果需要理解原理可以移步https://editor.youkuaiyun.com/md/?articleId=112068955,单片机复习篇。
三、实验过程
读取温度
1、搭好框架(简单框架,可跳过)
#include<stc15f2k60s2.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,
0X40,0X79,0X1A,0X30,0X19,0X12,0X02,0X78,0X00,0X10,0XBF,0XFF};
uchar yi,er,san,si,wu,liu,qi,ba;
void Allinit(void);
void Delayms(int ms);
void Display1(uchar yi,uchar er);
void Display2(uchar san,uchar si);
void Display3(uchar wu,uchar liu);
void Display4(uchar qi,uchar ba);
void main(void)
{
Allinit();
yi=21;er=21;san=21;si=21;wu=21;liu=21;qi=21;ba=21;
while(1)
{
Display1(yi,er);
Display2(san,si);
Display3(wu,liu);
Display4(qi,ba);
}
}
void Display1(uchar yi,uchar er)
{
P2=0XC0;
P0=0X01;
P2=0XE0;
P0=tab[yi];
Delayms(1);
P2=0XC0;
P0=0X02;
P2=0XE0;
P0=tab[er];
Delayms(1);
}
void Display2(uchar san,uchar si)
{
P2=0XC0;
P0=0X04;
P2=0XE0;
P0=tab[san];
Delayms(1);
P2=0XC0;
P0=0X08;
P2=0XE0;
P0=tab[si];
Delayms(1);
}
void Display3(uchar wu,uchar liu)
{
P2=0XC0;
P0=0X10;
P2=0XE0;
P0=tab[wu];
Delayms(1);
P2=0XC0;
P0=0X20;
P2=0XE0;
P0=tab[liu];
Delayms(1);
}
void Display4(uchar qi,uchar ba)
{
P2=0XC0;
P0=0X40;
P2=0XE0;
P0=tab[qi];
Delayms(1);
P2=0XC0;
P0=0X80;
P2=0XE0;
P0=tab[ba];
Delayms(1);
}
void Allinit(void)
{
P2=0XA0;//打开控制蜂鸣器的573
P0=0X00;//关闭蜂鸣器继电器
P2=0XC0;//打开控制数码管位选的573
P0=0XFF;//选中所有数码管
P2=0XE0;//打开控制数码管段选的573
P0=0XFF;//关闭所有的数码管
P2=0X80;//打开控制LED的573
P0=0XFF;//关闭所有的LED
}
void Delayms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=845;j>0;j--);
}
2、载入驱动,驱动代码官方会提供,如果这几个驱动代码在主函数下面,记得提前声明。
void Init_DS18b20(void)
{
DQ=0;
Delay500us();
DQ=1;
Delay500us();
}
void DS18B20_WiteByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat&0x01;//1100 1100 & 0000 0001 = 0000 0000
Delay80us();
DQ=1;
dat >>=1; // 0110 0110 //0011 0011 // 0000 0000
}
}
unsigned char DS18B20_ReadByte(void)
{
unsigned char i;
unsigned char dat;//0000 0000
for(i=0;i<8;i++)
{
DQ=0;
dat >>=1; // 0101 0000
DQ=1;
if(DQ==1)
{
dat=dat|0x80; // 0000 0000 | 1000 0000 = 1000 0000
}
Delay80us();
}
return dat;
}
3、加入新定义的函数和变量
sbit DQ=P1^4;//温度传感器的数据引脚
uchar num;
long Wendu;
unsigned int tt=0;
void Delay500us();
void Delay100us();
void Delay80us();//自定义延时
long TemperGet(void);//获取数据,并进行转换
long TemperGet(void)
{
unsigned char low,high;
long temp;
Init_DS18b20();
DS18B20_WiteByte(0XCC);//1100 1100
DS18B20_WiteByte(0X44);
Delay500us();
Init_DS18b20();
DS18B20_WiteByte(0XCC);
DS18B20_WiteByte(0XBE);
low=DS18B20_ReadByte();
high=DS18B20_ReadByte();
// temp=high<<4; //0000 1010; 1010 0000
// temp=temp|(low>>4); // 1010 0110 ;; 0000 1010 1010 1010
//0.0625
temp=(high&0x0f); //0000 1010; 1010 0000
temp <<=8;
temp|=low; // 1010 0110 ;; 0000 1010 1010 1010
temp=temp*625;
return temp;
}
void Delay80us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 1;
j = 217;
do
{
while (--j);
} while (--i);
}
void Delay100us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 2;
j = 15;
do
{
while (--j);
} while (--i);
}
void Delay500us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 6;
j = 93;
do
{
while (--j);
} while (--i);
}
4、实现功能,主函数中调用并显示就行了
while(1){
Wendu=TemperGet();
yi=Wendu/10;er=Wendu%10;
}
总结
在本篇中学习了温度传感器,结合数码管使用。数码管的使用还包括带小数点的显示,将上述TempGet函数中注释掉的几行代码取消注释,将类似功能的几行代码注释一下。主函数汇总获取的数值要在原来基础上再除1000倍。
——————————————————————————————————————
本文纯原创,如有侵权请联系删除,如有错误,敬请批评指正,谢谢大家。