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

Dependencies:   DS1820

Committer:
hudakz
Date:
Fri Mar 27 12:37:34 2015 +0000
Revision:
3:eb195375cde7
Parent:
2:a680194ce6df
Child:
4:d263a2860484
Function isPresent() added.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:77a366f9ba45 1 /*
hudakz 0:77a366f9ba45 2 * Simple DS1820 sensor demo
hudakz 1:fe12bf2ad337 3 *
hudakz 1:fe12bf2ad337 4 * Note: Don't forget to connect a 4.7k Ohm resistor
hudakz 1:fe12bf2ad337 5 * between the DS1820's data pin and the +3.3V pin
hudakz 1:fe12bf2ad337 6 *
hudakz 0:77a366f9ba45 7 */
hudakz 0:77a366f9ba45 8
hudakz 0:77a366f9ba45 9 #include "mbed.h"
hudakz 0:77a366f9ba45 10 #include "DS1820.h"
hudakz 0:77a366f9ba45 11
hudakz 0:77a366f9ba45 12 Serial serial(USBTX, USBRX);
hudakz 1:fe12bf2ad337 13
hudakz 0:77a366f9ba45 14 int main() {
hudakz 1:fe12bf2ad337 15 DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
hudakz 1:fe12bf2ad337 16
hudakz 0:77a366f9ba45 17 if(ds1820.begin()) {
hudakz 2:a680194ce6df 18 ds1820.startConversion(); // start temperature conversion
hudakz 2:a680194ce6df 19 wait(1.0); // let DS1820 complete the temperature conversion
hudakz 0:77a366f9ba45 20 while(1) {
hudakz 1:fe12bf2ad337 21 serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature
hudakz 1:fe12bf2ad337 22 ds1820.startConversion(); // start temperature conversion
hudakz 1:fe12bf2ad337 23 wait(1.0); // let DS1820 complete the temperature conversion
hudakz 0:77a366f9ba45 24 }
hudakz 0:77a366f9ba45 25 } else
hudakz 0:77a366f9ba45 26 serial.printf("No DS1820 sensor found!\r\n");
hudakz 0:77a366f9ba45 27 }
hudakz 1:fe12bf2ad337 28
hudakz 1:fe12bf2ad337 29
hudakz 1:fe12bf2ad337 30 // You can create an array of sensors:
hudakz 1:fe12bf2ad337 31
hudakz 1:fe12bf2ad337 32 //int main() {
hudakz 1:fe12bf2ad337 33 // DS1820 ds1820[3] = {DS1820(PA_8), DS1820(PA_9), DS1820(PC_7)};
hudakz 1:fe12bf2ad337 34 //
hudakz 1:fe12bf2ad337 35 // for(int i = 0; i < 3; i++) {
hudakz 1:fe12bf2ad337 36 // if(!ds1820[i].begin()) {
hudakz 3:eb195375cde7 37 // serial.printf("Cannot find sensor %d", i);
hudakz 1:fe12bf2ad337 38 // } else
hudakz 1:fe12bf2ad337 39 // ds1820[i].startConversion();
hudakz 2:a680194ce6df 40 // }
hudakz 2:a680194ce6df 41 // wait(1.0); // let DS1820s complete the temperature conversion
hudakz 1:fe12bf2ad337 42 // while(1) {
hudakz 1:fe12bf2ad337 43 // for(int i = 0; i < 3; i++) {
hudakz 3:eb195375cde7 44 // if(ds1820[i].isPresent() {
hudakz 2:a680194ce6df 45 // serial.printf("temp%d = %3.1f\r\n", i, ds1820[i].read()); // read temperature
hudakz 2:a680194ce6df 46 // ds1820[i].startConversion(); // start temperature conversion
hudakz 2:a680194ce6df 47 // }
hudakz 1:fe12bf2ad337 48 // }
hudakz 1:fe12bf2ad337 49 // wait(1.0); // let DS1820s complete the temperature conversion
hudakz 1:fe12bf2ad337 50 // }
hudakz 1:fe12bf2ad337 51 //}
hudakz 3:eb195375cde7 52 //
hudakz 1:fe12bf2ad337 53
hudakz 1:fe12bf2ad337 54
hudakz 1:fe12bf2ad337 55
hudakz 1:fe12bf2ad337 56