Steven Kay / Mbed 2 deprecated MCP9803_NUCLEO_Driver

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP9803.cpp Source File

MCP9803.cpp

00001 /********************************************************************************************
00002 
00003    Filename: MCP9803.cpp
00004 
00005    Original Author: Steven Kay
00006 
00007    Development Group: Autonomous Systems Group, RAL Space
00008 
00009    Original Creation Date: April 2017
00010 
00011    Description: <Desc>
00012 
00013    Revision History: Version 1.0 - Initial Release
00014 
00015  *********************************************************************************************/
00016 
00017 #include "MCP9803.h"
00018 
00019 MCP9803::MCP9803(PinName sda, PinName scl, int Address, int frequency)
00020 {
00021     _I2C = new I2C(sda,scl);
00022    
00023     _I2C -> frequency(frequency);
00024 
00025     _I2C -> start();
00026     _I2C -> stop();
00027     
00028     chipAddress = Address;
00029     
00030     inBuffer = (char *)malloc(1);
00031 }
00032 
00033 
00034 
00035 int MCP9803::ConfigSensor(  int shutdown, int comp_int, int alert_polarity,
00036                             int fault_guide, int adc_res, int one_shot)
00037 {
00038     // Assign passed values to struct members
00039     CONFIG_REG_VALUE.CONFIG_BITS.SHUTDOWN_BIT = shutdown;
00040     CONFIG_REG_VALUE.CONFIG_BITS.COMP_INT_BIT = comp_int;
00041     CONFIG_REG_VALUE.CONFIG_BITS.ALERT_POLARITY_BIT = alert_polarity;
00042     CONFIG_REG_VALUE.CONFIG_BITS.FAULT_QUEUE = fault_guide;
00043     CONFIG_REG_VALUE.CONFIG_BITS.ADC_RES = adc_res;
00044     CONFIG_REG_VALUE.CONFIG_BITS.ONE_SHOT = one_shot;
00045 
00046     // Generate Command to send to MCP9803
00047     // <Pointer to Config Register, CONFIG Register Value>
00048     char dataOut[] = {CONFIG_REG_POINT, CONFIG_REG_VALUE.CONFIG_VALUE}; 
00049     
00050     // Write Bytes to MCP9803
00051     int writeReturn = I2C_Write(dataOut,CONFIG_CMD_LENGTH);
00052     
00053     // Check on return status of the Write command
00054     if(writeReturn != 0)
00055     {
00056         // Failure
00057         return FAILURE;     
00058     }
00059     else
00060     {
00061         // Success
00062         return SUCCESS;   
00063     }
00064 }
00065 
00066 
00067 int MCP9803::RawTempValue()
00068 {
00069     // Set MCP9803 Register Pointer to point to Temperature Register
00070     I2C_Write((char *)TEMP_REG_POINT,1);
00071     
00072     // Read TEMP_DATA_LENGTH bytes into temp buffer
00073     char *tempInBuffer = I2C_Read(TEMP_DATA_LENGTH);
00074     
00075     // Convert tempInBuffer to rawData
00076     // Shift first Byte (MSB) up by 8, OR with second Byte (LSB)
00077     // Then shift Result down by 4
00078     int rawData = ((*(tempInBuffer)<<8|*(tempInBuffer+1))>>4);
00079 
00080     // 
00081     return rawData;
00082 }
00083 
00084 
00085 
00086 float MCP9803::FormattedTempValue(int format)
00087 {
00088     // Get Raw Temp Value
00089     int tempData = RawTempValue();
00090     
00091     if(tempData == READ_TEMP_FAIL_VALUE)
00092     {
00093         return READ_TEMP_FAIL_ERROR;
00094     }
00095     
00096     // Check and convert if 2's Complement
00097     if(tempData > 2047)
00098     {
00099         tempData -= 4096;
00100     }
00101     
00102     // Return formatted data based on format
00103     switch(format)
00104     {
00105         // Return celcius value of temperature
00106         case CELCIUS:
00107             return (RAW_TO_C*tempData);
00108 
00109         // Return farenheit value of temperature
00110         case FARENHEIT:
00111             return ((RAW_TO_C*tempData)*C_F_1)+C_F_2;
00112 
00113         // Return Kelvin value of temperature
00114         case KELVIN:
00115             return ((RAW_TO_C*tempData)+C_TO_F);
00116  
00117         // If input is invalid, return value outwith range
00118         // of other format options
00119         default:
00120             return FORMAT_FAIL;
00121     }
00122 }
00123 
00124 
00125 
00126 int MCP9803::I2C_Write(char *dataOut,int dataLen)
00127 {    
00128     // Define Char Buffer of length dataLen
00129     char outBuffer[dataLen];
00130     
00131     // Populate Buffer with dataOut
00132     for(int i=0 ; i<dataLen ; i++)
00133     {
00134         outBuffer[i] = *(dataOut+i);
00135     }
00136     
00137     // Write out to I2C Bus
00138     int sendStatus = _I2C -> write(chipAddress,outBuffer,dataLen,0);
00139     
00140     // Return success value of the Write
00141     // Returns 0 for Success, 1 for failure
00142     return sendStatus;
00143 }
00144 
00145 
00146 
00147 char *MCP9803::I2C_Read(int dataLen)
00148 {
00149     // Reallocate memory to dataLen length expected for inBuffer
00150     setBufferSize(dataLen);
00151     
00152     // Read from the I2C Bus
00153     int receiveStatus = _I2C -> read(chipAddress,inBuffer,dataLen,0);
00154    
00155    // Check success value
00156    // If receiveStatus != 0, indicates failure
00157     if(receiveStatus != 0)
00158     {
00159         // Generate unique command to indicate failure, based on number of 
00160         // expected incoming bytes
00161         for(int i = 0; i < dataLen; i++)
00162         {
00163             // For even indexes of inBuffer
00164             if(i % 2 == 0)
00165             {
00166                 inBuffer[i] = FAIL_EVEN_VALUE;
00167             }
00168             // For odd indexes of inBuffer
00169             else
00170             {
00171                 inBuffer[i] = FAIL_ODD_VALUE;
00172             }
00173         }
00174         // Return unique error command
00175         return inBuffer;
00176     }
00177     else
00178     {
00179         // Return Buffer contents based on successful read
00180         return inBuffer;
00181     }
00182 }
00183     
00184 void MCP9803::setBufferSize(int dataLen)
00185 {
00186     // Reallocate memory for the inBuffer to account for differing lengths of 
00187     // data which can be read in
00188     inBuffer = (char *)realloc(inBuffer, dataLen);
00189 }
00190 
00191 /********************************************************************************************
00192 
00193    Method: LinearMotor::LinearMotor
00194 
00195    Description: Class initialiser sets up the Linear Motor's pins
00196 
00197    Method Visibility: Public
00198 
00199    Input Arguments
00200       Name               Type              Description
00201       -----------------------------------------------------------------------------------------
00202       pwmPin             Int               The integer number of the connected hardware pin for
00203                                            the PWM output to the linear motor
00204 
00205       sensorPin          Int               The integer number of the connected hardware pin for
00206                                            the Analog Sensor feedback from the Linear Motor
00207 
00208    Output Arguments
00209       ----------------------------------------------------------------------------------------
00210       Name               Type              Description
00211       N/A
00212 
00213  *********************************************************************************************/