javier vicente
/
MASTER_ONEWIRE_DS1820
ds18b20 work for ST Nucleo
Diff: main.cpp
- Revision:
- 2:ffa66d4cd7cf
- Parent:
- 1:10bbb8a13329
- Child:
- 3:5055285819f7
--- a/main.cpp Thu Mar 27 17:26:59 2014 +0000 +++ b/main.cpp Wed Dec 02 19:26:14 2020 +0000 @@ -1,38 +1,40 @@ +/* + * 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(SERIAL_TX, SERIAL_RX); - -const int MAX_PROBES = 3; -DS1820 probe[3] = {D2, D2, D2}; - - -int main() -{ - int i; - int devices_found=0; - - //pc.attach(&pcRx, pc.RxIrq); - pc.printf("Started\r\n"); +DigitalOut led(LED1); +DS1820 ds1820(D2); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin +float temp = 0; - pc.printf("search_ROM_setup\r\n"); - probe[0].search_ROM_setup(); - pc.printf("search_ROM\r\n"); - while (probe[devices_found].search_ROM() and devices_found<MAX_PROBES-1) - devices_found++; +int main() +{ + int error = 0; - // If maximum number of probes are found, - // bump the counter to include the last array entry - if (probe[devices_found].ROM[0] != 0xFF) - devices_found++; - pc.printf("devices found:%d\r\n", devices_found); - - - while(1) { - probe[0].convert_temperature(DS1820::all_devices); - for (i=0; i<devices_found; i++) { - pc.printf("Device[%d]: %3.1f \r\n",i, probe[i].temperature('c')); + if(ds1820.begin()) { + while(1) { + ds1820.startConversion(); // start temperature conversion from analog to digital + wait(1.0); // let DS1820 complete the temperature conversion + error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) + switch(error) { + case 0: // no errors -> 'temp' contains the value of measured temperature + pc.printf("temp = %3.1f C\r\n", temp); + 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; } - wait(1.0); // 1 sec - } + } else + pc.printf("No DS1820 sensor found!\r\n"); } + \ No newline at end of file