当前位置: 代码迷 >> 单片机 >> 小弟我的18B20驱动程序去测试,温度总是不变
  详细解决方案

小弟我的18B20驱动程序去测试,温度总是不变

热度:45   发布时间:2016-04-28 15:47:41.0
我的18B20驱动程序去测试,温度总是不变
C/C++ code
#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>MODULE_LICENSE("GPL");/*    起点工作室 18B20驱动程序    采用9位精度    时间75ms采样一次*/#define DEVICE_NAME "18B20-drv"#define GPHCON (unsigned long)(0x56000070)#define GPHDAT (unsigned long)(0x56000074)#define GPHUP (unsigned long)(0x56000078)typedef unsigned char BYTE;#define DS18B20_PIN S3C2410_GPH4#define DS18B20_PIN_OUTP S3C2410_GPH4_OUTP#define DS18B20_PIN_INP S3C2410_GPH4_INP#define HIGH 1#define LOW  0static BYTE data[2];//store 18B20rature high and lowstatic 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);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 nodestatic 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);        printk("/dev/%s%d register sucess\n",DEVICE_NAME,minor);    }        return 0;}static void temp_exit(void){    if(dev_class)    {        device_destroy(dev_class,MKDEV(major,minor));        class_destroy(dev_class);    }    unregister_chrdev(major,DEVICE_NAME);}module_init(temp_init);module_exit(temp_exit);////////////////////////////registerover//////////////////////////////////*  DS18B20 RESET*/static int temp_open(struct inode * pnode, struct file * pfile){    printk("18b20 open\n");        return 0;}static int temp_release(struct inode *pnode, struct file *pfile){    printk("18b20 release\n");    return 0;}int DS18B20_reset(void){    printk("hello reset\n");    s3c2410_gpio_cfgpin(DS18B20_PIN,DS18B20_PIN_OUTP);    s3c2410_gpio_setpin(DS18B20_PIN,1);    udelay(100);    s3c2410_gpio_setpin(DS18B20_PIN,0);    udelay(600);    s3c2410_gpio_setpin(DS18B20_PIN,1);    udelay(100);    s3c2410_gpio_cfgpin(DS18B20_PIN,DS18B20_PIN_INP);      if(s3c2410_gpio_getpin(DS18B20_PIN)==0)    {        printk("DS18B20 reset sucessfully\n");        return 0;    }    else    {        printk("reset failed and please try again\n");        return 1;    }}/*    DS18B20 WRITEBYTE*/    void DS18B20_WByte(BYTE cmd)    {        unsigned int i;        s3c2410_gpio_cfgpin(DS18B20_PIN,DS18B20_PIN_OUTP);        for(i=0;i<8;i++)        {            s3c2410_gpio_setpin(DS18B20_PIN,LOW);            udelay(1);            if(cmd & 0x01)            {                s3c2410_gpio_setpin(DS18B20_PIN,HIGH);            }            else            s3c2410_gpio_setpin(DS18B20_PIN,LOW);            udelay(60);                        s3c2410_gpio_setpin(DS18B20_PIN,HIGH);            udelay(15);            cmd=cmd>>1;        }        s3c2410_gpio_setpin(DS18B20_PIN,HIGH);    }/*    DS18B20 READBYTE*/BYTE DS18B20_RByte(void){    unsigned int i;    BYTE status=0;    for(i=0;i<8;i++)//九位精度末3位不用    {        s3c2410_gpio_cfgpin(DS18B20_PIN,DS18B20_PIN_OUTP);        s3c2410_gpio_setpin(DS18B20_PIN,LOW);        udelay(1);        status>>=1;        s3c2410_gpio_setpin(DS18B20_PIN,HIGH);        s3c2410_gpio_cfgpin(DS18B20_PIN,DS18B20_PIN_INP);                /*            if 1 us after LOW translate HIGH             read a 1        */        if(s3c2410_gpio_getpin(DS18B20_PIN))        status = status|0x80;        udelay(60);            }    printk("%x\n",status);    return status;}    /*   DS18B20 temp_init*/void DS18B20_start(void){    DS18B20_reset();    udelay(120);    DS18B20_WByte(0xcc);//write cmd    DS18B20_WByte(0x44);}/*    DS18B20 Read temp*/void DS18B20_Read(void){    DS18B20_reset();    mdelay(2);    DS18B20_WByte(0xcc);//write cmd    DS18B20_WByte(0x44);    data[0] = DS18B20_RByte();    data[1] = DS18B20_RByte();}    /*    DS18B20 PRO*/void DS18B20_PRO(void){    DS18B20_start();    udelay(5);    DS18B20_Read();}static ssize_t temp_read(struct file *file, char *buf, size_t len, loff_t *off){   DS18B20_PRO();    buf[0] = data[0];    buf[1] = data[1];    return 0;}