BMS_T2
Dependencies: INA226
max6675.cpp@2:3bbbe439ec11, 2020-10-13 (annotated)
- Committer:
- takuma1
- Date:
- Tue Oct 13 06:26:34 2020 +0000
- Revision:
- 2:3bbbe439ec11
BMS_NO_CAN;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuma1 | 2:3bbbe439ec11 | 1 | |
takuma1 | 2:3bbbe439ec11 | 2 | #include <mbed.h> |
takuma1 | 2:3bbbe439ec11 | 3 | #include "max6675.h" |
takuma1 | 2:3bbbe439ec11 | 4 | |
takuma1 | 2:3bbbe439ec11 | 5 | max6675::max6675(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) { |
takuma1 | 2:3bbbe439ec11 | 6 | |
takuma1 | 2:3bbbe439ec11 | 7 | } |
takuma1 | 2:3bbbe439ec11 | 8 | |
takuma1 | 2:3bbbe439ec11 | 9 | float max6675::read_temp() { |
takuma1 | 2:3bbbe439ec11 | 10 | short value = 0; |
takuma1 | 2:3bbbe439ec11 | 11 | float temp = 0; |
takuma1 | 2:3bbbe439ec11 | 12 | |
takuma1 | 2:3bbbe439ec11 | 13 | uint8_t highByte=0; |
takuma1 | 2:3bbbe439ec11 | 14 | uint8_t lowByte=0; |
takuma1 | 2:3bbbe439ec11 | 15 | |
takuma1 | 2:3bbbe439ec11 | 16 | select(); |
takuma1 | 2:3bbbe439ec11 | 17 | wait(.25); //This delay is needed else it does'nt seem to update the temp |
takuma1 | 2:3bbbe439ec11 | 18 | |
takuma1 | 2:3bbbe439ec11 | 19 | highByte = spi.write(0); |
takuma1 | 2:3bbbe439ec11 | 20 | lowByte = spi.write(0); |
takuma1 | 2:3bbbe439ec11 | 21 | deselect(); |
takuma1 | 2:3bbbe439ec11 | 22 | |
takuma1 | 2:3bbbe439ec11 | 23 | |
takuma1 | 2:3bbbe439ec11 | 24 | if (lowByte & (1<<2)) { |
takuma1 | 2:3bbbe439ec11 | 25 | error("No Probe"); |
takuma1 | 2:3bbbe439ec11 | 26 | } else { |
takuma1 | 2:3bbbe439ec11 | 27 | value = (highByte << 5 | lowByte>>3); |
takuma1 | 2:3bbbe439ec11 | 28 | } |
takuma1 | 2:3bbbe439ec11 | 29 | |
takuma1 | 2:3bbbe439ec11 | 30 | temp = (value*0.25); // Multiply the value by 0.25 to get temp in ˚C or |
takuma1 | 2:3bbbe439ec11 | 31 | // * (9.0/5.0)) + 32.0; // Convert value to ˚F (ensure proper floats!) |
takuma1 | 2:3bbbe439ec11 | 32 | |
takuma1 | 2:3bbbe439ec11 | 33 | return temp; |
takuma1 | 2:3bbbe439ec11 | 34 | } |
takuma1 | 2:3bbbe439ec11 | 35 | |
takuma1 | 2:3bbbe439ec11 | 36 | void max6675::select() { |
takuma1 | 2:3bbbe439ec11 | 37 | ncs = 0; |
takuma1 | 2:3bbbe439ec11 | 38 | } |
takuma1 | 2:3bbbe439ec11 | 39 | |
takuma1 | 2:3bbbe439ec11 | 40 | void max6675::deselect() { |
takuma1 | 2:3bbbe439ec11 | 41 | ncs = 1; |
takuma1 | 2:3bbbe439ec11 | 42 | } |