Simple DS1820 sensor demo showing how to use the DS1820 library [https://developer.mbed.org/users/hudakz/code/DS1820/]

Dependencies:   DS1820

main.cpp

Committer:
hudakz
Date:
2019-01-27
Revision:
7:8beaacebc82d
Parent:
6:d68db387a734
Child:
9:1d1c57840c7e

File content as of revision 7:8beaacebc82d:

/*
 * Simple DS1820 sensor demo
 *
 * Note: Don't forget to connect a 4.7k Ohm resistor 
 *       between the DS1820's data pin and the +3.3V pin
 *
 */
#include "mbed.h"
#include "DS1820.h"

Serial      pc(USBTX, USBRX);
DigitalOut  led(LED1);
DS1820      ds1820(p8);  // substitute p8 with actual mbed pin name connected to the DS1820 data pin
float       temp = 0;
int         result = 0;

int main()
{
    if (ds1820.begin()) {
        while (1) {
            ds1820.startConversion();   // start temperature conversion from analog to digital
            wait(1.0);                  // let DS1820 complete the temperature conversion
            result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
            switch (result) {
                case 0:                 // no errors -> 'temp' contains the value of measured temperature
                    pc.printf("temp = %3.1f%cC\r\n", temp, 176);
                    break;

                case 1:                 // no sensor present -> 'temp' is not updated
                    pc.printf("no sensor present\n\r");
                    break;

                case 2:                 // CRC error -> 'temp' is not updated
                    pc.printf("CRC error\r\n");
            }

            led = !led;
        }
    }
    else
        pc.printf("No DS1820 sensor found!\r\n");
}

// Array of sensors:
//int main() {
//    DS1820  ds1820[3] = {DS1820(PA_8), DS1820(PA_9), DS1820(PC_7)};
//
//    for(int i = 0; i < 3; i++) {
//        if(!ds1820[i].begin()) {
//            serial.printf("Cannot find sensor %d", i);
//        } else
//            ds1820[i].startConversion();
//    }
//    wait(1.0);  // let DS1820s complete the temperature conversion
//    while(1) {
//        for(int i = 0; i < 3; i++) {
//            if(ds1820[i].isPresent() {
//                serial.printf("temp%d = %3.1f\r\n", i, ds1820[i].read());     // read temperature
//                ds1820[i].startConversion();     // start temperature conversion
//            }
//        }
//        wait(1.0);  // let DS1820s complete the temperature conversion
//    }
//}
//