管理提醒: 本帖被 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]