Initial I2C Working
Revision 2:832cb4376d2a, committed 2017-03-29
- Comitter:
- sk398
- Date:
- Wed Mar 29 17:55:38 2017 +0000
- Parent:
- 1:444546e8cd20
- Child:
- 3:c6aad2355a40
- Commit message:
- Working version on the NUCLEO-F446RE board
Changed in this revision
--- a/MCP9803/MCP9803.cpp Wed Mar 29 12:20:34 2017 +0000
+++ b/MCP9803/MCP9803.cpp Wed Mar 29 17:55:38 2017 +0000
@@ -30,26 +30,115 @@
inBuffer = (char *)malloc(1);
}
-int MCP9803::ConfigSensor()
+
+
+int MCP9803::ConfigSensor( int shutdown, int comp_int, int alert_polarity,
+ int fault_guide, int adc_res, int one_shot)
{
+ // Assign passed values to struct members
+ CONFIG_REG_VALUE.CONFIG_BITS.SHUTDOWN_BIT = shutdown;
+ CONFIG_REG_VALUE.CONFIG_BITS.COMP_INT_BIT = comp_int;
+ CONFIG_REG_VALUE.CONFIG_BITS.ALERT_POLARITY_BIT = alert_polarity;
+ CONFIG_REG_VALUE.CONFIG_BITS.FAULT_QUEUE = fault_guide;
+ CONFIG_REG_VALUE.CONFIG_BITS.ADC_RES = adc_res;
+ CONFIG_REG_VALUE.CONFIG_BITS.ONE_SHOT = one_shot;
+
+ // Generate Command to send to MCP9803
+ // <Pointer to Config Register, CONFIG Register Value>
+ char dataOut[] = {CONFIG_REG_POINT, CONFIG_REG_VALUE.CONFIG_VALUE};
+
+ // Write Bytes to MCP9803
+ int writeReturn = I2C_Write(dataOut,CONFIG_CMD_LENGTH);
+
+ // Check on return status of the Write command
+ if(writeReturn != 0)
+ {
+ // Failure
+ return FAILURE;
+ }
+ else
+ {
+ // Success
+ return SUCCESS;
+ }
+}
+
+
+int MCP9803::RawTempValue()
+{
+ // Set MCP9803 Register Pointer to point to Temperature Register
+ I2C_Write((char *)TEMP_REG_POINT,1);
+
+ // Read TEMP_DATA_LENGTH bytes into temp buffer
+ char *tempInBuffer = I2C_Read(TEMP_DATA_LENGTH);
+ // Convert tempInBuffer to rawData
+ // Shift first Byte (MSB) up by 8, OR with second Byte (LSB)
+ // Then shift Result down by 4
+ int rawData = ((*(tempInBuffer)<<8|*(tempInBuffer+1))>>4);
+
+ //
+ return rawData;
+}
+
+
+
+float MCP9803::FormattedTempValue(int format)
+{
+ // Get Raw Temp Value
+ int tempData = RawTempValue();
- return 0;
+ if(tempData == READ_TEMP_FAIL_VALUE)
+ {
+ return READ_TEMP_FAIL_ERROR;
+ }
+
+ // Check and convert if 2's Complement
+ if(tempData > 2047)
+ {
+ tempData -= 4096;
+ }
+
+ // Return formatted data based on format
+ switch(format)
+ {
+ // Return celcius value of temperature
+ case CELCIUS:
+ return (RAW_TO_C*tempData);
+
+ // Return farenheit value of temperature
+ case FARENHEIT:
+ return ((RAW_TO_C*tempData)*C_F_1)+C_F_2;
+
+ // Return Kelvin value of temperature
+ case KELVIN:
+ return ((RAW_TO_C*tempData)+C_TO_F);
+
+ // If input is invalid, return value outwith range
+ // of other format options
+ default:
+ return FORMAT_FAIL;
+ }
}
int MCP9803::I2C_Write(char *dataOut,int dataLen)
{
+ // Define Char Buffer of length dataLen
char outBuffer[dataLen];
+ // Populate Buffer with dataOut
for(int i=0 ; i<dataLen ; i++)
{
outBuffer[i] = *(dataOut+i);
}
+ // Write out to I2C Bus
int sendStatus = _I2C -> write(chipAddress,outBuffer,dataLen,0);
+ // Return success value of the Write
+ // Returns 0 for Success, 1 for failure
return sendStatus;
}
@@ -57,50 +146,47 @@
char *MCP9803::I2C_Read(int dataLen)
{
+ // Reallocate memory to dataLen length expected for inBuffer
setBufferSize(dataLen);
+ // Read from the I2C Bus
int receiveStatus = _I2C -> read(chipAddress,inBuffer,dataLen,0);
+ // Check success value
+ // If receiveStatus != 0, indicates failure
if(receiveStatus != 0)
{
+ // Generate unique command to indicate failure, based on number of
+ // expected incoming bytes
for(int i = 0; i < dataLen; i++)
{
+ // For even indexes of inBuffer
if(i % 2 == 0)
{
- inBuffer[i] = 0xFF;
+ inBuffer[i] = FAIL_EVEN_VALUE;
}
+ // For odd indexes of inBuffer
else
{
- inBuffer[i] = 0x00;
+ inBuffer[i] = FAIL_ODD_VALUE;
}
- printf("Buffer Value = %02x",inBuffer[i]);
}
+ // Return unique error command
return inBuffer;
}
-
else
{
+ // Return Buffer contents based on successful read
return inBuffer;
}
-
}
void MCP9803::setBufferSize(int dataLen)
{
+ // Reallocate memory for the inBuffer to account for differing lengths of
+ // data which can be read in
inBuffer = (char *)realloc(inBuffer, dataLen);
}
-
-char *MCP9803::getBuffer()
-{
-
-}
-
-void MCP9803::setBuffer()
-{
-
-}
-
-
/********************************************************************************************
--- a/MCP9803/MCP9803.h Wed Mar 29 12:20:34 2017 +0000
+++ b/MCP9803/MCP9803.h Wed Mar 29 17:55:38 2017 +0000
@@ -19,6 +19,34 @@
#include "mbed.h"
+#define SUCCESS 0
+#define FAILURE 1
+
+#define FAIL_EVEN_VALUE 0xFF
+#define FAIL_ODD_VALUE 0x00
+
+#define TEMP_REG_POINT 0x00
+#define CONFIG_REG_POINT 0x01
+#define TEMP_HYST_POINT 0x02
+#define TEMP_LIM_SET_POINT 0x03
+
+#define CONFIG_CMD_LENGTH 2
+#define TEMP_DATA_LENGTH 2
+
+#define READ_TEMP_FAIL_VALUE 0x0FF0
+#define READ_TEMP_FAIL_ERROR -2000
+
+#define CELCIUS 0x01
+#define FARENHEIT 0x02
+#define KELVIN 0x03
+#define FORMAT_FAIL -1000
+
+#define RAW_TO_C 0.0625
+#define C_F_1 1.8
+#define C_F_2 32
+#define C_TO_F 273.15
+
+
union CONFIG_REG
{
struct
@@ -39,9 +67,12 @@
public:
MCP9803(PinName sda, PinName scl, int Address, int frequency);
- int ConfigSensor();
- int I2C_Write(char *dataOut,int dataLen);
- char *I2C_Read(int dataLen);
+ int ConfigSensor( int shutdown, int comp_int, int alert_polarity,
+ int fault_guide, int adc_res, int one_shot);
+
+
+ int RawTempValue();
+ float FormattedTempValue(int format);
private:
I2C *_I2C;
@@ -50,13 +81,10 @@
char *inBuffer;
- void setBufferSize(int dataLen);
- char *getBuffer();
- void setBuffer();
+ int I2C_Write(char *dataOut,int dataLen);
+ char *I2C_Read(int dataLen);
+ void setBufferSize(int dataLen);
-protected:
-
-
};
#endif
\ No newline at end of file
--- a/main.cpp Wed Mar 29 12:20:34 2017 +0000
+++ b/main.cpp Wed Mar 29 17:55:38 2017 +0000
@@ -5,44 +5,24 @@
int main()
{
- char data[2];
+ // Example of how to Configure Sensor
- data[0] = 0x01;
- data[1] = 0x72;
-
- printf("Write Return Value = %d\r\n",TempSensor.I2C_Write(data,2));
-
- data[0] = 0x01;
+ int configSuccess = TempSensor.ConfigSensor(0x00,0x01,0x00,0x02,0x03,0x00);
- printf("Write Return Value = %d\r\n",TempSensor.I2C_Write(data,1));
-
- TempSensor.I2C_Read(1);
-
- data[0] = 0x00;
- TempSensor.I2C_Write(data,1);
-
- int len = 2;
- char *buffer = TempSensor.I2C_Read(len);
-
- char inBuffer[len];
+ // Print value returned from ConfigSensor
+ // 0 indicates success, 1 indicates failure
+ printf("CONFIG Success = %d\r\n\r\n",configSuccess);
- for(int i = 0; i < len; i++)
- {
- inBuffer[i] = *(buffer+i);
- }
-
- int dat = (((inBuffer[0] << 8) | inBuffer[1])>>4);
-
- printf("data value = %04x\r\n",dat);
+ // Print raw temp value
+ // Anything other than 0x0FF0 indicates success
+ printf("Temp Raw = %04x\r\n",TempSensor.RawTempValue());
- if(dat > 2047)
- {
- dat -= 4096;
- }
-
- float tempC = 0.0625*dat;
- printf("Temp C = %f\r\n\r\n",tempC);
-
-
-
+ // Print converted temp values
+ // Anything other than -2000.000 indicates success
+ printf("Temp C = %f\r\n",TempSensor.FormattedTempValue(CELCIUS));
+ printf("Temp F = %f\r\n",TempSensor.FormattedTempValue(FARENHEIT));
+ printf("Temp K = %f\r\n",TempSensor.FormattedTempValue(KELVIN));
+
+ // Example of failure from sending wrong format
+ printf("Temp ? = %f\r\n",TempSensor.FormattedTempValue(0x0F));
}
\ No newline at end of file