LSM9DS1 i2c

Dependencies:   mbed

Committer:
antimo8
Date:
Sun Jul 08 08:26:04 2018 +0000
Revision:
0:986686a93674
Child:
1:a7dc83962b93
create repo;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antimo8 0:986686a93674 1 #include "mbed.h"
antimo8 0:986686a93674 2
antimo8 0:986686a93674 3 #define LM75_REG_TEMP (0x00) // Temperature Register
antimo8 0:986686a93674 4 #define LM75_REG_CONF (0x01) // Configuration Register
antimo8 0:986686a93674 5 #define LM75_ADDR (0x90) // LM75 address
antimo8 0:986686a93674 6
antimo8 0:986686a93674 7 I2C i2c(I2C_SDA, I2C_SCL);
antimo8 0:986686a93674 8
antimo8 0:986686a93674 9 DigitalOut myled(LED1);
antimo8 0:986686a93674 10
antimo8 0:986686a93674 11 Serial pc(SERIAL_TX, SERIAL_RX);
antimo8 0:986686a93674 12
antimo8 0:986686a93674 13 volatile char TempCelsiusDisplay[] = "+abc.d C";
antimo8 0:986686a93674 14
antimo8 0:986686a93674 15 int main()
antimo8 0:986686a93674 16 {
antimo8 0:986686a93674 17
antimo8 0:986686a93674 18 char data_write[2];
antimo8 0:986686a93674 19 char data_read[2];
antimo8 0:986686a93674 20
antimo8 0:986686a93674 21 /* Configure the Temperature sensor device STLM75:
antimo8 0:986686a93674 22 - Thermostat mode Interrupt
antimo8 0:986686a93674 23 - Fault tolerance: 0
antimo8 0:986686a93674 24 */
antimo8 0:986686a93674 25 data_write[0] = LM75_REG_CONF;
antimo8 0:986686a93674 26 data_write[1] = 0x02;
antimo8 0:986686a93674 27 int status = i2c.write(LM75_ADDR, data_write, 2, 0);
antimo8 0:986686a93674 28 if (status != 0) { // Error
antimo8 0:986686a93674 29 while (1) {
antimo8 0:986686a93674 30 myled = !myled;
antimo8 0:986686a93674 31 wait(0.2);
antimo8 0:986686a93674 32 }
antimo8 0:986686a93674 33 }
antimo8 0:986686a93674 34
antimo8 0:986686a93674 35 while (1) {
antimo8 0:986686a93674 36 // Read temperature register
antimo8 0:986686a93674 37 data_write[0] = LM75_REG_TEMP;
antimo8 0:986686a93674 38 i2c.write(LM75_ADDR, data_write, 1, 1); // no stop
antimo8 0:986686a93674 39 i2c.read(LM75_ADDR, data_read, 2, 0);
antimo8 0:986686a93674 40
antimo8 0:986686a93674 41 // Calculate temperature value in Celcius
antimo8 0:986686a93674 42 int tempval = (int)((int)data_read[0] << 8) | data_read[1];
antimo8 0:986686a93674 43 tempval >>= 7;
antimo8 0:986686a93674 44 if (tempval <= 256) {
antimo8 0:986686a93674 45 TempCelsiusDisplay[0] = '+';
antimo8 0:986686a93674 46 } else {
antimo8 0:986686a93674 47 TempCelsiusDisplay[0] = '-';
antimo8 0:986686a93674 48 tempval = 512 - tempval;
antimo8 0:986686a93674 49 }
antimo8 0:986686a93674 50
antimo8 0:986686a93674 51 // Decimal part (0.5°C precision)
antimo8 0:986686a93674 52 if (tempval & 0x01) {
antimo8 0:986686a93674 53 TempCelsiusDisplay[5] = 0x05 + 0x30;
antimo8 0:986686a93674 54 } else {
antimo8 0:986686a93674 55 TempCelsiusDisplay[5] = 0x00 + 0x30;
antimo8 0:986686a93674 56 }
antimo8 0:986686a93674 57
antimo8 0:986686a93674 58 // Integer part
antimo8 0:986686a93674 59 tempval >>= 1;
antimo8 0:986686a93674 60 TempCelsiusDisplay[1] = (tempval / 100) + 0x30;
antimo8 0:986686a93674 61 TempCelsiusDisplay[2] = ((tempval % 100) / 10) + 0x30;
antimo8 0:986686a93674 62 TempCelsiusDisplay[3] = ((tempval % 100) % 10) + 0x30;
antimo8 0:986686a93674 63
antimo8 0:986686a93674 64 // Display result
antimo8 0:986686a93674 65 pc.printf("temp = %s\n", TempCelsiusDisplay);
antimo8 0:986686a93674 66 myled = !myled;
antimo8 0:986686a93674 67 wait(1.0);
antimo8 0:986686a93674 68 }
antimo8 0:986686a93674 69
antimo8 0:986686a93674 70 }
antimo8 0:986686a93674 71