ze

Dependencies:   mbed WakeUp OneWire

Committer:
mangalho_eanes
Date:
Sun Nov 22 20:11:11 2020 +0000
Revision:
2:15f2d7d650bb
Commit message (required)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mangalho_eanes 2:15f2d7d650bb 1 #ifndef DS1820_H_
mangalho_eanes 2:15f2d7d650bb 2 #define DS1820_H_
mangalho_eanes 2:15f2d7d650bb 3
mangalho_eanes 2:15f2d7d650bb 4 #include <OneWire.h>
mangalho_eanes 2:15f2d7d650bb 5
mangalho_eanes 2:15f2d7d650bb 6 /**
mangalho_eanes 2:15f2d7d650bb 7 * Dallas' DS1820 family temperature sensor.
mangalho_eanes 2:15f2d7d650bb 8 * This library depends on the OneWire library (Dallas' 1-Wire bus protocol implementation)
mangalho_eanes 2:15f2d7d650bb 9 * available at <http://developer.mbed.org/users/hudakz/code/OneWire/>
mangalho_eanes 2:15f2d7d650bb 10 *
mangalho_eanes 2:15f2d7d650bb 11 * Example of use:
mangalho_eanes 2:15f2d7d650bb 12 *
mangalho_eanes 2:15f2d7d650bb 13 * @code
mangalho_eanes 2:15f2d7d650bb 14 *
mangalho_eanes 2:15f2d7d650bb 15 * Single sensor.
mangalho_eanes 2:15f2d7d650bb 16 *
mangalho_eanes 2:15f2d7d650bb 17 * #include "mbed.h"
mangalho_eanes 2:15f2d7d650bb 18 * #include "DS1820.h"
mangalho_eanes 2:15f2d7d650bb 19 *
mangalho_eanes 2:15f2d7d650bb 20 * Serial pc(USBTX, USBRX);
mangalho_eanes 2:15f2d7d650bb 21 * DigitalOut led(LED1);
mangalho_eanes 2:15f2d7d650bb 22 * DS1820 ds1820(D8); // substitute D8 with actual mbed pin name connected to 1-wire bus
mangalho_eanes 2:15f2d7d650bb 23 * float temp = 0;
mangalho_eanes 2:15f2d7d650bb 24 * int result = 0;
mangalho_eanes 2:15f2d7d650bb 25 *
mangalho_eanes 2:15f2d7d650bb 26 * int main()
mangalho_eanes 2:15f2d7d650bb 27 * {
mangalho_eanes 2:15f2d7d650bb 28 * pc.printf("\r\n--Starting--\r\n");
mangalho_eanes 2:15f2d7d650bb 29 * if (ds1820.begin()) {
mangalho_eanes 2:15f2d7d650bb 30 * while (1) {
mangalho_eanes 2:15f2d7d650bb 31 * ds1820.startConversion(); // start temperature conversion from analog to digital
mangalho_eanes 2:15f2d7d650bb 32 * wait(1.0); // let DS1820 complete the temperature conversion
mangalho_eanes 2:15f2d7d650bb 33 * result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
mangalho_eanes 2:15f2d7d650bb 34 * switch (result) {
mangalho_eanes 2:15f2d7d650bb 35 * case 0: // no errors -> 'temp' contains the value of measured temperature
mangalho_eanes 2:15f2d7d650bb 36 * pc.printf("temp = %3.1f%cC\r\n", temp, 176);
mangalho_eanes 2:15f2d7d650bb 37 * break;
mangalho_eanes 2:15f2d7d650bb 38 *
mangalho_eanes 2:15f2d7d650bb 39 * case 1: // no sensor present -> 'temp' is not updated
mangalho_eanes 2:15f2d7d650bb 40 * pc.printf("no sensor present\n\r");
mangalho_eanes 2:15f2d7d650bb 41 * break;
mangalho_eanes 2:15f2d7d650bb 42 *
mangalho_eanes 2:15f2d7d650bb 43 * case 2: // CRC error -> 'temp' is not updated
mangalho_eanes 2:15f2d7d650bb 44 * pc.printf("CRC error\r\n");
mangalho_eanes 2:15f2d7d650bb 45 * }
mangalho_eanes 2:15f2d7d650bb 46 *
mangalho_eanes 2:15f2d7d650bb 47 * led = !led;
mangalho_eanes 2:15f2d7d650bb 48 * }
mangalho_eanes 2:15f2d7d650bb 49 * }
mangalho_eanes 2:15f2d7d650bb 50 * else
mangalho_eanes 2:15f2d7d650bb 51 * pc.printf("No DS1820 sensor found!\r\n");
mangalho_eanes 2:15f2d7d650bb 52 * }
mangalho_eanes 2:15f2d7d650bb 53 *
mangalho_eanes 2:15f2d7d650bb 54 *
mangalho_eanes 2:15f2d7d650bb 55 * More sensors connected to the same 1-wire bus.
mangalho_eanes 2:15f2d7d650bb 56 *
mangalho_eanes 2:15f2d7d650bb 57 * #include "mbed.h"
mangalho_eanes 2:15f2d7d650bb 58 * #include "DS1820.h"
mangalho_eanes 2:15f2d7d650bb 59 *
mangalho_eanes 2:15f2d7d650bb 60 * #define SENSORS_COUNT 64 // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
mangalho_eanes 2:15f2d7d650bb 61 *
mangalho_eanes 2:15f2d7d650bb 62 * Serial pc(USBTX, USBRX);
mangalho_eanes 2:15f2d7d650bb 63 * DigitalOut led(LED1);
mangalho_eanes 2:15f2d7d650bb 64 * OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin
mangalho_eanes 2:15f2d7d650bb 65 * DS1820* ds1820[SENSORS_COUNT];
mangalho_eanes 2:15f2d7d650bb 66 * int sensors_found = 0; // counts the actually found DS1820 sensors
mangalho_eanes 2:15f2d7d650bb 67 * float temp = 0;
mangalho_eanes 2:15f2d7d650bb 68 * int result = 0;
mangalho_eanes 2:15f2d7d650bb 69 *
mangalho_eanes 2:15f2d7d650bb 70 * int main() {
mangalho_eanes 2:15f2d7d650bb 71 * int i = 0;
mangalho_eanes 2:15f2d7d650bb 72 *
mangalho_eanes 2:15f2d7d650bb 73 * pc.printf("\r\n Starting \r\n");
mangalho_eanes 2:15f2d7d650bb 74 * //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
mangalho_eanes 2:15f2d7d650bb 75 * for(i = 0; i < SENSORS_COUNT; i++) {
mangalho_eanes 2:15f2d7d650bb 76 * ds1820[i] = new DS1820(&oneWire);
mangalho_eanes 2:15f2d7d650bb 77 * if(!ds1820[i]->begin()) {
mangalho_eanes 2:15f2d7d650bb 78 * delete ds1820[i];
mangalho_eanes 2:15f2d7d650bb 79 * break;
mangalho_eanes 2:15f2d7d650bb 80 * }
mangalho_eanes 2:15f2d7d650bb 81 * }
mangalho_eanes 2:15f2d7d650bb 82 *
mangalho_eanes 2:15f2d7d650bb 83 * sensors_found = i;
mangalho_eanes 2:15f2d7d650bb 84 *
mangalho_eanes 2:15f2d7d650bb 85 * if (sensors_found == 0) {
mangalho_eanes 2:15f2d7d650bb 86 * pc.printf("No DS1820 sensor found!\r\n");
mangalho_eanes 2:15f2d7d650bb 87 * return -1;
mangalho_eanes 2:15f2d7d650bb 88 * }
mangalho_eanes 2:15f2d7d650bb 89 * else
mangalho_eanes 2:15f2d7d650bb 90 * pc.printf("Found %d sensors.\r\n", sensors_found);
mangalho_eanes 2:15f2d7d650bb 91 *
mangalho_eanes 2:15f2d7d650bb 92 * while(1) {
mangalho_eanes 2:15f2d7d650bb 93 * pc.printf("-------------------\r\n");
mangalho_eanes 2:15f2d7d650bb 94 * for(i = 0; i < sensors_found; i++)
mangalho_eanes 2:15f2d7d650bb 95 * ds1820[i]->startConversion(); // start temperature conversion from analog to digital
mangalho_eanes 2:15f2d7d650bb 96 * wait(1.0); // let DS1820s complete the temperature conversion
mangalho_eanes 2:15f2d7d650bb 97 * for(int i = 0; i < sensors_found; i++) {
mangalho_eanes 2:15f2d7d650bb 98 * if(ds1820[i]->isPresent())
mangalho_eanes 2:15f2d7d650bb 99 * pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature
mangalho_eanes 2:15f2d7d650bb 100 * }
mangalho_eanes 2:15f2d7d650bb 101 * }
mangalho_eanes 2:15f2d7d650bb 102 * }
mangalho_eanes 2:15f2d7d650bb 103 *
mangalho_eanes 2:15f2d7d650bb 104 * @endcode
mangalho_eanes 2:15f2d7d650bb 105 *
mangalho_eanes 2:15f2d7d650bb 106 * Note: Don't forget to connect a 4.7k Ohm resistor
mangalho_eanes 2:15f2d7d650bb 107 * between the DS1820's data pin and the +3.3V pin
mangalho_eanes 2:15f2d7d650bb 108 *
mangalho_eanes 2:15f2d7d650bb 109 */
mangalho_eanes 2:15f2d7d650bb 110 class DS1820
mangalho_eanes 2:15f2d7d650bb 111 {
mangalho_eanes 2:15f2d7d650bb 112 OneWire *oneWire;
mangalho_eanes 2:15f2d7d650bb 113 bool present;
mangalho_eanes 2:15f2d7d650bb 114 bool model_s;
mangalho_eanes 2:15f2d7d650bb 115 uint8_t data[12];
mangalho_eanes 2:15f2d7d650bb 116 uint8_t addr[8];
mangalho_eanes 2:15f2d7d650bb 117 float toFloat(uint16_t word);
mangalho_eanes 2:15f2d7d650bb 118 static uint8_t lastAddr[8];
mangalho_eanes 2:15f2d7d650bb 119
mangalho_eanes 2:15f2d7d650bb 120 public:
mangalho_eanes 2:15f2d7d650bb 121 DS1820(PinName pin, int sample_point_us = 13);
mangalho_eanes 2:15f2d7d650bb 122 // DS1820(char model, PinName pin);
mangalho_eanes 2:15f2d7d650bb 123 DS1820(OneWire* wire);
mangalho_eanes 2:15f2d7d650bb 124 bool begin(void);
mangalho_eanes 2:15f2d7d650bb 125 bool isPresent();
mangalho_eanes 2:15f2d7d650bb 126 void setResolution(uint8_t res);
mangalho_eanes 2:15f2d7d650bb 127 void startConversion(void);
mangalho_eanes 2:15f2d7d650bb 128 float read(void);
mangalho_eanes 2:15f2d7d650bb 129 uint8_t read(float& temp);
mangalho_eanes 2:15f2d7d650bb 130 };
mangalho_eanes 2:15f2d7d650bb 131 #endif /* DS1820_H_ */
mangalho_eanes 2:15f2d7d650bb 132