1,初始化
#define tmp_gpio_cfgpin_OUT (v_pGPIORegs->GPFCON=v_pGPIORegs->GPFCON&(~(3<<2)) | 1<<2)
#define tmp_gpio_cfgpin_in (v_pGPIORegs->GPFCON=v_pGPIORegs->GPFCON&(~(3<<2)) | 0<<2)
#define tmp_gpio_pulldown (v_pGPIORegs->GPFUDP=v_pGPIORegs->GPFUDP&(~(3<<2)) | 0<<2)
#define tmp_gpio_pullup (v_pGPIORegs->GPFUDP=v_pGPIORegs->GPFUDP&(~(3<<2)) | 1<<2)
#define tmp_gpio_setdata_down (v_pGPIORegs->GPFDAT=v_pGPIORegs->GPFDAT&(~(1<<1)) | 0<<1)
#define tmp_gpio_setdata_up (v_pGPIORegs->GPFDAT=v_pGPIORegs->GPFDAT&(~(1<<1)) | 1<<1)
#define tmp_gpio_setdata( n) setdat(n)//(v_pGPIORegs->GPFDAT=v_pGPIORegs->GPFDAT&(~(1<<1)) | n<<1)
#define tmp_gpio_getdata (v_pGPIORegs->GPFDAT&((1<<1)) )
static int ds18b20_init(void)
{
int retval = 0;
tmp_gpio_cfgpin_OUT;//(DQ, CFG_OUT);
tmp_gpio_pulldown;//(DQ, 0);
tmp_gpio_setdata_up;//(DQ, 1);
sleep(2);
tmp_gpio_setdata_down;//(DQ, 0); // 拉低ds18b20总线,复位ds18b20
sleep(500); // 保持复位电平500us
tmp_gpio_setdata_up;//(DQ, 1); // 释放ds18b20总线
sleep(60);
// 若复位成功,ds18b20发出存在脉冲(低电平,持续60~240us)
tmp_gpio_cfgpin_in;//(DQ, CFG_IN);
retval = tmp_gpio_getdata;//(DQ);
sleep(500);
tmp_gpio_cfgpin_OUT;//(DQ, CFG_OUT);
tmp_gpio_pulldown;//(DQ, 0);
tmp_gpio_setdata_up;//(DQ, 1); // 释放总线
// RETAILMSG(1, (TEXT("Tmp init ok ...\r\n")));
return retval;
}
2,写时序
static void write_byte(unsigned char data)
{
int i = 0;
tmp_gpio_cfgpin_OUT;//(DQ, CFG_OUT);
tmp_gpio_pullup;//(DQ, 1);
for (i = 0; i < 8; i ++)
{
// 总线从高拉至低电平时,就产生写时隙
tmp_gpio_setdata_up;//(DQ, 1);
sleep(2);
tmp_gpio_setdata_down;//(DQ, 0);
tmp_gpio_setdata( data & 0x01);
sleep(60);
data >>= 1;
}
tmp_gpio_setdata_up;//(DQ, 1); // 重新释放ds18b20总线
}
3,读数据返回给应用程序
static unsigned char read_byte(void)
{
int i;
unsigned char data = 0;
for (i = 0; i < 8; i++)
{
// 总线从高拉至低,只需维持低电平17ts,再把总线拉高,就产生读时隙
tmp_gpio_cfgpin_OUT;//(DQ, CFG_OUT);
tmp_gpio_pulldown;//(DQ, 0);
tmp_gpio_setdata_up;//(DQ, 1);
sleep(2);
tmp_gpio_setdata_down;//(DQ, 0);
sleep(2);
tmp_gpio_setdata_up;//(DQ, 1);
sleep(8);
data >>= 1;
tmp_gpio_cfgpin_in;//(DQ, CFG_IN);
if (tmp_gpio_getdata)
data |= 0x80;
sleep(50);
}
tmp_gpio_cfgpin_OUT;//(DQ, CFG_OUT);
tmp_gpio_pulldown;//(DQ, 0);
tmp_gpio_setdata_up;//(DQ, 1); // 释放ds18b20总线
return data;
}
DWORD TMP_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
RETAILMSG(DEBUG_GPIO, (TEXT("Hello_Control: GPIO_Read\r\n")));
unsigned char integer_value = 0;
float tmpValue;
float temperature, decimal_value; // 温度数值,decimal_value为小数部分的值
int flag;
unsigned long err;
unsigned char result[2] = {0x00, 0x00};
//struct ds18b20_device *dev = filp->private_data;
int tmp;//=result[0]+result[1]*16;
flag = ds18b20_init();
if (flag)
{
//#ifdef DEBUG
RETAILMSG(1, (TEXT("ds18b20 init failed ...\r\n")));
// printk(KERN_WARNING "ds18b20 init failed\n");
//#endif
return -1;
}
#if 1
write_byte(0xcc);
write_byte(0x44);
flag = ds18b20_init();
if (flag)
return -1;
write_byte(0xcc);
write_byte(0xbe);
result[0] = read_byte(); // 温度低八位
result[1] = read_byte(); // 温度高八位
//*(DWORD *)pBuffer=*(DWORD *)result;
//pBuffer=(LPVOID)result;
*(DWORD *)pBuffer=(int)result[0]+(((int)result[1])<<8);
// integer_value = ((result[0] & 0xf0) >> 4) | ((result[1] & 0x07) << 4);
// 精确到0.25度
// decimal_value = 0.5 * ((result[0] & 0x0f) >> 3) + 0.25 * ((result[0] & 0x07) >> 2);
// decimal_value = 0.5 * ((result[0] & 0x0f) >> 3) + 0.25 * ((result[0] & 0x07) >> 2)+ 0.125 * ((result[0] & 0x03) >> 1)+ 0.0625 * ((result[0] & 0x01) >>0);
// tmpValue = (float)integer_value + decimal_value;
// tmp=result[0]-'0'+(result[1]-'0'<<8);
//*(DWORD *)pBuffer=(int)(result[0])+(int)(result[1]<<4);
// pBuffer[0] = read_byte(); // 温度低八位
// pBuffer[1] = read_byte(); // 温度高八位
RETAILMSG(1,(TEXT("Tmpvalue= %d %d %d %d \r\n"),(int)result[0],(int)result[1],*(DWORD *)result,*(DWORD *)pBuffer));
#endif
return TRUE;
}
4,应用程序读取
void CtmpDlg::OnCalValue()
{
// TODO: 在此添加控件通知处理程序代码
BYTE byReceiveBuf[1024]; ///缓冲区的大小
LPVOID lpbuff;CString str1;
unsigned char result[2] = {0x00, 0x00};
int nBytes= 0;
int sock;
DWORD dwBytes;
float tmpValue;
unsigned char integer_value = 0;
float temperature, decimal_value; // 温度数值,decimal_value为小数部分的值
hDeviceTmp=CreateFile(L"TMP1:",GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
//DeviceIoControl(hDeviceTmp,0,NULL,0,NULL,0,NULL,NULL);
// memset(byReceiveBuf, 0, 1024);
while(1)
{
//读字符
if(ReadFile(hDeviceTmp, lpbuff, 1, &dwBytes, 0))
{
Sleep(1000);
result[0]=(*(DWORD *)lpbuff)&0xff;
result[1]=(*(DWORD *)lpbuff)&0xff00>>4;
integer_value = ((result[0] & 0xf0) >> 4) | ((result[1] & 0x07) << 4);
// 精确到0.25度
decimal_value = 0.5 * ((result[0] & 0x0f) >> 3) + 0.25 * ((result[0] & 0x07) >> 2);
// decimal_value = 0.5 * ((result[0] & 0x0f) >> 3) + 0.25 * ((result[0] & 0x07) >> 2)+ 0.125 * ((result[0] & 0x03) >> 1)+ 0.0625 * ((result[0] & 0x01) >>0);
tmpValue = (float)integer_value + decimal_value;
//tmpValue=lpbuff[0];
str1.Format(_T("%f"),tmpValue);
m_TmpEdit.SetWindowTextW(str1);
}
}
}