驱动代码:

#include    <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <plat/gpio-cfg.h> #define DEVICE_NAME "TEM0" #define TEM_SHOW_CODE 0x01 //static struct cdev cdev;
struct class *tem_class;
//static dev_t devno;
//static int major = 243;//可以用int alloc_chrdev_region(dev_t *dev,unsigned baseminor,
//unsigned count,const char *name);向系统动态申请未被占用的设备号。 struct tem_dev_t
{
struct cdev cdev;
}tem_dev; /**
* * s3c_gpio_cfgpin() - Change the GPIO function of a pin.
* * @pin pin The pin number to configure.
* * @to to The configuration for the pin's function.
* *
* * Configure which function is actually connected to the external
* * pin, such as an gpio input, output or some form of special function
* * connected to an internal peripheral block.
* *
* * The @to parameter can be one of the generic S3C_GPIO_INPUT, S3C_GPIO_OUTPUT
* * or S3C_GPIO_SFN() to indicate one of the possible values that the helper
* * will then generate the correct bit mask and shift for the configuration.
* *
* * If a bank of GPIOs all needs to be set to special-function 2, then
* * the following code will work:
* *
* * for (gpio = start; gpio < end; gpio++)
* * s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN(2));
* *
* * The @to parameter can also be a specific value already shifted to the
* * correct position in the control register, although these are discouraged
* * in newer kernels and are only being kept for compatibility.
* */
unsigned int gpio=; void tem_reset(void)
{
s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN());//当x=0时是输入功能,x=1时是输出功能
gpio_set_value(gpio, );
udelay();//延迟0.1ms
gpio_set_value(gpio, );
udelay();
gpio_set_value(gpio, );
udelay();
s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN());
} void tem_wbyte(unsigned char data)
{
int i; s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN());
for (i = ; i < ; ++i)
{
gpio_set_value(gpio, );
udelay(); if (data & 0x01)
{
gpio_set_value(gpio, );
}
udelay();
gpio_set_value(gpio, );
udelay();
data >>= ;
}
gpio_set_value(gpio, );
} unsigned char tem_rbyte(void)
{
int i;
unsigned char ret = ; for (i = ; i < ; ++i)
{
s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN());
gpio_set_value(gpio, );
udelay();
gpio_set_value(gpio, ); s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN());
ret >>= ;
if (gpio_get_value(gpio))
{
ret |= 0x80;
}
udelay();
}
s3c_gpio_cfgpin(gpio, S3C_GPIO_SFN()); return ret;
} static ssize_t tem_read(struct file *filp, char *buf, size_t len, loff_t *offset)
{
unsigned char low, high; tem_reset();
udelay();
tem_wbyte(0xcc);
tem_wbyte(0x44); mdelay();
tem_reset();
udelay();
tem_wbyte(0xcc);
tem_wbyte(0xbe); low = tem_rbyte();
high = tem_rbyte(); *buf = low / + high * ; *(buf + ) = (low & 0x0f) * / + (high & 0x0f) * / % ;
return ;
}
/*******************************************************************
*
*ioctl 还不准确,需要改进
* by mhb
*
**********************************************************************/
int tem_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
printk("in the ioctl!!\n");//debug
switch(cmd)
{
case TEM_SHOW_CODE:
{
int i;
unsigned char fc,sn[],crc;
tem_reset();
udelay();
tem_wbyte(0x33);
fc = tem_rbyte;
for (i = ; i < ; i++)
{
/* code */
sn[i]=tem_rbyte;
}
crc=tem_rbyte;
printk("familycode=%x\n",fc);
printk("serialnumber=%x%x%x%x%x%x\n",sn[],sn[],sn[],sn[],sn[],sn[]);
printk("crc=%x\n",crc);
break;
}
default:
{
return - ENOTTY;
}
}
} static struct file_operations tem_fops =
{
.owner = THIS_MODULE,
.read = tem_read,
.ioctl =tem_ioctl,
}; static int __init tem_init(void)
{
int result;
int major;
int minor; cdev_init(&tem_dev.cdev,&tem_fops);/*init cdev*/
tem_dev.cdev.owner=THIS_MODULE;
// devno = MKDEV(major, 0);
// result = register_chrdev_region(devno, 1, DEVICE_NAME);
result = alloc_chrdev_region(&tem_dev.cdev.dev,,,DEVICE_NAME);
major=MAJOR(tem_dev.cdev.dev);
minor=MINOR(tem_dev.cdev.dev);
if(major)
{
printk("majior=%d\n",major);
printk("minor=%d\n",minor);
}else
printk("can not get device num!!\n"); if (result)
{
printk("register failed\n");
return result;
}
#ifdef CONFIG_OK210_BOARD_V2
gpio=S5PV210_MP04();
#else
gpio=S5PV210_GPH3();
#endif
// cdev_init(&cdev, &tem_fops);
// cdev.owner = THIS_MODULE;
// cdev.ops = &tem_fops;
result = cdev_add(&tem_dev.cdev, tem_dev.cdev.dev, );
if (result)
{
printk("cdev add failed\n");
goto fail1;
} tem_class = class_create(THIS_MODULE, "tmp_class");/*在sys目录下创建tmp_class这个类,/sys/class/~*/
if (IS_ERR(tem_class))
{
printk("class create failed\n");
goto fail2;
} device_create(tem_class, NULL, tem_dev.cdev.dev, DEVICE_NAME, DEVICE_NAME);/*自动创建设备/dev/TEM0*/
return ;
fail2:
cdev_del(&tem_dev.cdev);
fail1:
unregister_chrdev_region(tem_dev.cdev.dev, );
return result;
} static void __exit tem_exit(void)
{
device_destroy(tem_class, tem_dev.cdev.dev);
class_destroy(tem_class);
cdev_del(&tem_dev.cdev);
unregister_chrdev_region(tem_dev.cdev.dev, );
} module_init(tem_init);
module_exit(tem_exit); MODULE_LICENSE("GPL");
MODULE_AUTHOR("mhb");

