驱动代码:

#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. [Effective C++ --008]别让异常逃离析构函数

    这章非常容易理解:因为C++并不禁止析构函数吐出异常,只是不鼓励这样做而已. 一.原因 假设我们有10个装着鸡蛋的容器,而且现在我们还想着把它在析构函数打烂. class Egg { public : ...

  2. dd,实现系统备份

    一.dd的使用 dd命令的解释dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾则乘以相应的数字:b=512:c=1:k=1024:w=2 参数: ...

  3. Ubuntu10.04下载并编译Android4.3源代码

    注:转载或引用请标明出处    http://blog.csdn.net/luzhenrong45/article/details/9719433 去年用Ubuntu10.10成功下载并编译Andro ...

  4. Hibernate学习笔记--------3.缓存

    一.一级缓存 一级缓存又称为“Session缓存”或者“会话级缓存”,通过Session从数据库查询实体时,会把实体在内存中存储起来,下一次查询同一实体时不再再次执行sql语句查询数据库,而是从内存中 ...

  5. httpd.conf配置解析php

    PHPIniDir "D:/php-5.3.5" LoadModule php5_module "D:/php-5.3.5/php5apache2_2.dll" ...

  6. [转载]删除所有的.svn文件夹

    Windows 下,在DOS窗口中运行如下命令 dos 代码 for /r <你项目的路径> %i in (.svn) do rd /s /q %i Linux 下,可以先运行 显示出当前 ...

  7. android显示手机电量

    package com.basillee.asus.demo; import android.app.Notification; import android.content.BroadcastRec ...

  8. 对list集合中的对象进行排序(转载)

    原文链接:http://blog.csdn.net/veryisjava/article/details/51675036 Collections对List集合中的数据进行排序 有时候需要对集合中的元 ...

  9. Cordova+angularjs+ionic+vs2015开发(一)

    欢迎加群学习:457351423 这里有4000多部学习视频,涵盖各种技术,有需要的欢迎进群学习! 一.Windows环境配置 1.如果已经安装VS2015,打开[工具]-[选项]找到Cordova选 ...

  10. ubuntu下aircrack-ng的wifi破解

    首先安装aircrack-ng,apt-get install aircrack-ng. 然后打开shell,输入airmon-ng start wlan0. 输入airodump-ng mon0. ...