首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > 单片机 >

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

2012-09-23 
我的18B20驱动程序去测试,温度总是不变C/C++ code#includelinux/init.h#includelinux/kernel.h#includ

我的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;} 



我的测试程序为

C/C++ code
#include<stdio.h>#include"sys/types.h"#include"sys/ioctl.h"#include<stdlib.h>#include<sys/stat.h>#include<fcntl.h>#include<sys/time.h>#include<termios.h>int main(void){    int fd;    double result=0;    unsigned char buff[2];    unsigned short int temp=0;    fd = open("/dev/18B20-drv0",O_RDWR|O_NDELAY|O_NOCTTY);    if(fd<0)    {        printf("open failed\n");        return 1;    }    else    {        while(1)        {            read(fd,buff,1);            printf("high is %x  low is %x\n",buff[1],buff[0]);            temp = (unsigned short)buff[1]<<8;            temp|= (unsigned short)buff[0];            printf("connected sucessfully\n");            if((buff[0]<0xf8))            result = 0.0625*((double)temp);            else            {                temp=~temp+1;                result = 0.0625*((double)temp);            }                            printf("the temprature is %f\n",result);            sleep(1);        }        close(fd);        return 0;    }}


但是结果总是buf[0]和buf[1]都为ff,并且reset也不成功

[解决办法]
用排除来解决,先用正确的程序代码一点一点去试,而不是用你的代码去试
[解决办法]
void DS18B20_Read(void)
{
DS18B20_reset();
mdelay(2);
DS18B20_WByte(0xcc);//write cmd
DS18B20_WByte(0x44);
data[0] = DS18B20_RByte();
data[1] = DS18B20_RByte();
}
应该是这里的问题,写了转换命令了后要等待一定的时间再读取温度。具体你看数据手册,好像是720ms。

热点排行