DS18B20是一种常见的数字温度传感器,其控制命令和数据都是以数字信号的方式输入输出,它的通信接口采用的是1-Wire(单总线)。1-Wire是一种半双工异步的通讯协议(通信双方可以各自约定通信速率互相传输数据,但必须分时复用一根数据线)。
单片机需要控制DQ口,而且使用时要接一个上拉电阻将18B20配置成开漏输出模式。
当在树莓派上面,树莓派是一台电脑主机,只需要将树莓派配置成使能1-Wire就可以通过通信进行访问18B20的内容了。使用命令即可配置
sudo raspi-config
确认,然后重启树莓派。
sudo reboot
通过lsmod | grep w1命令可以查看当前系统支持的单总线协议模块。
然后就是树莓派与DS18B20的接线了。
接线方式
单总线的接口默认是GPIO 4(BCM),所以将温度传感器数据口接入此,然后分别接正负就行了。
然后打开路径 /sys/bus/w1/devices/ /w1_bus_master1/可以看到有个28-xxxxx的文件夹,里面有个w1_slave文件存放了温度信息,可以用cat命令查看。
实例代码
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int Get_DS18B20_Temp(double *temp);
/**
*@brief main
*@param
*@retval the main function
*/
int main(int argc, char **argv)
{
double temp;
if (Get_DS18B20_Temp(&temp) < 0)
{
printf("ERROR: DS18B20 Get Temperature Failure\n");
return -1;
}
printf("Temperature is: %.3f C\n", temp);
return 0;
}
/**
*@brief main
*@param *temp
*@retval 获取DS18B20测量的温度信息
*/
int Get_DS18B20_Temp(double *temp)
{
int fd = -1;
int rv = -1;
char path[50] = "/sys/bus/w1/devices/";//路径名
char dir_name[20];
int found = -1;
DIR *dirp;
struct dirent *direntp;
char buf[128];
char *ptr;
//清空dir_name和buf内存空间的值,避免随机值产生乱码
memset(dir_name, 0, sizeof(dir_name));
memset(buf, 0, sizeof(buf));
//打开文件夹"/sys/bus/w1/devices/"
if ((dirp = opendir(path)) == NULL)
{
//strerror(errno)会提示错误内容
printf("ERROR: Open Directory '%s' Failure: %s\n", path, strerror(errno));
return -1;
}
printf("Open Directory '%s' Successfully\n", path);
//在文件夹中找DS18B20的文件夹28-xx
while ((direntp = readdir(dirp)) != NULL)
{
if (strstr(direntp->d_name, "28-") == NULL)
continue;
//提取文件名称
strncpy(dir_name, direntp->d_name, strlen(direntp->d_name));
printf("Find file: %s\n", dir_name);
found = 1;
break;
}
closedir(dirp);
//找不到该文件夹
if (found == -1)
{
printf("ERROR: Can not find DS18B20 in %s\n", path);
return -2;
}
//将文件夹名和文件名分别拼接到path上
strncat(path, dir_name, strlen(dir_name));
strncat(path, "/w1_slave", sizeof(path)-strlen(path));
//打开w1_slave文件
if ((fd = open(path, O_RDONLY)) < 0)
{
printf("ERROR: Open file '%s' Failure: %s\n", path, strerror(errno));
return -3;
}
printf("Open file '%s' fd[%d] Successfully\n", path, fd);
//从w1_slave中读取内容
if ((rv = read(fd, buf, sizeof(buf))) < 0)
{
printf("ERROR: Read data from file '%s' Failure: %s\n", path, strerror(errno));
rv = -4;
goto cleanup;
}
printf("Read %dB data from file '%s'\n", rv, path);
//从buf里匹配"t=",并将ptr移到数据的首地址上
if ((ptr = strstr(buf, "t=") + 2) == NULL)
{
printf("ERROR: Find data Failure: %s", strerror(errno));
rv = -5;
goto cleanup;
}
//将数据转为double型,除1000得到正常的十进制温度
*temp = atof(ptr)/1000;
cleanup:
if (fd > 0)
close(fd);
return rv;
}