主要参考2416下的电容屏和摄像头驱动代码
首先配置 然后写入 然后读。
代码如下
CString lpGroupName=TEXT("I2C0:");
hI2C = CreateFile(lpGroupName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, 0);
if(hI2C == INVALID_HANDLE_VALUE)
{
MessageBox(TEXT("open error"));
}
I2C_INIT_DESC I2CInitDesc;
I2CInitDesc.IN_wSlaveAddr =0xb6;
I2CInitDesc.IN_dwClockSpeed = 200;
I2CInitDesc.OUT_dwActualSpeed = 0;
if(!DeviceIoControl(hI2C,
IOCTL_I2C_INIT,
&I2CInitDesc, sizeof(I2CInitDesc),
NULL, 0,
NULL, NULL) )
{
}
Sleep(10);
I2C_WRITE_DESC I2CWriteDesc;
I2C_READ_DESC I2CReadDesc;
unsigned char i1=0x27;
CString ad;
unsigned char i[31]="";
CString pp;
I2CWriteDesc.IO_pbtData = &i1;
I2CWriteDesc.IN_dwData = 1;
I2CWriteDesc.IN_bStop = false;
for(int yy1=0;yy1<3;yy1++)
{
if ( !DeviceIoControl(hI2C,
IOCTL_I2C_GENERAL_WRITE,
&I2CWriteDesc, sizeof(I2C_WRITE_DESC),
NULL, 0,
NULL, NULL) )
{
}
else
{
I2C_READ_DESC I2CReadDesc;
//i=0;
I2CReadDesc.IO_pbtData = i;
I2CReadDesc.IN_dwData = 31;
I2CReadDesc.IN_bStop = true;
if (!DeviceIoControl(hI2C,
IOCTL_I2C_GENERAL_READ,
&I2CReadDesc, sizeof(I2C_READ_DESC),
NULL, 0,
NULL, NULL))
{
}
int x = i[0];
int y =i[1];
float cal=((y<<8)|x)*0.02-273.15;
ad.Format(TEXT("%0.2f ℃"),cal);
m_string.SetWindowTextW(ad);
}
}
CloseHandle(hI2C);