017——DS18B20驱动开发(基于I.MX6uLL)

目录

一、 模块介绍

1.1 简介

1.2 主要特点

1.3 存储器介绍

1.4 时序

1.5 命令

1.5.1 命令大全   

1.5.2 命令使用

1.5.3 使用示例

1.6 原理图

二、 驱动程序

三、 应用程序

四、 测试


一、 模块介绍

1.1 简介

        DS18B20 温度传感器具有线路简单、体积小的特点,用来测量温度非常简单,在一根通信线上可以挂载多个 DS18B20 温度传感器。用户可以通过编程实现9~12 位的温度读数,每个 DS18B20 有唯一的 64 位序列号,保存在 rom 中,因此一条总线上可以挂载多个 DS18B20。

1.2 主要特点

■采用单总线接口仅需一个端口引脚进行通信
■每颗芯片具有全球唯一的64位的序列号
■具有多点分布式测温功能
■无需外围元器件
■可通过数据线供电;供电电压范围为2.5V∽5.5V
■测度测量范围为-55°C to +125°C(-67°F to +257°F)
■在-10°C∽70°C范围内精确度为±0.4°C
■温度分辨率9-12位可选
■最高12位精度下,温度转换速度小于400ms
■具有用户自定义的非易失性温度报警设置
■报名搜索命令识别并标识超过程序设定温度的器件
■超强静电保护能力:HBM 8000V MM 800V
■可提供贴片的MSOP8封装和3脚的TO-90封装
■应用包括温度控制、工业系统、消费品、粮情测温、温度计或任何感热系统

1.3 存储器介绍

        以前玩这个的时候都忽略这里了,就看时序图和数据帧了。

        DS18B20 内部有个 64 位只读存储器( ROM)和 64 位配置存储器( SCRATCHP)。64 位只读存储器( ROM)包含序列号等,具体格式如下图

        低八位用于 CRC 校验,中间 48 位是 DS18B20 唯一序列号,高八位是该系列产品系列号(固定为 28h)。因此,根据每个 DS18B20 唯一的序列号,可以实现一条总线上可以挂载多个 DS18B20 时,获取指定 DS18B20 的温度信息。

        64 位配置存储器( SCRATCHP)由 9 个 Byte 组成,包含温度数据、配置信息等,具体格式如下图:

⚫ Byte[0:1]:温度值。也就是当我们发出一个测量温度的命令之后,还需
要发送一个读内存的命令才能把温度值读取出来。
⚫ Byte[2:3]: TL 是低温阈值设置, TH 是高温阈值设置。当温度低于/超过
阈值,就会报警。 TL、 TH 存储在 EEPROM 中,数据在掉电时不会丢失;
⚫ Byte4:配置寄存器。用于配置温度精度为 9、 10、 11 或 12 位。配置寄存
器也存储在 EEPROM 中,数据在掉电时不会丢失;
⚫ Byte[5:7]:厂商预留;
⚫ Byte[8]: CRC 校验码。
 

1.4 时序

上面这些资料来源于这位老前辈的毕业设计翻译,翻译的挺好就是有点糊。

① ① 初始化时序
类似前面的 DHT11,主机要跟 DS18B20 通信,首先需要发出一个开始信号。
深黑色线表示由主机驱动信号,浅灰色线表示由 DS18B20 驱动信号。
最开始时引脚是高电平,想要开始传输信号,
a) 必须要拉低至少 480us,这是复位信号;
b) 然后拉高释放总线,等待 15~60us 之后,
c) 如果 GPIO 上连有 DS18B20 芯片,它会拉低 60~240us。
如果主机在最后检查到 60~240us 的低脉冲,则表示 DS18B20 初始化成功
② 写时序
⚫ 如果写 0,拉低至少 60us(写周期为 60-120us)即可;
⚫ 如果写 1,先拉低至少 1us,然后拉高,整个写周期至少为 60us 即可
③ 读时序
⚫ 主机先拉低至少 1us,随后读取电平,如果为 0,即读到的数据是 0,如果
为 1,即可读到的数据是 1。
⚫ 整个过程必须在 15us 内完成, 15us 后引脚都会被拉高
 

1.5 命令

(这块的图直接用韦东山老师的了,原来的老哥翻译的这个图片太马赛克了。但是韦老师的也不太好,后面有水印影响观感。)

