Coffeemaker controller

Dependencies:   mbed OneWire

Committer:
jvfausto
Date:
Sat Sep 26 00:30:09 2020 +0000
Revision:
0:17c5e4f9a805
Coffeemaker code

Who changed what in which revision?

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