- UID
- 1029342
- 性别
- 男
|
/*读取数据*/
static int ds18b20_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
char lowValue=0,highValue=0;
unsigned int i;
//float value; if(reset_ds18b20()){
printk("init error\n");
}
__udelay(400);
set_conOUT();
set_data(1);
write_cmd(0xCC);
write_cmd(0x44);
__udelay(100000);
if(reset_ds18b20()){
printk("init error\n");
}
__udelay(400);
set_conOUT();
set_data(1);
write_cmd(0xcc);
write_cmd(0xBE);
/*读取温度转化数值*/
for(i=0; i<8; i++){
if( read_bit() ){
lowValue |= (0x01<<i);
}
__udelay(62);
}
printk("lowValue is %d\n",lowValue);
for(i=0; i<8; i++){
if( read_bit() ){
highValue |= (0x01<<i);
}
__udelay(62);
}
printk("highValue is %d\n",highValue);
#if 0
i = highValue;
i <<= 8;
i = i|lowValue;
value = i*0.0625;
printk("kernel is %d\n",value);
#endif
highValue <<= 4;
highValue |= ((lowValue&0xf0)>>4) ;
/*拷贝内核数据到用户空间*/
copy_to_user(buffer, &highValue, sizeof(highValue));
return 0;
}
/*写命令,在此置空*/
static int ds18b20_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
{
return 0;
}
static int ds18b20_release(struct inode *inode,struct file *filp)
{
printk (KERN_INFO "device closed\n");
return 0;
}
static int ds18b20_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
return 0;
}
struct file_operations ds18b20_fops ={
.owner = THIS_MODULE,
.open = ds18b20_open,
.read = ds18b20_read,
.write = ds18b20_write,
.ioctl = ds18b20_ioctl,
.release = ds18b20_release,
};
static void ds18b20_setup_cdev(void)
{
int error,devno = MKDEV(ds18b20_major,0);
cdev_init(&cdev,&ds18b20_fops);
cdev.owner = THIS_MODULE;
cdev.ops = &ds18b20_fops;
error = cdev_add(&cdev,devno,1);
if( error )
printk(KERN_INFO"Error %d adding ds18b20 %d\n",error,0);
my_class = class_create(THIS_MODULE,"my_class");
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return;
}
device_create(my_class,NULL,devno,NULL,"ds18b20");
} |
|