private float ReadCapSen1_1(byte[] RegAddrBuf1, byte[] RegAddrBuf2) { byte[] ReadBuf1, ReadBuf2; ReadBuf1 = new byte[2]; /* We read 2 bytes sequentially */ ReadBuf2 = new byte[2]; /* We read 2 bytes sequentially */ I2CSensor.WriteRead(RegAddrBuf1, ReadBuf1); I2CSensor.WriteRead(RegAddrBuf2, ReadBuf2); /* In order to get the raw 16-bit data values, we need to separate the bytes */ int RawData = BitConverter.ToInt16(ReadBuf1, 0); //int RawData = BitConverter.ToInt16(ReadBuf, 0); CapMeasure capMeas1_1; capMeas1_1.CapReadLSB1_1 = (0xFF00 & RawData) >> 8; capMeas1_1.CapreadMSB1_1 = 0x00FF & RawData; /* In order to get the raw 16-bit data values, we need to separate the bytes */ RawData = BitConverter.ToInt16(ReadBuf2, 0); capMeas1_1.CapReadLSB1_2 = (0xFF00 & RawData) >> 8; capMeas1_1.CapreadMSB1_2 = 0x00FF & RawData; int CapLabel1_1 = (capMeas1_1.CapreadMSB1_1 * 256 + capMeas1_1.CapReadLSB1_1); int CapLabel1_2 = capMeas1_1.CapreadMSB1_2 * 256 + capMeas1_1.CapReadLSB1_2; int CapLabel1Both = (CapLabel1_1 * 256 + capMeas1_1.CapreadMSB1_2); float FinalCapMeasure = CapLabel1Both / 524288; return(FinalCapMeasure); }
private float ReadCapSen1_1(int RegAddrBuf1, int RegAddrBuf2) { int temp1 = (I2CSensor.ReadAddressWord(RegAddrBuf1)); int temp2 = (I2CSensor.ReadAddressWord(RegAddrBuf2)); byte[] byteData1 = BitConverter.GetBytes(temp1); byte[] byteData2 = BitConverter.GetBytes(temp2); byte[] byteData3 = { byteData2[0], byteData1[1], byteData1[0], 0 }; int CapLabel1Both = BitConverter.ToInt32(byteData3); float FinalCapMeasure = CapLabel1Both / 524288f; return(FinalCapMeasure); }
private OneRegisterRead ReadOneReg(byte[] RegAddrBuf) { byte[] ReadBuf; ReadBuf = new byte[2]; // need to read the two bytes stored in the targeted register I2CSensor.WriteRead(RegAddrBuf, ReadBuf); // A little confusing but decided to parse the two bytes into separate bytes, convert to 'int' // then return this information so we can perform calculations. int RawData = BitConverter.ToInt16(ReadBuf, 0); OneRegisterRead OneRegReadDataOut; OneRegReadDataOut.DataMSB = 0xFF00 & RawData >> 8; OneRegReadDataOut.DataLSB = 0x00FF & RawData; // if reading Done bit, use this statement ==> ((byte)(0x08 & RawData) >> 3) return(OneRegReadDataOut); }
private void WriteToI2CDevice() { /* * Initialize the FDC1004 capacitance sensor: * * For this device, we create 3-byte write buffers: ==> this device uses * The first byte is the register address we want to write to. * The second byte is the contents of the MSB that we want to write to the register. * The third byte is the contents of the LSB that we want to write to the register. */ byte[] WriteBuf_MeasurementOneFormat = new byte[] { MEAS_ONE_CONTROL, 0x1c, 0x00 }; // configs measurement 1 byte[] WriteBuf_MeasurementTwoFormat = new byte[] { MEAS_TWO_CONTROL, 0x3c, 0x00 }; // configs measurement 2 byte[] WriteBuf_MeasurementThreeFormat = new byte[] { MEAS_THREE_CONTROL, 0x5c, 0x00 }; // configs measurement 3 byte[] WriteBuf_Cin1 = new byte[] { CIN1_CONTROL, 0x30, 0x00 }; // Set Offset for Cin1 to "6"pF based on datsheet calculations byte[] WriteBuf_FDC_Config = new byte[] { FDC_CONF_CONTROL, 0x0D, 0xE0 }; //set to read at 400S/s with repeat and read at measurement #1,#2,#3 I2CSensor.Write(WriteBuf_MeasurementOneFormat); I2CSensor.Write(WriteBuf_MeasurementTwoFormat); I2CSensor.Write(WriteBuf_MeasurementThreeFormat); I2CSensor.Write(WriteBuf_Cin1); I2CSensor.Write(WriteBuf_FDC_Config); }
private void WriteToI2CDevice() { /* * Initialize the FDC1004 capacitance sensor: * * For this device, we create 3-byte write buffers: ==> this device uses * The first byte is the register address we want to write to. * The second byte is the contents of the MSB that we want to write to the register. * The third byte is the contents of the LSB that we want to write to the register. */ byte[] WriteBuf_MeasurementOneFormat = new byte[] { 0x1c, 0x00 }; // configs measurement 1 byte[] WriteBuf_MeasurementTwoFormat = new byte[] { 0x3c, 0x00 }; // configs measurement 2 byte[] WriteBuf_MeasurementThreeFormat = new byte[] { 0x5c, 0x00 }; // configs measurement 3 byte[] WriteBuf_FDC_Config = new byte[] { 0x0D, 0xE0 }; //set to read at 400S/s with repeat and read at measurement #1,#2,#3 I2CSensor.WriteAddressWord(MEAS_ONE_CONTROL, BitConverter.ToUInt16(WriteBuf_MeasurementOneFormat)); I2CSensor.WriteAddressWord(MEAS_TWO_CONTROL, BitConverter.ToUInt16(WriteBuf_MeasurementTwoFormat)); I2CSensor.WriteAddressWord(MEAS_THREE_CONTROL, BitConverter.ToUInt16(WriteBuf_MeasurementThreeFormat)); I2CSensor.WriteAddressWord(FDC_CONF_CONTROL, BitConverter.ToUInt16(WriteBuf_FDC_Config)); }
/// <summary> /// Resets the I2C port /// </summary> public void Reset() { byte[] WriteBuf_FDC_Config = new byte[] { FDC_CONF_CONTROL, 0x8C, 0x00 }; I2CSensor.Write(WriteBuf_FDC_Config); WriteToI2CDevice(); }