write_ds18b20(0xbe); //读取RAM
result[0] = read_ds18b20(); //读低8位,存放在result[0]
result[1] = read_ds18b20(); //读高8位,存放在result[1]
ds18b20_reset();
err = copy_to_user(buf, &result, sizeof(result));
return err ? -EFAULT : min(sizeof(result), size);
}
static int ds18b20_release(struct inode *inode,struct file *filp)
{
return 0;
}
static const struct file_operations ds18b20_fops={
.owner=THIS_MODULE,
.read=ds18b20_read,
.open=ds18b20_open,
.release=ds18b20_release,
};
void ds18b20_setup_dev(struct ds18b20_dev *dev,int minor)
{
int err;
int devno;
devno = MKDEV(ds18b20_major,minor);
cdev_init(&dev->cdev,&ds18b20_fops);
dev->cdev.owner=THIS_MODULE;
dev->cdev.ops=&ds18b20_fops;
err=cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE 'Error %d adding %dn',err,minor);
}
static int __init ds18b20_init(void)
{
int result;
dev_t devno = 0;
if(ds18b20_major){
devno = MKDEV(ds18b20_major,ds18b20_minor);
result = register_chrdev_region(devno,1,DEV_NAME);
}
else{
result = alloc_chrdev_region(&devno,0,1,DEV_NAME);
ds18b20_major = MAJOR(devno);
}
if(result < 0)
return result;
ds18b20_devp = kmalloc(sizeof(struct ds18b20_dev),GFP_KERNEL);
if(ds18b20_devp){
result = -ENOMEM;
goto fail_malloc;
}
memset(ds18b20_devp,0,sizeof(struct ds18b20_dev));
ds18b20_setup_dev(ds18b20_devp,0);
my_class = class_create(THIS_MODULE,'ds18b20_class');
/*在linux 2.6.27之前是:class_device_create*/
my_device = device_create(my_class,NULL,MKDEV(ds18b20_major, ds18b20_minor),NULL,'ds18b20');
return 0;
fail_malloc:
unregister_chrdev_region(devno,1);
return result;
}
static void __exit ds18b20_exit(void)
{
cdev_del(&ds18b20_devp->cdev);
kfree(ds18b20_devp);
device_destroy(my_class, MKDEV(ds18b20_major, ds18b20_minor));//删除设备文件 /dev/ds18b20
class_destroy(my_class); //删除创建的bus
unregister_chrdev_region(MKDEV(ds18b20_major,ds18b20_minor),1);
}
module_init(ds18b20_init);
module_exit(ds18b20_exit);
MODULE_LICENSE('GPL');
MODULE_AUTHOR('WIOT');
应用程序代码:
#include
#include
#include
#include
void ds18b20_delay(int i)
{
int j, k;
for (j = 0; j < i; j++)
for(k = 0; k < 50000; k++);
}
int main(int argc,char **argv)
{
int fd, i;
unsigned char result[2];
unsigned int Temp;
unsigned char Temperature[7]; //显示温度用的buffer
fd = open('/dev/ds18b20',0);
if(fd < 0){
perror('open device failed!n');
exit(1);
}
while(1){
read(fd,&result,sizeof(result));
Temp = ((result[1]<<8)|result[0])*6.25; //这个是为了让温度后面有两位小数
Temperature[0] = Temp/1000+48;
Temperature[1] = Temp%1000/100+48;
Temperature[2] = '.';
Temperature[3] = Temp%100/10+48;
Temperature[4] = Temp%10+48;
Temperature[5] = ' ';
Temperature[6] = 'C';
printf('The temperature is:%sn',Temperature);
ds18b20_delay(1000);
}
return 0;
}
驱动放弃了好长时间了啊,要捡起来,并且深入。以后还是要坚定自己的选择。既然对这个感兴趣,就要经常的去折腾。
上一篇:mini2440 编译U-Boot-1.1.6遇到的一些问题
下一篇:U-Boot-2011.03移植nandflash到mini2440
推荐阅读最新更新时间:2024-11-02 05:13
设计资源 培训 开发板 精华推荐
- ER2120QI 2A多从属通道同步降压稳压器典型应用电路
- ADR392B 4.096 Vout 微功率、低噪声精密电压基准的典型应用
- 串口自动下载
- 使用 ROHM Semiconductor 的 BU4811 的参考设计
- AIS2DW12适配器板,用于标准DIL 24插座
- EVAL-ADM3055EEBZ,用于评估 ADM3055E 5kV rms、信号和电源隔离、CAN FD CAN 收发器的评估板
- EVAL-AD7467CB,用于数据采集系统的 AD7467、10 位、275KSPS ADC 的评估板
- EVAL-AD7621CBZ,用于 AD7621、16 位、3 Msps PulSAR 模数转换器的评估板
- 用于 Intel 486TM DX4TM Overdrive 微处理器的 LT1587CT-3.3 3.3V/3A 低压差稳压器的典型应用
- NCV2002SN1T1G高端电流检测典型应用电路