我今天也编了一个,18B20~~~用数码管显示温度的。发上来分享一下
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^0;
uint temp;
float f_temp;
/*uint warn_11=270;
uint warn_12=250;
uint warn_h1=300;
uint warn_h2=320;*/
unsigned char code table[]=
{0xc0, 0xf9, 0xa4, 0xb0, 0x99, //不带小数点
0x92, 0x82, 0xf8, 0x80, 0x98,
0x40, 0x79, 0x24, 0x30, 0x19, //带小数点
0x12, 0x02, 0x78, 0x00, 0x18
};
void delay(uint z)//延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void dsreset(void) //18B20复位,初始化函数
{
uint i;
ds=0;
i=103;
while(i>0)i--;
ds=1;
i=4;
while(i>0)i--;
}
bit tempreadbit(void) //读1位函数
{
uint i;
bit dat;
ds=0;i++; //i++ 起延时作用
ds=1;i++;i++;
dat=ds;
i=8;while(i>0)i--;
return (dat);
}
uchar tempread(void) //读1个字节
{
uchar i,j,dat;
dat=0;
for(i=1;i1;
if(testb) //写 1
{
ds=0;
i++;i++;
ds=1;
i=8;while(i>0)i--;
}
else
{
ds=0; //写 0
i=8;while(i>0)i--;
ds=1;
i++;i++;
}
}
}
void tempchange(void) //DS18B20 开始获取温度并转换
{
dsreset();
delay(1);
tempwritebyte(0xcc); // 写跳过读ROM指令
tempwritebyte(0x44); // 写温度转换指令
}
uint get_temp() //读取寄存器中存储的温度数据
{
uchar a,b;
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0xbe);
a=tempread(); //读低8位
b=tempread(); //读高8位
temp=b;
temp0;i--)
{
dis_temp(get_temp());}
for(i=10;i>0;i--)
{
dis_temp(get_temp());}
}
}