program for I2C master device

Dependencies:   mbed

Fork of I2C_HelloWorld_Mbed by mbed official

Revision:
8:3e6945d5d9a1
Parent:
7:b07706b16279
Child:
9:36fc715eb54c
--- a/main.cpp	Tue Dec 15 22:44:03 2015 +0000
+++ b/main.cpp	Wed Dec 16 19:42:37 2015 +0000
@@ -3,16 +3,47 @@
  
 int main() {
     init();
+    wait_ms(1000);
     while (1) {}
 }
 
 void write_and_read(){
-    write_data();    
+   /* write_data();    
     wait_ms(20);
     char regaddr = 0x24;
-    read_data(&regaddr); // works OK (pure read, without pointer setting)
+    read_data(&regaddr); // works OK (pure read, without pointer setting)*/
+   
+    rtc_sensor();
+}  
+
+void rtc_sensor()
+{
+    char temperature[2];
+    get_temperature_raw(temperature);
+    float result = proceed_temperature(temperature);
+    pc.printf("<Temperature: %1.2f\n\r",result);
     wait_ms(20);
-}  
+}
+
+void get_temperature_raw(char* temperature)
+{
+    char reg_addr = TEMPERATURE_REGISTER;
+    if(i2c.write(RTC_ADDR, &reg_addr, 1)) 
+        pc.printf("Writing: Error\n\r");
+    wait_ms(20);
+    if(i2c.read(RTC_ADDR, temperature, 2))
+        pc.printf("Reading: Error\n\r");
+}
+
+float proceed_temperature(char * temperature)
+{
+    char MSB = temperature[0]; // MSB is a signed int8 and carries temperature in accuracy 1 Celsius degree
+    char LSB = temperature[1]; // LSB carries fraction part of temperature. Possible values are 0, 0.25, 0.5, 0.75
+    float high = (float)MSB;
+    float low = ((float)((uint8_t)LSB >> 6) * 0.25f);
+    return high + low;
+}
+
 
 void write_data(){
      //writing to slave