1.5.1 命令大全   

     现在我们知道怎么发 1 位数据,收 1 位数据。发什么数据才能得到温度值,这需要用到“命令”。DS18B20 中有两类命令: ROM 命令、功能命令,列表如下:

1.5.2 命令使用

DS18B20 芯片手册中有 ROM 命令、功能命令的流程图,先贴出来,下一小节再举例。

1.5.3 使用示例

        总线上只一个 DS18B20 设备时,根据下表发送命令、读取数据。 因为只有一个 DS18B20,所以不需要选择设备,发出“ Skip ROM”命令。 然后发户“ ConvertT”命令启动温度转换;等待温度转换成功后,要读数据前,也要发出“ Skip ROM”命令。 下表列得很清楚:

        总线上有多个 DS18B20 设备时,根据下表发送命令、读取数据。 首先肯定是要选中指定设备:使用“ Match ROM”命令发出 ROM Code 来选择中设备; 然后发户“Convert T”命令启动温度转换; 等待温度转换成功后,要读数据前,也要发出“Match ROM”命令、 ROM Code。 下表列得很清楚:

1.6 原理图

二、 驱动程序

(今天破了一个案,dht11和ds18b20一点都不一样,以前一直没深入了解过ds18b20看来小觑它了)

#include "acpi/acoutput.h"
#include "asm-generic/errno-base.h"
#include "asm-generic/gpio.h"
#include "asm/gpio.h"
#include "asm/uaccess.h"
#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>

struct gpio_desc{
	int gpio;
	int irq;
    char *name;
    int key;
	struct timer_list key_timer;
} ;

static struct gpio_desc gpios[] = {
    {115, 0, "ds18b20", },
};

/* 主设备号                                                                 */
static int major = 0;
static struct class *gpio_class;

static spinlock_t ds18b20_spinlock;


static void ds18b20_udelay(int us)
{
	u64 time = ktime_get_ns();
	while (ktime_get_ns() - time < us*1000);
}

static int ds18b20_reset_and_wait_ack(void)
{
	int timeout = 100;

	gpio_set_value(gpios[0].gpio, 0);
	ds18b20_udelay(480);

	gpio_direction_input(gpios[0].gpio);

	/* 等待ACK */
	while (gpio_get_value(gpios[0].gpio) && timeout--)
	{
		ds18b20_udelay(1);
	}

	if (timeout == 0)
		return -EIO;

	/* 等待ACK结束 */
	timeout = 300;
	while (!gpio_get_value(gpios[0].gpio) && timeout--)
	{
		ds18b20_udelay(1);
	}
	if (timeout == 0)
		return -EIO;
	
	return 0;

}

static void ds18b20_send_cmd(unsigned char cmd)
{
	int i;
	
	gpio_direction_output(gpios[0].gpio, 1);

	for (i = 0; i < 8; i++)
	{
		if (cmd & (1<<i)) 
		{
			/* 发送1 */
			gpio_direction_output(gpios[0].gpio, 0);
			ds18b20_udelay(2);
			gpio_direction_output(gpios[0].gpio, 1);
			ds18b20_udelay(60);
		}
		else
		{
			/* 发送0 */
			gpio_direction_output(gpios[0].gpio, 0);
			ds18b20_udelay(60);		
			gpio_direction_output(gpios[0].gpio, 1);
		}
	}
}

static void ds18b20_read_data(unsigned char *buf)
{
	int i;
	unsigned char data = 0;

	gpio_direction_output(gpios[0].gpio, 1);
	for (i = 0; i < 8; i++)
	{
		gpio_direction_output(gpios[0].gpio, 0);
		ds18b20_udelay(2);
		gpio_direction_input(gpios[0].gpio);
		ds18b20_udelay(15);
		if (gpio_get_value(gpios[0].gpio))
		{
			data |= (1<<i);
		}
		ds18b20_udelay(50);
		gpio_direction_output(gpios[0].gpio, 1);
	}

	buf[0] = data;
}

/********************************************************/  
/*DS18B20的CRC8校验程序*/  
/********************************************************/   
/* 参考: https://www.cnblogs.com/yuanguanghui/p/12737740.html */   
static unsigned char calcrc_1byte(unsigned char abyte)   
{   
	unsigned char i,crc_1byte;     
	crc_1byte=0;                //设定crc_1byte初值为0  
	for(i = 0; i < 8; i++)   
	{   
		if(((crc_1byte^abyte)&0x01))   
		{   
			crc_1byte^=0x18;     
			crc_1byte>>=1;   
			crc_1byte|=0x80;   
		}         
		else     
			crc_1byte>>=1;   

		abyte>>=1;         
	}   
	return crc_1byte;   
}

