2.11

本文介绍了一段使用DS18B20温度传感器读取温度的C语言程序实现。程序通过初始化DS18B20并发送读取命令来获取温度数据,然后将读取到的数据转换为实际温度值,并利用LCD显示模块实时显示温度值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<reg52.h>
#include<intrins.h>
#include<stdio.h>
#include"./delay/delay.h"


sbit DS = P2^7;


extern void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str);
extern void InitLcd1602();
bit ack = 0;
void DS18B20_init()
{
    DS = 0;
delay_us(200);
delay_us(200);
DS = 1;
delay_us(30);
if(DS == 0)

   ack = 1;
}
else
{
  ack = 0;
}
delay_us(200);
}


void DS18B20_write_byte(unsigned char byte)
{
    unsigned char i;


for(i = 0;i < 8; i++)
{  
    DS = 1;    
DS = 0;
_nop_();
_nop_();
DS = byte & 0x01;
byte >>= 1;
delay_us(30);  
}
delay_us(30);
DS = 1;
}


bit DS18B20_read_byte()
{
    bit temp;


    DS = 1;
    DS = 0;
_nop_();
_nop_();
DS = 1;
temp  = DS;
delay_us(30);


    return temp;
}


unsigned char DS18B20_read_string()
{
    unsigned char i,j,k ;


for(i = 0; i < 8; i++)
{
   j = DS18B20_read_byte();
        k = (j << 7)|(k >> 1);
}


return k;
}


void main()
{
unsigned char LSB;
unsigned int temp,MSB;
float temp_f;
unsigned char disbuf[20];


InitLcd1602();
while(1)
{
    /*if(ack == 1)
 {
     LED = 0;
 }*/
 DS18B20_init();
 DS18B20_write_byte(0xCC);
 DS18B20_write_byte(0x44);
 delay_ms(20);
 DS18B20_init();
 DS18B20_write_byte(0xCC);
 DS18B20_write_byte(0xBE);
 LSB = DS18B20_read_string();
 MSB = DS18B20_read_string();
 temp = (MSB<<8) + LSB;
 temp_f = (float)temp * 0.0625;
 sprintf(disbuf,"temp is %5.3f c",temp_f);
 LcdShowStr(0,0,disbuf);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值