speed test for ds1820 sensors

Dependencies:   DS1820 mbed-rtos mbed

Committer:
bdk9
Date:
Tue Nov 29 17:24:12 2016 +0000
Revision:
0:f5e0d17a17d1
published

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bdk9 0:f5e0d17a17d1 1 #include "mbed.h"
bdk9 0:f5e0d17a17d1 2 #include "DS1820.h"
bdk9 0:f5e0d17a17d1 3
bdk9 0:f5e0d17a17d1 4 #define NUM_DS1820 3
bdk9 0:f5e0d17a17d1 5 #define PIN_DS1820 D2
bdk9 0:f5e0d17a17d1 6
bdk9 0:f5e0d17a17d1 7 // DEVICES
bdk9 0:f5e0d17a17d1 8 double temperatures[NUM_DS1820];
bdk9 0:f5e0d17a17d1 9 DS1820* thermometers[NUM_DS1820];
bdk9 0:f5e0d17a17d1 10 Serial pc(USBTX, USBRX);
bdk9 0:f5e0d17a17d1 11
bdk9 0:f5e0d17a17d1 12 // Discover DS1820 probes on pin defined by PIN_DS1820
bdk9 0:f5e0d17a17d1 13 void ds1820_init() {
bdk9 0:f5e0d17a17d1 14 // Initialize the thermometer array to DS1820 objects
bdk9 0:f5e0d17a17d1 15 int num_devices = 0;
bdk9 0:f5e0d17a17d1 16 while(DS1820::unassignedProbe(PIN_DS1820)) {
bdk9 0:f5e0d17a17d1 17 thermometers[num_devices] = new DS1820(PIN_DS1820);
bdk9 0:f5e0d17a17d1 18 num_devices++;
bdk9 0:f5e0d17a17d1 19 if (num_devices == NUM_DS1820)
bdk9 0:f5e0d17a17d1 20 break;
bdk9 0:f5e0d17a17d1 21 }
bdk9 0:f5e0d17a17d1 22 pc.printf("Found %d device(s)\r\n\n", num_devices);
bdk9 0:f5e0d17a17d1 23 }
bdk9 0:f5e0d17a17d1 24
bdk9 0:f5e0d17a17d1 25 void read_temps() {
bdk9 0:f5e0d17a17d1 26 double temp;
bdk9 0:f5e0d17a17d1 27 thermometers[0]->convertTemperature(false, DS1820::all_devices);
bdk9 0:f5e0d17a17d1 28 for (int i=0; i<NUM_DS1820; i++) {
bdk9 0:f5e0d17a17d1 29 temp = (double) thermometers[i]->temperature();
bdk9 0:f5e0d17a17d1 30 temperatures[i] = temp;
bdk9 0:f5e0d17a17d1 31 }
bdk9 0:f5e0d17a17d1 32 }
bdk9 0:f5e0d17a17d1 33 int main()
bdk9 0:f5e0d17a17d1 34 {
bdk9 0:f5e0d17a17d1 35 ds1820_init();
bdk9 0:f5e0d17a17d1 36 pc.printf("HELLOP");
bdk9 0:f5e0d17a17d1 37 while(1) {
bdk9 0:f5e0d17a17d1 38 read_temps();
bdk9 0:f5e0d17a17d1 39 pc.printf("%.3f %.3f %.3f\n" , temperatures[0], temperatures[1], temperatures[2]);
bdk9 0:f5e0d17a17d1 40 }
bdk9 0:f5e0d17a17d1 41 }