![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
DS1820
Dependencies: BufferedSerial DS1820
main.cpp@0:08aac9dc9662, 2020-09-21 (annotated)
- Committer:
- romanbg
- Date:
- Mon Sep 21 23:11:07 2020 +0000
- Revision:
- 0:08aac9dc9662
DS1820
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
romanbg | 0:08aac9dc9662 | 1 | /* |
romanbg | 0:08aac9dc9662 | 2 | * DS1820 sensor demo |
romanbg | 0:08aac9dc9662 | 3 | * |
romanbg | 0:08aac9dc9662 | 4 | * Note: Don't forget to connect a 4.7k Ohm resistor |
romanbg | 0:08aac9dc9662 | 5 | * between the DS1820's data pin and the +3.3V pin |
romanbg | 0:08aac9dc9662 | 6 | * |
romanbg | 0:08aac9dc9662 | 7 | */ |
romanbg | 0:08aac9dc9662 | 8 | #include "mbed.h" |
romanbg | 0:08aac9dc9662 | 9 | #include "BufferedSerial.h" |
romanbg | 0:08aac9dc9662 | 10 | #include "DS1820.h" |
romanbg | 0:08aac9dc9662 | 11 | |
romanbg | 0:08aac9dc9662 | 12 | |
romanbg | 0:08aac9dc9662 | 13 | BufferedSerial pc(PA_9, PA_10); |
romanbg | 0:08aac9dc9662 | 14 | |
romanbg | 0:08aac9dc9662 | 15 | |
romanbg | 0:08aac9dc9662 | 16 | DigitalOut led(PC_13); |
romanbg | 0:08aac9dc9662 | 17 | DS1820 ds1820(PA_6); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin |
romanbg | 0:08aac9dc9662 | 18 | float temp = 0; |
romanbg | 0:08aac9dc9662 | 19 | |
romanbg | 0:08aac9dc9662 | 20 | int main() |
romanbg | 0:08aac9dc9662 | 21 | { |
romanbg | 0:08aac9dc9662 | 22 | int error = 0; |
romanbg | 0:08aac9dc9662 | 23 | |
romanbg | 0:08aac9dc9662 | 24 | if(ds1820.begin()) { |
romanbg | 0:08aac9dc9662 | 25 | while(1) { |
romanbg | 0:08aac9dc9662 | 26 | ds1820.startConversion(); // start temperature conversion from analog to digital |
romanbg | 0:08aac9dc9662 | 27 | ThisThread::sleep_for(1000); // let DS1820 complete the temperature conversion |
romanbg | 0:08aac9dc9662 | 28 | error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
romanbg | 0:08aac9dc9662 | 29 | switch(error) { |
romanbg | 0:08aac9dc9662 | 30 | case 0: // no errors -> 'temp' contains the value of measured temperature |
romanbg | 0:08aac9dc9662 | 31 | pc.printf("temp = %3.1f%cC\r\n", temp, 176); |
romanbg | 0:08aac9dc9662 | 32 | break; |
romanbg | 0:08aac9dc9662 | 33 | case 1: // no sensor present -> 'temp' is not updated |
romanbg | 0:08aac9dc9662 | 34 | pc.printf("no sensor present\n\r"); |
romanbg | 0:08aac9dc9662 | 35 | break; |
romanbg | 0:08aac9dc9662 | 36 | case 2: // CRC error -> 'temp' is not updated |
romanbg | 0:08aac9dc9662 | 37 | pc.printf("CRC error\r\n"); |
romanbg | 0:08aac9dc9662 | 38 | } |
romanbg | 0:08aac9dc9662 | 39 | led = !led; |
romanbg | 0:08aac9dc9662 | 40 | } |
romanbg | 0:08aac9dc9662 | 41 | } else |
romanbg | 0:08aac9dc9662 | 42 | printf("No DS1820 sensor found!\r\n"); |
romanbg | 0:08aac9dc9662 | 43 | } |