最近在用18B20想测温度,但是纠结了好多天,温度总是0,8,16,2000多这样乱跳,大多还是0,时序我也是按照18B20手册的,但是还是不对。还有一个不懂的是释放总线这个概念,有些地方说是直接给总线置1,有些是把IO改为输入模式,我试了下用置1的方法不成功。 下面是我的代码:::
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<asm/uaccess.h>
#include<asm/io.h>
#include<linux/device.h>
#include<asm/gpio.h>
#include<linux/poll.h>
#include<linux/interrupt.h>
#include<linux/irq.h>
#include<linux/time.h>
#include<linux/delay.h>
#include<asm/hardware.h>
#include <asm-arm/arch-s3c2410/regs-gpio.h>
MODULE_LICENSE("GPL");
#define DEVICE_NAME "18B20-drv"
#define GPJCON (unsigned long)(0x560000D0)
#define GPJDAT (unsigned long)(0x560000D4)
#define GPJUP (unsigned long) (0x560000D8)
typedef char BYTE;
static volatile unsigned long* _io_gpjcon;
static volatile unsigned long* _io_gpjdat;
static volatile unsigned long* _io_gpjup;
static BYTE data[2];//store 18B20rature 1 and 0
static int major=0;
static int minor=0;
static int temp_open(struct inode *, struct file *);
static int temp_release(struct inode *, struct file *);
static ssize_t temp_read(struct file *, char *, size_t, loff_t *);
//static ssize_t temp_write(struct file *, const char *, size_t, loff_t *);
//static int temp_ioctl(struct inode *, struct file *, unsigned int cmd, unsigned long arg);
void DQ_OUTP(void);
void DQ_INTP(void);
void set_DQ(unsigned int);
unsigned long get_DQ(void);
int DQ_reset(void);
unsigned char DQ_RBYTE(void);
int DQ_PRO(void);
static struct file_operations fops =
{
.owner = THIS_MODULE,
.open = temp_open,
.release = temp_release,
.read = temp_read,
// .write = temp_write,
//.ioctl = temp_ioctl,
};
static struct class *dev_class;//use this to create device node
static int temp_init(void)
{
major = register_chrdev(0,DEVICE_NAME,&fops);
if(major < 0)
{
printk("register %s error\n",DEVICE_NAME);
return 1;
}
printk("DEV Number:%d,%d\n",major,minor);
dev_class = class_create(THIS_MODULE,DEVICE_NAME);
if(dev_class == NULL)
{
printk("18B20 node create error\n");
printk("TODO:mknod /dev/%s c %d %d\n",DEVICE_NAME,major,minor);
}
else
{
device_create(dev_class,NULL,MKDEV(major,minor),"%s%d",DEVICE_NAME,minor);