Steven Kay
/
MCP9803_NUCLEO_Driver
Initial I2C Working
main.cpp@1:444546e8cd20, 2017-03-29 (annotated)
- Committer:
- sk398
- Date:
- Wed Mar 29 12:20:34 2017 +0000
- Revision:
- 1:444546e8cd20
- Parent:
- 0:fbf82bf637bb
- Child:
- 2:832cb4376d2a
working, useful for I2C Comms with NUCLEO Boards;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:fbf82bf637bb | 1 | #include "mbed.h" |
sk398 | 0:fbf82bf637bb | 2 | #include "MCP9803.h" |
sk398 | 0:fbf82bf637bb | 3 | |
sk398 | 0:fbf82bf637bb | 4 | MCP9803 TempSensor(PB_9,PB_8,0x90,100000); |
sk398 | 0:fbf82bf637bb | 5 | |
sk398 | 0:fbf82bf637bb | 6 | int main() |
sk398 | 0:fbf82bf637bb | 7 | { |
sk398 | 0:fbf82bf637bb | 8 | char data[2]; |
sk398 | 0:fbf82bf637bb | 9 | |
sk398 | 0:fbf82bf637bb | 10 | data[0] = 0x01; |
sk398 | 0:fbf82bf637bb | 11 | data[1] = 0x72; |
sk398 | 0:fbf82bf637bb | 12 | |
sk398 | 0:fbf82bf637bb | 13 | printf("Write Return Value = %d\r\n",TempSensor.I2C_Write(data,2)); |
sk398 | 0:fbf82bf637bb | 14 | |
sk398 | 0:fbf82bf637bb | 15 | data[0] = 0x01; |
sk398 | 0:fbf82bf637bb | 16 | |
sk398 | 0:fbf82bf637bb | 17 | printf("Write Return Value = %d\r\n",TempSensor.I2C_Write(data,1)); |
sk398 | 0:fbf82bf637bb | 18 | |
sk398 | 0:fbf82bf637bb | 19 | TempSensor.I2C_Read(1); |
sk398 | 0:fbf82bf637bb | 20 | |
sk398 | 0:fbf82bf637bb | 21 | data[0] = 0x00; |
sk398 | 0:fbf82bf637bb | 22 | TempSensor.I2C_Write(data,1); |
sk398 | 0:fbf82bf637bb | 23 | |
sk398 | 1:444546e8cd20 | 24 | int len = 2; |
sk398 | 1:444546e8cd20 | 25 | char *buffer = TempSensor.I2C_Read(len); |
sk398 | 1:444546e8cd20 | 26 | |
sk398 | 1:444546e8cd20 | 27 | char inBuffer[len]; |
sk398 | 1:444546e8cd20 | 28 | |
sk398 | 1:444546e8cd20 | 29 | for(int i = 0; i < len; i++) |
sk398 | 1:444546e8cd20 | 30 | { |
sk398 | 1:444546e8cd20 | 31 | inBuffer[i] = *(buffer+i); |
sk398 | 1:444546e8cd20 | 32 | } |
sk398 | 1:444546e8cd20 | 33 | |
sk398 | 1:444546e8cd20 | 34 | int dat = (((inBuffer[0] << 8) | inBuffer[1])>>4); |
sk398 | 1:444546e8cd20 | 35 | |
sk398 | 1:444546e8cd20 | 36 | printf("data value = %04x\r\n",dat); |
sk398 | 1:444546e8cd20 | 37 | |
sk398 | 1:444546e8cd20 | 38 | if(dat > 2047) |
sk398 | 1:444546e8cd20 | 39 | { |
sk398 | 1:444546e8cd20 | 40 | dat -= 4096; |
sk398 | 1:444546e8cd20 | 41 | } |
sk398 | 1:444546e8cd20 | 42 | |
sk398 | 1:444546e8cd20 | 43 | float tempC = 0.0625*dat; |
sk398 | 1:444546e8cd20 | 44 | printf("Temp C = %f\r\n\r\n",tempC); |
sk398 | 0:fbf82bf637bb | 45 | |
sk398 | 0:fbf82bf637bb | 46 | |
sk398 | 0:fbf82bf637bb | 47 | |
sk398 | 0:fbf82bf637bb | 48 | } |