Librería para leer temperatura haciendo comunicación SPI con el integrado MAX6675
max6675.cpp@0:33fb492d139e, 2020-06-08 (annotated)
- Committer:
- cristian_junca
- Date:
- Mon Jun 08 23:18:38 2020 +0000
- Revision:
- 0:33fb492d139e
Read temperatura with SPI communication
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cristian_junca | 0:33fb492d139e | 1 | #include "mbed.h" |
cristian_junca | 0:33fb492d139e | 2 | #include "max6675.h" |
cristian_junca | 0:33fb492d139e | 3 | |
cristian_junca | 0:33fb492d139e | 4 | max6675::max6675(SPI& _spi, PinName _ss) : spi(_spi), ss(_ss) { |
cristian_junca | 0:33fb492d139e | 5 | |
cristian_junca | 0:33fb492d139e | 6 | } |
cristian_junca | 0:33fb492d139e | 7 | |
cristian_junca | 0:33fb492d139e | 8 | float max6675::read_temp() { |
cristian_junca | 0:33fb492d139e | 9 | short value = 0; |
cristian_junca | 0:33fb492d139e | 10 | float temp = 0; |
cristian_junca | 0:33fb492d139e | 11 | |
cristian_junca | 0:33fb492d139e | 12 | uint8_t highByte=0; |
cristian_junca | 0:33fb492d139e | 13 | uint8_t lowByte=0; |
cristian_junca | 0:33fb492d139e | 14 | |
cristian_junca | 0:33fb492d139e | 15 | select(); |
cristian_junca | 0:33fb492d139e | 16 | wait(.25); //This delay is needed else it doesn't seem to update the temp |
cristian_junca | 0:33fb492d139e | 17 | |
cristian_junca | 0:33fb492d139e | 18 | highByte = spi.write(0); |
cristian_junca | 0:33fb492d139e | 19 | lowByte = spi.write(0); |
cristian_junca | 0:33fb492d139e | 20 | deselect(); |
cristian_junca | 0:33fb492d139e | 21 | |
cristian_junca | 0:33fb492d139e | 22 | if (lowByte & (1<<2)) { |
cristian_junca | 0:33fb492d139e | 23 | error("No Probe"); |
cristian_junca | 0:33fb492d139e | 24 | } else { |
cristian_junca | 0:33fb492d139e | 25 | value = (highByte << 5 | lowByte>>3); |
cristian_junca | 0:33fb492d139e | 26 | } |
cristian_junca | 0:33fb492d139e | 27 | |
cristian_junca | 0:33fb492d139e | 28 | temp = (value*0.25); // Multiply the value by 0.25 to get temp in ˚C or |
cristian_junca | 0:33fb492d139e | 29 | // * (9.0/5.0)) + 32.0; // Convert value to ˚F (ensure proper floats!) |
cristian_junca | 0:33fb492d139e | 30 | |
cristian_junca | 0:33fb492d139e | 31 | return temp; |
cristian_junca | 0:33fb492d139e | 32 | } |
cristian_junca | 0:33fb492d139e | 33 | |
cristian_junca | 0:33fb492d139e | 34 | void max6675::select() { |
cristian_junca | 0:33fb492d139e | 35 | ss = 0; |
cristian_junca | 0:33fb492d139e | 36 | } |
cristian_junca | 0:33fb492d139e | 37 | |
cristian_junca | 0:33fb492d139e | 38 | void max6675::deselect() { |
cristian_junca | 0:33fb492d139e | 39 | ss = 1; |
cristian_junca | 0:33fb492d139e | 40 | } |