/* 参考: https://www.cnblogs.com/yuanguanghui/p/12737740.html */   
static unsigned char calcrc_bytes(unsigned char *p,unsigned char len)  
{  
	unsigned char crc=0;  
	while(len--) //len为总共要校验的字节数  
	{  
		crc=calcrc_1byte(crc^*p++);  
	}  
	return crc;  //若最终返回的crc为0,则数据传输正确  
}  


static int ds18b20_verify_crc(unsigned char *buf)
{
    unsigned char crc;

	crc = calcrc_bytes(buf, 8);

    if (crc == buf[8])
		return 0;
	else
		return -1;
}

static void ds18b20_calc_val(unsigned char ds18b20_buf[], int result[])
{
	unsigned char tempL=0,tempH=0;
	unsigned int integer;
	unsigned char decimal1,decimal2,decimal;

	tempL = ds18b20_buf[0]; //读温度低8位
	tempH = ds18b20_buf[1]; //读温度高8位

	if (tempH > 0x7f)      							//最高位为1时温度是负
	{
		tempL    = ~tempL;         				    //补码转换,取反加一
		tempH    = ~tempH+1;      
		integer  = tempL/16+tempH*16;      			//整数部分
		decimal1 = (tempL&0x0f)*10/16; 			//小数第一位
		decimal2 = (tempL&0x0f)*100/16%10;			//小数第二位
		decimal  = decimal1*10+decimal2; 			//小数两位
	}
	else
	{
		integer  = tempL/16+tempH*16;      				//整数部分
		decimal1 = (tempL&0x0f)*10/16; 					//小数第一位
		decimal2 = (tempL&0x0f)*100/16%10;				//小数第二位
		decimal  = decimal1*10+decimal2; 				//小数两位
	}
	result[0] = integer;
	result[1] = decimal;
}

/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t ds18b20_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	unsigned long flags;
	int err;
	unsigned char kern_buf[9];
	int i;
	int result_buf[2];

	if (size != 8)
		return -EINVAL;

	/* 1. 启动温度转换 */
	/* 1.1 关中断 */
	spin_lock_irqsave(&ds18b20_spinlock, flags);

	/* 1.2 发出reset信号并等待回应 */
	err = ds18b20_reset_and_wait_ack();
	if (err)
	{
		spin_unlock_irqrestore(&ds18b20_spinlock, flags);
		printk("ds18b20_reset_and_wait_ack err\n");
		return err;
	}

	/* 1.3 发出命令: skip rom, 0xcc */
	ds18b20_send_cmd(0xcc);

	/* 1.4 发出命令: 启动温度转换, 0x44 */
	ds18b20_send_cmd(0x44);

	/* 1.5 恢复中断 */
	spin_unlock_irqrestore(&ds18b20_spinlock, flags);

	/* 2. 等待温度转换成功 : 可能长达1s */
	set_current_state(TASK_INTERRUPTIBLE);
	schedule_timeout(msecs_to_jiffies(1000));

	/* 3. 读取温度 */
	/* 3.1 关中断 */
	spin_lock_irqsave(&ds18b20_spinlock, flags);

	/* 3.2 发出reset信号并等待回应 */
	err = ds18b20_reset_and_wait_ack();
	if (err)
	{
		spin_unlock_irqrestore(&ds18b20_spinlock, flags);
		printk("ds18b20_reset_and_wait_ack second err\n");
		return err;
	}
	/* 3.3 发出命令: skip rom, 0xcc */
	ds18b20_send_cmd(0xcc);

	/* 3.4 发出命令: read scratchpad, 0xbe */
	ds18b20_send_cmd(0xbe);

	/* 3.5 读9字节数据 */
	for (i = 0; i < 9; i++)
	{
		ds18b20_read_data(&kern_buf[i]);
	}

	/* 3.6 恢复中断 */
	spin_unlock_irqrestore(&ds18b20_spinlock, flags);

	/* 3.7 计算CRC验证数据 */
	err = ds18b20_verify_crc(kern_buf);
	if (err)
	{
		printk("ds18b20_verify_crc err\n");
		return err;
	}

	/* 4. copy_to_user */
	ds18b20_calc_val(kern_buf, result_buf);
	
	err = copy_to_user(buf, result_buf, 8);
	return 8;
}


