佛山信息技术网站开发,江苏省交通厅门户网站建设管理办法,广告公司网站建设方案,成都专业的网站建设制作公司哪家好目录
一、 模块介绍
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。 如果主机在最后检查到 60240us 的低脉冲则表示 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.hstruct 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 (1i)) {/* 发送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 | (1i);}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_1byte0; //设定crc_1byte初值为0 for(i 0; i 8; i) { if(((crc_1byte^abyte)0x01)) { crc_1byte^0x18; crc_1byte1; crc_1byte|0x80; } else crc_1byte1; abyte1; } return crc_1byte;
}/* 参考: https://www.cnblogs.com/yuanguanghui/p/12737740.html */
static unsigned char calcrc_bytes(unsigned char *p,unsigned char len)
{ unsigned char crc0; while(len--) //len为总共要校验的字节数 { crccalcrc_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;elsereturn -1;
}static void ds18b20_calc_val(unsigned char ds18b20_buf[], int result[])
{unsigned char tempL0,tempH0;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 ~tempH1; integer tempL/16tempH*16; //整数部分decimal1 (tempL0x0f)*10/16; //小数第一位decimal2 (tempL0x0f)*100/16%10; //小数第二位decimal decimal1*10decimal2; //小数两位}else{integer tempL/16tempH*16; //整数部分decimal1 (tempL0x0f)*10/16; //小数第一位decimal2 (tempL0x0f)*100/16%10; //小数第二位decimal decimal1*10decimal2; //小数两位}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.hstatic 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]);elseprintf(get ds18b20: -1\n);sleep(5);}close(fd);return 0;
} 四、 测试 注意一下这次设备的名字前面有个my剩下的就是老样子暂时先直接用韦老师的后面我在按需修改。