测试代码:

#include "stdio.h"
#include "sys/types.h"
#include "sys/ioctl.h"
#include "stdlib.h"
#include "termios.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "sys/time.h"
#define TEM_SHOW 0x01
main()
{
int fd;
#if 1
unsigned char buf[];
#else
unsigned int tmp;
#endif
float result; if ((fd=open("/dev/TEM0",O_RDWR | O_NDELAY | O_NOCTTY)) < )
{
printf("Open Device DS18B20 failed.\r\n");
exit();
}
else
{
printf("Open Device DS18B20 successed.\r\n");
while()
{
// read(fd, buf, sizeof(buf));
// result = tmp * 1000;
// result = *(int *)buf;
read(fd, buf, sizeof(buf));
#if 0
printf("%d\n", buf[]);
printf("%d\n", buf[]);
result = (float)buf[];
result /= ;
result += ((float)buf[] * );
#endif
#if 0
// printf("%f .C\r\n", result);
sleep();
#else
printf("%d.%d C\r\n", buf[], buf[]);
sleep();
ioctl(fd,TEM_SHOW,);
#endif
}
close(fd);
}
}

Linux下18b20温度传感器驱动代码及测试实例的更多相关文章

  1. Linux下简易蜂鸣器驱动代码及测试实例

    驱动代码: #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> ...

  2. 怎样在Windows和Linux下写相同的代码

    目前,Linux在国内受到了越来越多的业内人士和用户的青睐.相信在不久的将来,在国内为Linux开发 的应用软件将会有很大的增加(这不,金山正在招兵买马移植WPS呢).由于未来将会是Windows和L ...

  3. Linux下几种常见压缩方式测试对比

    目录 Linux下几种常见压缩方式测试对比 参考 简介 测试 总结 Linux下几种常见压缩方式测试对比

  4. Linux下四款Web服务器压力测试工具(http_load、webbench、ab、siege)介绍

    一.http_load程序非常小,解压后也不到100Khttp_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般不会把 ...

  5. Linux下高并发socket链接数测试

    一.如何增大service进程的max open files ulimit -n 只能改小max open files,不能改大.需要按照以下步骤: 修改/etc/security/limits.co ...

  6. 介绍linux下Source Insight强大代码编辑器sublime_text_3

    背景 1 一. 运行环境 1 二.安装环境配置 1 三.创建快捷方式 1 四.配置全局环境 2 五.操作界面 3 背景 在windows操作系统系统下,文本代码编辑器众多,各路英雄豪杰争相写了许多强大 ...

  7. linux下使用indent整理代码(代码格式化)【转】

    转自:https://blog.csdn.net/jiangjingui2011/article/details/7197069 常用的设置: indent -npro -kr -i8 -ts8 -s ...

  8. linux下重启oracle服务:监听器和实例

    一.在Linux下重启Oracle数据库及监听器: 方法1: 用root以ssh登录到linux,打开终端输入以下命令: cd $ORACLE_HOME #进入到oracle的安装目录 dbstart ...

  9. [转载]以及部分总结--Linux下创建单机ASM存储的Oracle实例的过程---感谢方总

    Linux下单机安装ASM流程总结 一.安装Linux ESXi上传iso镜像至存储目录 创建虚拟机,并且选择主机设备的ISO启动 选择完成时编辑虚拟机设置 配置镜像文件如下: 打开控制台: 并且选择 ...

随机推荐

  1. com.code.servlet

    package com.code.servlet; import java.io.IOException; import java.util.LinkedHashMap; import java.ut ...

  2. 【转】java静态代码块和构造方法执行顺序

    先看看下面几个类,然后判断它们的输出public class A { static{System.out.print(1);}public A(){System.out.print(2);}} pub ...

  3. C# 之 遍历本地文件夹下的所有文件

    /// <summary> /// 遍历 rootdir目录下的所有文件 /// </summary> /// <param name="rootdir&quo ...

  4. mysql语句在客户端与服务端的基本使用

    //把数据库导出到脚本文件mysqldump -uroot -p1234 --databases abc > d:/a/abc.sql------------------------------ ...

  5. MAC上安装Homebrew、Nginx、PHP、MySQL

    准备工作 新版的 Mac OS 内置了Apache 和 PHP,可以通过以下命令查看Apache和PHP的版本号: # httpd -v # php -v 安装Homebrew Mac下的Homebr ...

  6. spring的依赖注入的最常见的两种方法

    package com.lsz.spring.action; public class User { /** * set注入 */ private String username; public vo ...

  7. 【转】Oracle - 数据库的实例、表空间、用户、表之间关系

    [转]Oracle - 数据库的实例.表空间.用户.表之间关系 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机 ...

  8. JDBC与ODBC的区别与应用

    jdbc是使用通过JAVA的数据库驱动直接和数据库相连,而jdbc-odbc连接的是ODBC的数据源,真正与数据库建立连接的是ODBC! 建议使用JDBC直接连接,同时最好使用连接池! JDBC 是 ...

  9. 【MINA】粘包断包处理

    1.先解释下什么叫粘包和断包 粘包 就是数据以字节的形式在网络中传输,一个数据包的字节可能经过多次的读取粘合才能形成一个完整的数据包 断包 一次读取的内容可能包含了两个或多个数据包的内容,那么我们必须 ...

  10. 【转】prototype扩展的JavaScript常用函数库

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...