资讯详情

温度传感器DS18B20 89C51单片机C语言程序

#define ucharunsignedchar /*宏定义 字符型数据 整型数据 */

#define uint unsigned int

uint DD;

sbit DQ=P2^2; //定义DS18B20总线I/O

unsigned int temputer;

char Code[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d,

0x7d, 0x07, 0x7f, 0x6f};

uchar code C[] = {0x0, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};

/*****延时子程序*****/

void Delay_DS18B20(int num)

{

while(num--) ;

}

void delay(unsigned int time) //延迟子程序 入口参数 time

{

unsigned int n;

n=0;

while(n{n ;}

return;

}

/*****初始化DS18B20*****/

void Init_DS18B20(void)

{

unsigned char x=0;

DQ = 1; //DQ复位

Delay_DS18B20(2); //稍作延迟

DQ = 0; ///单片机将DQ拉低

Delay_DS18B20(80); //精确延迟大于480us

DQ = 1; ///拉高总线

Delay_DS18B20(14);

x = DQ; ///稍作延迟后,如果x=0初始化成功,x=一是初始化失败

Delay_DS18B20(20);

}

/*****读一个字节*****/

unsigned char ReadOneChar(void)

{

unsigned char i=0;

unsigned char dat = 0;

for (i=8;i>0;i--)

{

DQ = 0; // 给脉冲信号

dat>>=1;

DQ = 1; // 给脉冲信号

if(DQ)

dat|=0x80;

Delay_DS18B20(8);

}

return(dat);

}

/*****写一个字节*****/

void WriteOneChar(unsigned char dat)

{

unsigned char i=0;

for (i=8; i>0; i--)

{

DQ = 0;

DQ = dat&0x01;

Delay_DS18B20(10);

DQ = 1;

dat>>=1;

}

}

/*****读取温度*****/

unsigned int ReadTemperature(void)

{

unsigned char a=0;

unsigned char b=0;

unsigned int t=0;

float tt=0;

Init_DS18B20();

// Init_DS18B20();

WriteOneChar(0xCC); ////跳过读序号列号的操作

WriteOneChar(0x44); //启动温度转换

Delay_DS18B20(20);

Init_DS18B20();

WriteOneChar(0xCC); ////跳过读序号列号的操作

WriteOneChar(0xBE); //读取温度寄存器

a=ReadOneChar(); //读低8位

b=ReadOneChar(); //读高8位

t=b;

t<<=8;

t=t|a;

tt=t*0.0625;

t= tt*10 0.5; ///放大10倍输出,四舍五入

return(t);

}

void main()

{

while(1)

{

temputer=ReadTemperature();//temputer 是3位数的 温度增加了10倍

P0 = Code[temputer/100];

P1 = C[1];

delay(100);

DD=temputer/100;

temputer=temputer-DD*100;

P0 = Code[temputer/10];

P1 = C[2];

delay(100);

P0 = 0x80;

P1 = C[3];

delay(100);

P0 = Code[temputer];

P1 = C[4];

delay(10);

}

}

标签: t7560温度传感器的精确度表tt2181传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台