51单片机单片机DS18B20温度计温度计C语言程序(附详解)语言程序(附详解)
#include#include#include //要用到取绝对值函数abs()//通过DS18B20测试当前环境温度, 并通过数码管显示当前
温度值, 目前显示范围: -55~ +125度sbit wela = P2^7; //数码管位选sbit dula = P2^6; //数码管段选sbit ds =
P2^2;int tempValue;//0-F数码管的编码(共阳极)unsigned char code table[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//0-9数码管的编码(共
阳极), 带小数点unsigned char code tableWidthDot[]={0x40, 0x79, 0x24, 0x30,0x19, 0x12, 0x02,0x78, 0x00,
0x10};//延时函数, 对于11.0592MHz时钟, 例i=10,则大概延时10ms.void delay(unsig
#include<reg51.h>
#include<intrins.h>
#include <math.H> //要用到取绝对值函数abs()
//通过DS18B20测试当前环境温度, 并通过数码管显示当前温度值, 目前显示范围: -55~ +125度
sbit wela = P2^7; //数码管位选
sbit dula = P2^6; //数码管段选
sbit ds = P2^2;
int tempValue;
//0-F数码管的编码(共阳极)
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
//0-9数码管的编码(共阳极), 带小数点
unsigned char code tableWidthDot[]={0x40, 0x79, 0x24, 0x30,
0x19, 0x12, 0x02,0x78, 0x00, 0x10};
//延时函数, 对于11.0592MHz时钟, 例i=10,则大概延时10ms.
void delay(unsigned int i)
{
unsigned int j;
while(i--)
{
for(j = 0; j < 125; j++);
}
}
//初始化DS18B20
//让DS18B20一段相对长时间低电平, 然后一段相对非常短时间高电平, 即可启动
void dsInit()
{
//对于11.0592MHz时钟, unsigned int型的i, 作一个i++操作的时间大于?us
unsigned int i;
ds = 0;
i = 100; //拉低约800us, 符合协议要求的480us以上
评论0