温度传感器的c语言程序,DS18B20数字温度传感器C语言程序实例

51单片机DS18B20数字温度传感器设计 与C程序

#include

#define uchar unsigned char

#define uint unsigned int

#define DQ RC1

#define DQ_HIGH() TRISC1=1

#define DQ_LOW() TRISC1=0;DQ=0

__CONFIG(0x3B31);

const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,

0x07,0x7f,0x6f};

const uchar table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,

0x87,0xff,0xef};

uint temper;

uchar a1,a2,a3,a4;

void delay(uint x);

void delayus(uint,uchar);

void init();

void disp(uchar num1,uchar num2,uchar num3,uchar num4);

void write_byte(uchar date);

uchar read_byte();

void get_tem();

void main()

{

init();

while(1)

{

get_tem();

}

}

void reset()

{

uchar st=1;

DQ_HIGH();

NOP();NOP();

while(st)

{

DQ_LOW();

delayus(70,30);

DQ_HIGH();

delayus(4,4);

if(DQ==1)

st=1;

else

st=0;

delayus(50,10);

}

}

void write_byte(uchar date)

{

uchar i,temp;

DQ_HIGH();

NOP();NOP();

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

{

temp=date&0x01;//01010101

DQ_LOW();

delayus(0,0);

if(temp==1)

DQ_HIGH();

delayus(2,2);

DQ_HIGH();

date=date>>1;//00101010

}

}

uchar read_byte()

{

uchar i,date;

static bit j;

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

{

date=date>>1;

DQ_HIGH();

NOP();NOP();

DQ_LOW();

NOP();NOP();NOP();NOP();NOP();NOP();

DQ_HIGH();

NOP();NOP();NOP();NOP();

j=DQ;

if(j==1)

date=date|0x80;//1000 0000

delayus(1,1);

}

return (date);

}

void get_tem()

{

uchar tem1,tem2,num;

float aaa;

reset(); //复位

write_byte(0xCC);//跳过ROM

write_byte(0x44);//温度转换

for(num=100;num>0;num--)

disp(a1,a2,a3,a4);

reset();

write_byte(0xCC);

write_byte(0xBE);

tem1=read_byte();

tem2=read_byte();

aaa=(tem2*256+tem1)*6.25;

temper=(int)aaa;

a1=temper/1000;

a2=temper%1000/100;

a3=temper%100/10;

a4=temper%10;

}

void delayus(uint x,uchar y)

{

uint i;

uchar j;

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

for(j=y;j>0;j--);

}

void delay(uint x)

{

uint a,b;

for(a=x;a>0;a--)

for(b=110;b>0;b--);

}

void init()

{

TRISD=0;

TRISA=0;

PORTD=0;

PORTA=0;

}

void disp(uchar num1,uchar num2,uchar num3,uchar num4)

{

PORTD=table[num1];//显示第一个数码管

PORTA=0x20;//0010 0000

delay(2);

PORTD=table1[num2];//显示第二个数码管

PORTA=0x10;//0001 0000

delay(2);

PORTD=table[num3];//显示第三个数码管

PORTA=0x08;//0000 1000

delay(2);

PORTD=table[num4];//显示第四个数码管

PORTA=0x04;//0000 0100

delay(2);

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值