EVGENIY BREYNER
/
FROST
ddd
main.cpp@0:9c03bf9526c4, 2019-12-11 (annotated)
- Committer:
- zbr2006
- Date:
- Wed Dec 11 13:58:25 2019 +0000
- Revision:
- 0:9c03bf9526c4
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zbr2006 | 0:9c03bf9526c4 | 1 | #include "mbed.h" |
zbr2006 | 0:9c03bf9526c4 | 2 | #include "DS1820.h" |
zbr2006 | 0:9c03bf9526c4 | 3 | |
zbr2006 | 0:9c03bf9526c4 | 4 | Serial pc(USBTX, USBRX); |
zbr2006 | 0:9c03bf9526c4 | 5 | DigitalOut led(LED1); |
zbr2006 | 0:9c03bf9526c4 | 6 | DS1820 ds1820(D8); // substitute D8 with actual mbed pin name connected to 1-wire bus |
zbr2006 | 0:9c03bf9526c4 | 7 | float temp = 0; |
zbr2006 | 0:9c03bf9526c4 | 8 | int result = 0; |
zbr2006 | 0:9c03bf9526c4 | 9 | |
zbr2006 | 0:9c03bf9526c4 | 10 | int main() |
zbr2006 | 0:9c03bf9526c4 | 11 | { |
zbr2006 | 0:9c03bf9526c4 | 12 | pc.printf("\r\n--Starting--\r\n"); |
zbr2006 | 0:9c03bf9526c4 | 13 | if (ds1820.begin()) { |
zbr2006 | 0:9c03bf9526c4 | 14 | while (1) { |
zbr2006 | 0:9c03bf9526c4 | 15 | ds1820.startConversion(); // start temperature conversion from analog to digital |
zbr2006 | 0:9c03bf9526c4 | 16 | wait(1.0); // let DS1820 complete the temperature conversion |
zbr2006 | 0:9c03bf9526c4 | 17 | result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
zbr2006 | 0:9c03bf9526c4 | 18 | switch (result) { |
zbr2006 | 0:9c03bf9526c4 | 19 | case 0: // no errors -> 'temp' contains the value of measured temperature |
zbr2006 | 0:9c03bf9526c4 | 20 | pc.printf("temp = %3.1f%cC\r\n", temp, 176); |
zbr2006 | 0:9c03bf9526c4 | 21 | break; |
zbr2006 | 0:9c03bf9526c4 | 22 | |
zbr2006 | 0:9c03bf9526c4 | 23 | case 1: // no sensor present -> 'temp' is not updated |
zbr2006 | 0:9c03bf9526c4 | 24 | pc.printf("no sensor present\n\r"); |
zbr2006 | 0:9c03bf9526c4 | 25 | break; |
zbr2006 | 0:9c03bf9526c4 | 26 | |
zbr2006 | 0:9c03bf9526c4 | 27 | case 2: // CRC error -> 'temp' is not updated |
zbr2006 | 0:9c03bf9526c4 | 28 | pc.printf("CRC error\r\n"); |
zbr2006 | 0:9c03bf9526c4 | 29 | } |
zbr2006 | 0:9c03bf9526c4 | 30 | |
zbr2006 | 0:9c03bf9526c4 | 31 | led = !led; |
zbr2006 | 0:9c03bf9526c4 | 32 | } |
zbr2006 | 0:9c03bf9526c4 | 33 | } |
zbr2006 | 0:9c03bf9526c4 | 34 | else |
zbr2006 | 0:9c03bf9526c4 | 35 | pc.printf("No DS1820 sensor found!\r\n"); |
zbr2006 | 0:9c03bf9526c4 | 36 | } |