Read the temperaure in celsius and then convert to Fahrenheit degrees, and also display the value on the serial port.
Dependencies: Hotboards_temp mbed
main.cpp@0:44c15fe6cc16, 2016-03-22 (annotated)
- Committer:
- Hotboards
- Date:
- Tue Mar 22 22:09:37 2016 +0000
- Revision:
- 0:44c15fe6cc16
first release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hotboards | 0:44c15fe6cc16 | 1 | #include "mbed.h" |
Hotboards | 0:44c15fe6cc16 | 2 | /* |
Hotboards | 0:44c15fe6cc16 | 3 | Hotboards temp Library - Farenheit degrees |
Hotboards | 0:44c15fe6cc16 | 4 | Read the temperaure in celsius and then convert to Farenheit degrees, and also display |
Hotboards | 0:44c15fe6cc16 | 5 | the value on the serial port. |
Hotboards | 0:44c15fe6cc16 | 6 | The circuit |
Hotboards | 0:44c15fe6cc16 | 7 | * VDD --> 3.3v |
Hotboards | 0:44c15fe6cc16 | 8 | * SDA --> PB_9 |
Hotboards | 0:44c15fe6cc16 | 9 | * SCL --> PB_8 |
Hotboards | 0:44c15fe6cc16 | 10 | * ALERT --> PB_13 |
Hotboards | 0:44c15fe6cc16 | 11 | * GND --> GND |
Hotboards | 0:44c15fe6cc16 | 12 | Library and example created by Diego from Hotboards |
Hotboards | 0:44c15fe6cc16 | 13 | Ported to mbed by Pedro from Hotboards |
Hotboards | 0:44c15fe6cc16 | 14 | This example code is in the public domain. |
Hotboards | 0:44c15fe6cc16 | 15 | */ |
Hotboards | 0:44c15fe6cc16 | 16 | #include "mbed.h" |
Hotboards | 0:44c15fe6cc16 | 17 | #include "Hotboards_temp.h" |
Hotboards | 0:44c15fe6cc16 | 18 | |
Hotboards | 0:44c15fe6cc16 | 19 | Serial pc(USBTX, USBRX); |
Hotboards | 0:44c15fe6cc16 | 20 | //I2C bus instance for the library |
Hotboards | 0:44c15fe6cc16 | 21 | I2C device( I2C_SDA, I2C_SCL ); |
Hotboards | 0:44c15fe6cc16 | 22 | // instance a sensor with address number 7 (none of the jumpers on the board is short circuited) |
Hotboards | 0:44c15fe6cc16 | 23 | // and also 0.5 celsius degrees resolution |
Hotboards | 0:44c15fe6cc16 | 24 | Hotboards_temp sensor( device, Sensor_7); |
Hotboards | 0:44c15fe6cc16 | 25 | |
Hotboards | 0:44c15fe6cc16 | 26 | |
Hotboards | 0:44c15fe6cc16 | 27 | |
Hotboards | 0:44c15fe6cc16 | 28 | int main( void ) |
Hotboards | 0:44c15fe6cc16 | 29 | { |
Hotboards | 0:44c15fe6cc16 | 30 | |
Hotboards | 0:44c15fe6cc16 | 31 | // init sensor |
Hotboards | 0:44c15fe6cc16 | 32 | sensor.init(); |
Hotboards | 0:44c15fe6cc16 | 33 | |
Hotboards | 0:44c15fe6cc16 | 34 | while(1) |
Hotboards | 0:44c15fe6cc16 | 35 | { |
Hotboards | 0:44c15fe6cc16 | 36 | // read temperature in celcius degrees |
Hotboards | 0:44c15fe6cc16 | 37 | float temp = sensor.read(); |
Hotboards | 0:44c15fe6cc16 | 38 | // convert celcius to fahrenheit |
Hotboards | 0:44c15fe6cc16 | 39 | float tempF = sensor.CelsiusToFarenheit( temp); |
Hotboards | 0:44c15fe6cc16 | 40 | // print it on the serial port |
Hotboards | 0:44c15fe6cc16 | 41 | pc.printf("Temp sensor : %3.1f F\r\n",tempF); |
Hotboards | 0:44c15fe6cc16 | 42 | // take the next value after 2 sec (just to not read too often) |
Hotboards | 0:44c15fe6cc16 | 43 | wait(2); |
Hotboards | 0:44c15fe6cc16 | 44 | } |
Hotboards | 0:44c15fe6cc16 | 45 | } |