主题 : 刚写了个DS18B20的驱动,很简陋,很粗糙,且很多测试语句,大家凑合着用吧 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 51292
精华: 1
发帖: 21
金钱: 155 两
威望: 31 点
贡献值: 1 点
综合积分: 62 分
注册时间: 2011-07-01
最后登录: 2011-08-18
楼主  发表于: 2011-07-21 11:08

 刚写了个DS18B20的驱动,很简陋,很粗糙,且很多测试语句,大家凑合着用吧

管理提醒: 本帖被 xoom 执行加亮操作(2011-08-05)
RT,非常粗糙简陋,或许对大家有那么一点价值,凑合用吧
 
1、把GPIODriver解压成一个文件夹,放到 C:\WINCE600\PLATFORM\SMDK6410\SRC\DRIVERS\   的文件夹下
 
2、在C:\WINCE600\PLATFORM\SMDK6410\FILES\platform.reg 文件的末尾,加上以下:
 
   [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\GPIOdrivers]
   "Prefix"="GIO"
   "Dll"="GPIOdriver.dll"
   "Order"="201"
 
3、在C:\WINCE600\PLATFORM\SMDK6410\FILES\platform.bib 文件末尾,加上以下:
 
     gpiodriver.dll  $(_FLATRELEASEDIR)\gpiodriver.dll NK SHK
 
4、写应用测试程序的时候,API定义如下(以VS2008举例)(参数类型也可以对照着GPIODriver.cpp中的相关函数,可以自己修改)
        [DllImport("coredll.dll")]
        private static extern int CreateFile(
                string lpFileName,
                uint dwDesiredAccess,
                int dwShareMode,
                int lpSecurityAttributes,
                int dwCreationDisposition,
                int dwFlagsAndAttributes,
                int hTemplateFile
                );
        [DllImport("coredll.dll")]
        private static extern bool CloseHandle(
                int hObject   // handle to object
                );
        [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
        internal static extern bool DeviceIoControl(
                    int hDevice,
                    int dwIoControlCode,
                    byte[] lpInBuffer,
                    int nInBufferSize,
                    byte[] lpOutBuffer,
                    int nOutBufferSize,
                    IntPtr lpBytesReturned,
                    IntPtr lpOverlapped
                );
        [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern unsafe bool ReadFile(
          int hFile,
          Byte* lpBuffer,
          int nNumberOfBytesToRead,
          int* lpNumberOfBytesRead,
          int lpOverlapped);
 
5、读温度的话,
       Byte[] temp_buf = new Byte[2];
       private const uint GENERIC_READ = 0x80000000;
       private const uint GENERIC_WRITE = 0x40000000;
       private const int OPEN_EXISTING = 3;
       private int temdriver;
 
       temdriver = CreateFile("GIO1:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
       DeviceIoControl(temdriver, 0x01, null, 0, temp_buf, 2, (IntPtr)0, (IntPtr)0);
       CloseHandle(temdriver);
 
这样,DS18B20的两个温度字节就到了temp_buf 数组中
6、数据处理
 
       int temp;
       double result;
      
       temp = temp_buf[0] | (temp_buf[1] << 8);
       result = Convert.ToDouble(temp & 0x07FF) * 0.0625;
       if ((temp & 0xF800) != 0)
             result = result * (-1);
  
这样,result就是最后温度的值

附件:
[attachment=2573]
附件设置隐藏,需要回复后才能看到
It's time....
级别: 侠客
UID: 20328
精华: 0
发帖: 79
金钱: 410 两
威望: 82 点
贡献值: 0 点
综合积分: 158 分
注册时间: 2010-04-29
最后登录: 2015-05-22
1楼  发表于: 2011-12-09 17:18