友善之臂Tiny6410 开发板下的DS18B20驱动程序,参考了http://hi.baidu.com/ppln/blog/item/7e3991f57c308067dcc47476.html的代码,在此表示感谢。
//驱动程序#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "Digital Thermometer DS18B20"
static char data[2];
static int reset(void)
{
gpio_direction_output(S3C64XX_GPN(8), 0);
gpio_set_value(S3C64XX_GPN(8),1);
udelay(100);
gpio_set_value(S3C64XX_GPN(8),0);
udelay(600);
gpio_set_value(S3C64XX_GPN(8),1);
udelay(100);
gpio_direction_output(S3C64XX_GPN(8), 1);
if(gpio_get_value(S3C64XX_GPN(8)))
{
// printk("kernel: DS18B20 initilize failed!\n");
return 0;
}
return 1;
}
void write_byte(unsigned char byte)
{
unsigned char i;
gpio_direction_output(S3C64XX_GPN(8), 0);
for(i=0; i<8; i++)
{
gpio_set_value(S3C64XX_GPN(8),0);
udelay(1);
if(byte & 0x01)
{
gpio_set_value(S3C64XX_GPN(8),1);
}
udelay(60);
gpio_set_value(S3C64XX_GPN(8),1);
udelay(15);
byte >>= 1;
}
gpio_set_value(S3C64XX_GPN(8),1);
}
unsigned char read_byte(void)
{
unsigned char i=0;
unsigned char byte=0;
for(i=0; i<8; i++)
{
gpio_direction_output(S3C64XX_GPN(8), 0);
gpio_set_value(S3C64XX_GPN(8),0);
udelay(1);
byte >>=1;
gpio_set_value(S3C64XX_GPN(8),1);
gpio_direction_output(S3C64XX_GPN(8), 1);
if(gpio_get_value(S3C64XX_GPN(8)))
byte |= 0x80;
udelay(60);
}
return byte;
}
static void proc(void)
{
while(reset());
udelay(120);
write_byte(0xcc);
write_byte(0x44);
udelay(5);
while(reset());
udelay(200);
write_byte(0xcc);
write_byte(0xbe);
data[0] = read_byte();
data[1] = read_byte();
}
static int ds18b20_open(struct inode *inode, struct file *file)
{
printk("open DS18B20.\n");
if(gpio_request(S3C64XX_GPN(8), "GPN") < 0)
{
return -1;
}
return 0;
}
static int ds18b20_close(struct inode *inode, struct file *file)
{
printk("close DS18B20.\n");
return 0;
}
static ssize_t ds18b20_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp)
{
return 0;
}
static int ds18b20_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
proc();
buff[0] = data[0];
buff[1] = data[1];
return 1;
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = ds18b20_open,
.release = ds18b20_close,
.read = ds18b20_read,
.write = ds18b20_write,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
ret = misc_register(&misc);
printk (DEVICE_NAME " initialized\n");
return ret;
}
static void __exit dev_exit(void)
{
printk("DS18B20 module exit.\n");
misc_deregister(&misc);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("centaur");
//测试程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int fd,t=0;
unsigned char buf[2]={0,0};
float result=0;
if ((fd=open("/dev/ds18b20",O_RDWR)) < 0)
{
printf("Open Device DS18B20 failed.\r\n");
exit(1);
}
while(1)
{
read(fd, buf, 1);
t=(int)buf[1];
t<<=8;
t=t|buf[0];
result=t*0.0625;
printf("%.1f \n", result);
sleep(1);
}
close(fd);
return 0;
}
注意事项:
操作GPIO前如果没有使用gpio_request(S3C64XX_GPN(8), "GPN")函数申请资源,系统会打印出如下信息。
阅读(4740) | 评论(0) | 转发(0) |