sqefqsdf
Dependencies: C12832 EthernetInterface LM75B mbed-rtos mbed
Fork of app-board-LM75B by
TemperatureSensor.cpp@7:0618a1e407d0, 2017-05-09 (annotated)
- Committer:
- gimohd
- Date:
- Tue May 09 12:26:57 2017 +0000
- Revision:
- 7:0618a1e407d0
- Parent:
- 6:77a4c45f6416
dfdsqf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gimohd | 6:77a4c45f6416 | 1 | #include "TemperatureSensor.h" |
gimohd | 6:77a4c45f6416 | 2 | #include "LM75B.h" |
gimohd | 6:77a4c45f6416 | 3 | #include <mbed.h> |
gimohd | 6:77a4c45f6416 | 4 | #include <string> |
gimohd | 6:77a4c45f6416 | 5 | |
gimohd | 6:77a4c45f6416 | 6 | |
gimohd | 6:77a4c45f6416 | 7 | TemperatureSensor::TemperatureSensor(PinName sca, PinName scl) |
gimohd | 6:77a4c45f6416 | 8 | :LM75B(sca,scl) |
gimohd | 6:77a4c45f6416 | 9 | { |
gimohd | 6:77a4c45f6416 | 10 | this->sca = sca; |
gimohd | 6:77a4c45f6416 | 11 | this->scl = scl; |
gimohd | 6:77a4c45f6416 | 12 | } |
gimohd | 6:77a4c45f6416 | 13 | |
gimohd | 6:77a4c45f6416 | 14 | int16_t TemperatureSensor::temp_short(void) |
gimohd | 6:77a4c45f6416 | 15 | { |
gimohd | 6:77a4c45f6416 | 16 | //Signed return value |
gimohd | 6:77a4c45f6416 | 17 | int16_t value; |
gimohd | 6:77a4c45f6416 | 18 | |
gimohd | 6:77a4c45f6416 | 19 | //Read the 11-bit raw temperature value |
gimohd | 6:77a4c45f6416 | 20 | value = this->read16(0x00) >> 5; |
gimohd | 6:77a4c45f6416 | 21 | |
gimohd | 6:77a4c45f6416 | 22 | //Sign extend negative numbers |
gimohd | 6:77a4c45f6416 | 23 | if (value & (1 << 10)) |
gimohd | 6:77a4c45f6416 | 24 | value |= 0xFC00; |
gimohd | 6:77a4c45f6416 | 25 | |
gimohd | 6:77a4c45f6416 | 26 | //Return the temperature in °C |
gimohd | 6:77a4c45f6416 | 27 | return value; |
gimohd | 6:77a4c45f6416 | 28 | } |
gimohd | 6:77a4c45f6416 | 29 | |
gimohd | 6:77a4c45f6416 | 30 | float TemperatureSensor::shortToFloat(int16_t value) |
gimohd | 6:77a4c45f6416 | 31 | { |
gimohd | 6:77a4c45f6416 | 32 | return value*0.125; |
gimohd | 6:77a4c45f6416 | 33 | } |
gimohd | 6:77a4c45f6416 | 34 | |
gimohd | 6:77a4c45f6416 | 35 | float TemperatureSensor::average(std::vector<int16_t> TMP) |
gimohd | 6:77a4c45f6416 | 36 | { |
gimohd | 6:77a4c45f6416 | 37 | printf("test \r\n"); |
gimohd | 6:77a4c45f6416 | 38 | float sum = 0; |
gimohd | 6:77a4c45f6416 | 39 | for( int i = 0; i < TMP.size(); i++ ) { |
gimohd | 6:77a4c45f6416 | 40 | float data = TemperatureSensor::shortToFloat(TMP.at(i)); |
gimohd | 6:77a4c45f6416 | 41 | sum +=data; |
gimohd | 6:77a4c45f6416 | 42 | } |
gimohd | 6:77a4c45f6416 | 43 | printf("Temperature average %f \r\n",(float) sum /TMP.size()); |
gimohd | 6:77a4c45f6416 | 44 | return ((float)sum) /TMP.size(); |
gimohd | 6:77a4c45f6416 | 45 | } |