/* 定义自己的file_operations结构体                                              */
static struct file_operations gpio_key_drv = {
	.owner	 = THIS_MODULE,
	.read    = ds18b20_read,
};


/* 在入口函数 */
static int __init ds18b20_init(void)
{
    int err;
    int i;
    int count = sizeof(gpios)/sizeof(gpios[0]);
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	spin_lock_init(&ds18b20_spinlock);
	
	for (i = 0; i < count; i++)
	{		
		err = gpio_request(gpios[i].gpio, gpios[i].name);
		gpio_direction_output(gpios[i].gpio, 1);
	}

	/* 注册file_operations 	*/
	major = register_chrdev(0, "100ask_ds18b20", &gpio_key_drv);  /* /dev/gpio_desc */

	gpio_class = class_create(THIS_MODULE, "100ask_ds18b20_class");
	if (IS_ERR(gpio_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "100ask_ds18b20");
		return PTR_ERR(gpio_class);
	}

	device_create(gpio_class, NULL, MKDEV(major, 0), NULL, "myds18b20"); /* /dev/myds18b20 */
	
	return err;
}

/* 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 */
static void __exit ds18b20_exit(void)
{
    int i;
    int count = sizeof(gpios)/sizeof(gpios[0]);
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	device_destroy(gpio_class, MKDEV(major, 0));
	class_destroy(gpio_class);
	unregister_chrdev(major, "100ask_ds18b20");

	for (i = 0; i < count; i++)
	{
		gpio_free(gpios[i].gpio);
	}
}


/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(ds18b20_init);
module_exit(ds18b20_exit);

MODULE_LICENSE("GPL");


三、 应用程序


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
#include <signal.h>

static int fd;

/*
 * ./button_test /dev/myds18b20
 *
 */
int main(int argc, char **argv)
{
	int buf[2];
	int ret;

	int i;
	
	/* 1. 判断参数 */
	if (argc != 2) 
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}


	/* 2. 打开文件 */
	fd = open(argv[1], O_RDWR | O_NONBLOCK);
	if (fd == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	while (1) 
	{
		if (read(fd, buf, 8) == 8)
			printf("get ds18b20: %d.%d\n", buf[0], buf[1]);
		else
			printf("get ds18b20: -1\n");
		
		sleep(5);
	}
	
	close(fd);
	
	return 0;
}


四、 测试

注意一下这次设备的名字前面有个my剩下的就是老样子,暂时先直接用韦老师的后面我在按需修改。

 

  • 13
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基于STM8S的DS18B20驱动程序主要用于读取和控制DS18B20温度传感器。DS18B20是一种数字温度传感器,可以通过一条单线串行总线与微控制器进行通信。以下是一个简单的基于STM8S的DS18B20驱动程序的步骤: 1. 设置引脚:首先,需要设置一个引脚作为DS18B20的数据线,并在STM8S的寄存器中配置它为输出模式。例如,可以将引脚P1设置为输出模式。 2. 初始化传感器:在初始化阶段,需要将数据线置为高电平。通过发出一个复位脉冲,在数据线上拉低然后释放,可以使传感器进入初始化模式。 3. 发送命令:在读取或控制传感器之前,需要向传感器发送一些特定的命令。例如,可以发送读取温度值的命令。 4. 接收数据:在向传感器发送命令后,需要通过数据线接收传感器返回的数据。数据位以bit的方式传输,可以借助STM8S的GPIO功能逐位接收数据。 5. 数据处理:接收到传感器返回的数据后,需要进行相关的处理。DS18B20返回的温度值是以16位二进制补码的形式表示的。可以将其转换为实际温度值,并进行进一步的计算和分析。 6. 温度控制:如果需要控制传感器的工作状态,可以通过发送特定的命令实现。例如,可以发送写入配置寄存器的命令,以设置传感器的分辨率或工作模式。 这是一个基于STM8S的简单DS18B20驱动程序的步骤。根据实际需求,还可以添加其他功能和优化代码,以满足更复杂的应用场景。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇努力学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值