Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 9:1d1c57840c7e, committed 2019-03-10
- Comitter:
- hudakz
- Date:
- Sun Mar 10 13:20:39 2019 +0000
- Parent:
- 8:6373abe686d2
- Child:
- 10:739d1a7a4b1b
- Commit message:
- Several DS1820 sensors connected to the same 1-wire bus.
Changed in this revision
--- a/DS1820.lib Sun Jan 27 18:03:28 2019 +0000 +++ b/DS1820.lib Sun Mar 10 13:20:39 2019 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/hudakz/code/DS1820/#74d2a4ac7f32 +http://developer.mbed.org/users/hudakz/code/DS1820/#98c261bcb399
--- a/main.cpp Sun Jan 27 18:03:28 2019 +0000
+++ b/main.cpp Sun Mar 10 13:20:39 2019 +0000
@@ -5,17 +5,20 @@
* between the DS1820's data pin and the +3.3V pin
*
*/
+/* Single sensor: */
+/*
#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
+OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected 1-wire bus
float temp = 0;
int result = 0;
int main()
{
+ pc.printf("\r\n--Starting--\r\n");
if (ds1820.begin()) {
while (1) {
ds1820.startConversion(); // start temperature conversion from analog to digital
@@ -40,26 +43,54 @@
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
-// }
-//}
-//
+/*Several sensors connected to the same 1-wire bus:*/
+
+#include "mbed.h"
+#include "DS1820.h"
+
+#define SENSORS_COUNT 64 // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
+
+Serial pc(USBTX, USBRX);
+DigitalOut led(LED1);
+OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected 1-wire bus
+DS1820* ds1820[SENSORS_COUNT];
+int sensors_found = 0; // counts the actually found DS1820 sensors
+float temp = 0;
+int result = 0;
+
+int main() {
+ int i = 0;
+
+ pc.printf("\r\n--Starting--\r\n");
+ //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
+ for(i = 0; i < SENSORS_COUNT; i++) {
+ ds1820[i] = new DS1820(&oneWire);
+ if(!ds1820[i]->begin()) {
+ delete ds1820[i];
+ break;
+ }
+ }
+
+ sensors_found = i;
+
+ if (sensors_found == 0) {
+ pc.printf("No DS1820 sensor found!\r\n");
+ return -1;
+ }
+ else
+ pc.printf("Found %d sensors.\r\n", sensors_found);
+
+ while(1) {
+ pc.printf("-------------------\r\n");
+ for(i = 0; i < sensors_found; i++)
+ ds1820[i]->startConversion(); // start temperature conversion from analog to digital
+ wait(1.0); // let DS1820s complete the temperature conversion
+ for(int i = 0; i < sensors_found; i++) {
+ if(ds1820[i]->isPresent())
+ pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature
+ }
+ }
+}
+
--- a/mbed.bld Sun Jan 27 18:03:28 2019 +0000 +++ b/mbed.bld Sun Mar 10 13:20:39 2019 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file