⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18b20linux驱动程序 .txt

📁 嵌入式linux下的18B20数字温度传感器的驱动程序。
💻 TXT
字号:
   1. #include <linux config.h="">  
   2. #include <linux module.h="">  
   3. #include <linux kernel.h="">  
   4. #include <linux fs.h="">  
   5. #include <linux init.h="">  
   6. #include <linux devfs_fs_kernel.h="">  
   7. #include <linux miscdevice.h="">  
   8. #include <linux delay.h="">  
   9. #include <asm irq.h="">  
  10. #include <asm arch="" regs-gpio.h="">  
  11. #include <asm hardware.h="">  
  12. #define DEVICE_NAME "18b20"  
  13. #define tp_MAJOR  232  
  14.   
  15. unsigned char sdata;//测量到的温度的整数部分  
  16. unsigned char xiaoshu1;//小数第一位  
  17. unsigned char xiaoshu2;//小数第二位  
  18. unsigned char xiaoshu;//两位小数  
  19.   
  20.   
  21. void tmreset (void)       //18b20初始化  
  22. {        
  23.        s3c2410_gpio_cfgpin(S3C2410_GPG0, S3C2410_GPG0_OUTP);  
  24.     s3c2410_gpio_setpin(S3C2410_GPG0, 1);  
  25.        udelay(100);  
  26.   
  27.     s3c2410_gpio_setpin(S3C2410_GPG0, 0);     
  28.        udelay(600);  
  29.        
  30.     s3c2410_gpio_setpin(S3C2410_GPG0, 1);  
  31.        udelay(100);  
  32.   
  33.     s3c2410_gpio_cfgpin(S3C2410_GPG0, S3C2410_GPG0_INP);     //设置寄存器对18b20进行读操作  
  34.       
  35.     if(s3c2410_gpio_getpin(S3C2410_GPG0)==0)  
  36.         printk("18b20 initialization is sucessful\n");  
  37.        
  38. }    
  39.   
  40. void tmwbyte (unsigned char dat)     //写一个字节函数  
  41. {                         
  42.         unsigned char j;  
  43.      s3c2410_gpio_cfgpin(S3C2410_GPG0, S3C2410_GPG0_OUTP);     
  44.      for (j=1;j<=8;j++)        
  45.      {   
  46.        s3c2410_gpio_setpin(S3C2410_GPG0, 0);   
  47.        udelay(1);   
  48.        if((dat&0x01)==1)  
  49.         {  
  50.            s3c2410_gpio_setpin(S3C2410_GPG0, 1);            
  51.         }       
  52.        else   
  53.         {  
  54.           // s3c2410_gpio_setpin(S3C2410_GPG0, 0);  
  55.         }  
  56.        udelay(60);  
  57.        s3c2410_gpio_setpin(S3C2410_GPG0, 1);  
  58.        udelay(15);  
  59.        dat = dat >> 1;  
  60.         }    
  61.         s3c2410_gpio_setpin(S3C2410_GPG0, 1);  
  62. }   
  63.   
  64. unsigned char tmrbyte (void)        //读一个字节函数  
  65.   {                  
  66.      unsigned char i,u=0;        
  67.              
  68.      for (i=1;i<=8;i++)        
  69.      {  
  70.         
  71.       s3c2410_gpio_cfgpin(S3C2410_GPG0, S3C2410_GPG0_OUTP);   
  72.       s3c2410_gpio_setpin(S3C2410_GPG0, 0);   
  73.       udelay(1);  
  74.        u >>= 1;   
  75.       s3c2410_gpio_setpin(S3C2410_GPG0, 1);  
  76.       //udelay(12);  
  77.       s3c2410_gpio_cfgpin(S3C2410_GPG0, S3C2410_GPG0_INP);   
  78.       if( s3c2410_gpio_getpin(S3C2410_GPG0))    u=u|0x80;  
  79.       udelay(60);    
  80.      }    
  81.      return (u);         
  82.         
  83. }  
  84.   
  85. void DS18B20PRO(void)           
  86. {      
  87.   unsigned char a,b;   
  88.   tmreset();             //复位   
  89.   udelay(120);  
  90.   tmwbyte(0xcc);         //跳过序列号命令     
  91.   tmwbyte(0x44);         //发转换命令 44H,  
  92.   udelay(5);  
  93.   tmreset ();      //复位   
  94.   udelay(200);  
  95.   tmwbyte (0xcc);  //跳过序列号命令     
  96.   tmwbyte (0xbe);  //发送读取命令  
  97.   a = tmrbyte ();  //读取低位温度      
  98.   b= tmrbyte ();  //读取高位温度             
  99.     sdata = a/16+b*16;      //整数部分    
 100.     xiaoshu1 = (a&0x0f)*10/16;    //小数第一位  
 101.     xiaoshu2 = (b&0x0f)*100/16%10;//小数第二位  
 102.     xiaoshu=xiaoshu1*10+xiaoshu2; //小数两位  
 103. }     
 104.   
 105. static ssize_t  s3c2440_18b20_read(struct file *filp, char *buf, size_t len, loff_t *off)  
 106.   
 107. {  
 108.    DS18B20PRO();  
 109.     *buf=sdata;  
 110.     //printk("%d\n",xiaoshu);  
 111.     //printk("%d\n",sdata);  
 112.     return 1;  
 113. }  
 114.   
 115. static struct file_operations s3c2440_18b20_fops = {  
 116.     .owner  =   THIS_MODULE,  
 117.     .read   =   s3c2440_18b20_read,  
 118. };  
 119.   
 120. static int __init s3c2440_18b20_init(void)  
 121. {  
 122.     int ret;  
 123.   
 124.     ret = register_chrdev(tp_MAJOR, DEVICE_NAME, &s3c2440_18b20_fops);  
 125.     if (ret < 0) {  
 126.       printk(DEVICE_NAME " can't register major number\n");  
 127.       return ret;  
 128.     }  
 129.   
 130.     devfs_mk_cdev(MKDEV(tp_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);  
 131.        //s3c2410_gpio_pullup(S3C2410_GPG0, 0);  
 132.      tmreset();     
 133.       
 134. }  
 135.   
 136. static void __exit s3c2440_18b20_exit(void)  
 137. {  
 138.     devfs_remove(DEVICE_NAME);  
 139.     unregister_chrdev(tp_MAJOR, DEVICE_NAME);  
 140. }  
 141.   
 142.   
 143. module_init(s3c2440_18b20_init);  
 144. module_exit(s3c2440_18b20_exit);  
 145.   
 146. </asm></asm></asm></linux></linux></linux></linux></linux></linux></linux></linux>  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -