Initial I2C Working

Dependencies:   mbed

main.cpp

Committer:
sk398
Date:
2017-03-29
Revision:
1:444546e8cd20
Parent:
0:fbf82bf637bb
Child:
2:832cb4376d2a

File content as of revision 1:444546e8cd20:

#include "mbed.h"
#include "MCP9803.h"

MCP9803 TempSensor(PB_9,PB_8,0x90,100000);

int main()
{
    char data[2];
    
    data[0] = 0x01;
    data[1] = 0x72;
    
    printf("Write Return Value = %d\r\n",TempSensor.I2C_Write(data,2));
    
    data[0] = 0x01;
    
    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];
    
    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);
    
    if(dat > 2047)
    {
        dat -= 4096;
    }
    
    float tempC = 0.0625*dat;
    printf("Temp C = %f\r\n\r\n",tempC);   
    
    
    
}