Zhongke Bluexun [RT-Thread RISC-V Evaluation Board] DS18B20 Temperature Measurement
[Copy link]
DS18B20 is a temperature measurement chip that we often use. This time we mounted DS18B20 on the AB32VG1 evaluation board to realize the temperature measurement function.
The DS18B20 driver timing can be found on Baidu, so I will not go into details here.
Create a new project and add the temperature measurement code as follows:
#define DS18B20_DQ GET_PIN(E, 7) //DQ数据线
//复位DS18B20
void DS18B20_Rst(void)
{
rt_pin_mode(DS18B20_DQ, PIN_MODE_OUTPUT); //输出模式
rt_pin_write(DS18B20_DQ, PIN_LOW); //拉低DQ
rt_hw_us_delay(360); //拉低750us(480~960us)
rt_pin_write(DS18B20_DQ, PIN_HIGH); //拉高DQ
}
//等待DS18B20应答,有应答返回0,无应答返回1
uint8_t DS18B20_Check(void)
{
uint8_t retry=0;
//进入接收模式,等待应答信号
//等待时间
rt_pin_mode(DS18B20_DQ, PIN_MODE_INPUT); //输入模式
rt_hw_us_delay(15); //等待15~60us
while(rt_pin_read(DS18B20_DQ)&&retry<30) //最多再等待120us
{
retry++;
rt_hw_us_delay(1);
};
if(retry>=30)
return 1; //120us未响应,则判断未检测到
else
retry=0;
//DS18B20开始拉低DQ
while(!rt_pin_read(DS18B20_DQ)&&retry<38) //最长拉低240us
{
retry++;
rt_hw_us_delay(1);
};
if(retry>=38)
return 1;
return 0;
}
//写字节到DS18B20
void DS18B20_Write_Byte(uint8_t dat)
{
uint8_t j;
uint8_t temp;
rt_pin_mode(DS18B20_DQ, PIN_MODE_OUTPUT); //输出模式
for(j=1;j<=8;j++)
{
temp=dat&0x01;
dat=dat>>1;
if (temp) //输出高
{
rt_pin_write(DS18B20_DQ, PIN_LOW); //拉低DQ
rt_hw_us_delay(1); //延时2us
rt_pin_write(DS18B20_DQ, PIN_HIGH); //拉高DQ
rt_hw_us_delay(29); //延时60us
}
else //输出低
{
rt_pin_write(DS18B20_DQ, PIN_LOW); //拉低DQ
rt_hw_us_delay(29); //延时60us
rt_pin_write(DS18B20_DQ, PIN_HIGH); //拉高DQ
rt_hw_us_delay(1); //延时2us
}
}
rt_pin_mode(DS18B20_DQ, PIN_MODE_INPUT); //输入模式
}
//从DS18B20读位
uint8_t DS18B20_Read_Bit(void)
{
uint8_t data;
rt_pin_mode(DS18B20_DQ, PIN_MODE_OUTPUT); //输出模式
rt_pin_write(DS18B20_DQ, PIN_LOW); //拉低DQ
rt_hw_us_delay(1); //延时2us
rt_pin_write(DS18B20_DQ, PIN_HIGH); //拉高DQ
rt_pin_mode(DS18B20_DQ, PIN_MODE_INPUT); //输入模式
rt_hw_us_delay(2); //延时12us
if(rt_pin_read(DS18B20_DQ)) //读DQ数据
data=1;
else
data=0;
rt_hw_us_delay(25); //延时50us
return data;
}
//从DS18B20读字节
uint8_t DS18B20_Read_Byte(void)
{
uint8_t i, j, dat=0;
for(i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
//开始温度转换
void DS18B20_Start(void)
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);
DS18B20_Write_Byte(0x44);
}
//读取温度值
float DS18B20_Get_Temp(void)
{
uint8_t sign; //温度符号,0为-,1为+
uint8_t TL,TH;
uint16_t temp;
float temp1;
DS18B20_Start ();
rt_thread_mdelay(800); //等待转换完成
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);
DS18B20_Write_Byte(0xbe);
TL=DS18B20_Read_Byte();
TH=DS18B20_Read_Byte();
if(TH>7)
{
TH=~TH;
TL=~TL;
sign=0; //温度为负
}
else
sign=1; //温度为正
temp=TH; //高八位
temp<<=8;
temp+=TL; //低八位
temp1=(float)temp * 0.0625; //转换实际温度
if(sign)
return temp1; //返回温度值
else
return -temp1;
}
uint8_t DS18B20_Init(void)
{
DS18B20_Rst();
return DS18B20_Check();
}
int main(void)
{
float Temperature = 0.0;
uint8_t pin = rt_pin_get("PA.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_pin_mode(DS18B20_DQ, PIN_MODE_OUTPUT);
if(DS18B20_Init())
{
rt_kprintf(" Not detected DS18B20\n ");
}
else
{
rt_kprintf(" Detected DS18B20\n ");
}
while (1)
{
rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(1000);
rt_pin_write(pin, PIN_HIGH);
rt_thread_mdelay(1000);
Temperature = DS18B20_Get_Temp();
rt_kprintf("\nTemperature:%.1f", Temperature);
}
}
Since RT-Thread's rt_kprintf cannot print floating-point data, rt_kprintf needs to be modified as follows
At the same time, add the header file #include <stdio.h>
to compile and download the program. The effect is as follows:
|