DS18B20 arm驱动程序
扫描二维码
随时随地手机看文章
此段代码没有ds18b20的id设置 ,只是实现了一个简单的温度输出。对ds18b20理解足够,重点在如何看到datesheet的时序图,以及功能的如何实现步骤。
#include //支持printk输出
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "DS18B20"
#define DS18B20_MAJOR 231
#define DQ S3C2410_GPG0
#define CLK S3C2410_GPB0
#define W1 S3C2410_GPH9
#define W2 S3C2410_GPE10
#define W3 S3C2410_GPH7
#define AB S3C2410_GPB4
#define OUT S3C2410_GPIO_OUTPUT
#define IN S3C2410_GPIO_INPUT
#define HIGH 1
#define LOW 0
//定义DS18B20ROM指令
#define DS18B20_ReadROM 0x33 //读ROM
#define DS18B20_MatchROM 0x55 //匹配ROM
#define DS18B20_SkipROM 0xCC //跳过ROM
#define DS18B20_SearchROM 0xF0 //搜索ROM
#define DS18B20_AlarmROM 0xEC //报警搜索
//定义DS18B20存储器操作命令
#define DS18B20_WriteSCR 0x4E //写暂存存储器
#define DS18B20_ReadSCR 0xBE //读暂存存储器
#define DS18B20_CopySCR 0x48 //复制暂存存储器
#define DS18B20_ConvertTemp 0x44 //温度变换
#define DS18B20_RecallEP 0xB8 //重新调出
#define DS18B20_ReadPower 0xB4 //读电源
static char devicecount=0; //计数器,防止被多个人多次打开
unsigned char DS18B20_Init(void)
{
s3c2410_gpio_cfgpin(DQ,OUT);
s3c2410_gpio_setpin(DQ,LOW);
udelay(700);
s3c2410_gpio_cfgpin(DQ,IN);
udelay(60);
udelay(240);
return 0;
}
unsigned char DS18B20_readbit(void)
{
unsigned char bit;
s3c2410_gpio_cfgpin(DQ,OUT);
s3c2410_gpio_setpin(DQ,LOW);
udelay(15);
s3c2410_gpio_cfgpin(DQ,IN);
bit=s3c2410_gpio_getpin(DQ);
udelay(45);
return bit;
}
unsigned char DS18B20_readbyte(void)
{
unsigned char i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=DS18B20_readbit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
void DS18B20_writebyte(unsigned char dat)
{
unsigned int i;
unsigned char j;
for(i=1;i<=8;i++)
{
j=dat&0x01;
dat=dat>>1;
if(j)
{
s3c2410_gpio_cfgpin(DQ,OUT);
s3c2410_gpio_setpin(DQ,LOW);
udelay(10);
s3c2410_gpio_setpin(DQ,HIGH);
udelay(45);
}
else
{
s3c2410_gpio_cfgpin(DQ,OUT);
s3c2410_gpio_setpin(DQ,LOW);
udelay(60);
s3c2410_gpio_setpin(DQ,HIGH);
}
}
}
unsigned short DS18B20_tmperature(void)
{
unsigned int a,b;
unsigned short tmp;
DS18B20_Init();
DS18B20_writebyte(DS18B20_SkipROM);
DS18B20_writebyte(DS18B20_ConvertTemp);
DS18B20_Init();
DS18B20_writebyte(DS18B20_SkipROM);
DS18B20_writebyte(DS18B20_ReadSCR);
a=DS18B20_readbyte();
b=DS18B20_readbyte();
tmp=b;
tmp=tmp<<8;
tmp=tmp|a;
return tmp;
}
void display(unsigned char dat)
{
unsigned char i;
for(i=1;i<=8;i++)
{
s3c2410_gpio_cfgpin(CLK,OUT);
s3c2410_gpio_setpin(CLK,LOW);
s3c2410_gpio_cfgpin(AB,OUT);
s3c2410_gpio_setpin(AB,dat&0x01);
s3c2410_gpio_setpin(CLK,HIGH);
dat>>=1;
}
udelay(700);
}
static int DS18B20_write(struct file *file, const char __user *buffer, size_t count, loff_t * ppos)
{
unsigned char i,a[3];
unsigned char table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴
unsigned char table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0x40};
copy_from_user(a,buffer,sizeof(a));
for(i=100;i--;i>0)
{
s3c2410_gpio_cfgpin(W1,OUT);
s3c2410_gpio_setpin(W1,LOW);
display(table[a[0]]);
s3c2410_gpio_cfgpin(W1,IN);
s3c2410_gpio_cfgpin(W2,OUT);
s3c2410_gpio_setpin(W2,LOW);
display(table1[a[1]]);
s3c2410_gpio_cfgpin(W2,IN);
s3c2410_gpio_cfgpin(W3,OUT);
s3c2410_gpio_setpin(W3,LOW);
display(table[a[2]]);
s3c2410_gpio_cfgpin(W3,IN);
}
return 0;
}
static int DS18B20_read(struct file *filp, char __user *buffer, size_t count, loff_t *ppos)
{
unsigned short tmp;
tmp=DS18B20_tmperature();
printk("tmperature = %d\n",tmp);
if(tmp<600)
copy_to_user(buffer,&tmp,sizeof(tmp));
return 0;
}
static int DS18B20_open(struct inode *inode,struct file *file)//3
{
if(devicecount >0) return -ERESTARTSYS;
printk("device open : success\n");
devicecount++;
return 0;
}
static int DS18B20_release(struct inode *inode, struct file *filp)//5
{
devicecount--;
printk("device release\n");
return 0;
}
static int DS18B20_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)//4
{
printk("*************This is the first driver----hello,welcome to embedded world *************\n");
return 0;
}
static struct file_operations DS18B20_fops = {//2_1
.owner = THIS_MODULE,
.ioctl = DS18B20_ioctl,
.write = DS18B20_write,
.read = DS18B20_read,
.open = DS18B20_open,
.release = DS18B20_release,
};
static int __init DS18B20_init(void)//2_0
{
int ret;
ret = register_chrdev(DS18B20_MAJOR, DEVICE_NAME, &DS18B20_fops);
if (ret < 0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit DS18B20_exit(void)//6
{
unregister_chrdev(DS18B20_MAJOR, DEVICE_NAME);
}
module_init(DS18B20_init); //1
module_exit(DS18B20_exit);//6
MODULE_AUTHOR("Yink_Liu");
MODULE_DESCRIPTION("A simple Hello World Moudle");
MODULE_ALIAS("a simple module");
MODULE_LICENSE("Dual BSD/GPL");