Demo for reading DS1820 temperature sensor with STM32F103C8T6 board.

Dependencies:   mbed DS1820

Reading DS1820 temperature sensor with STM32F103C8T6 board

Import the program into your online compiler.

Schematic

/media/uploads/hudakz/stm32f103c8t6_ds1820_04.png

Wiring

STM32F103C8T6DS1820
GND<=>GND
PA_9<=>DQ
3.3V<=>VDD
Committer:
hudakz
Date:
Tue Feb 05 13:10:49 2019 +0000
Revision:
3:99631e445ec4
Parent:
2:171926fbefb3
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:ce867244a530 1 /*
hudakz 0:ce867244a530 2 * DS1820 sensor demo
hudakz 0:ce867244a530 3 *
hudakz 0:ce867244a530 4 * Note: Don't forget to connect a 4.7k Ohm resistor
hudakz 0:ce867244a530 5 * between the DS1820's data pin and the +3.3V pin
hudakz 0:ce867244a530 6 *
hudakz 0:ce867244a530 7 */
hudakz 0:ce867244a530 8 #include "mbed.h"
hudakz 0:ce867244a530 9 #include "DS1820.h"
hudakz 0:ce867244a530 10
hudakz 2:171926fbefb3 11 Serial pc(PA_2, PA_3);
hudakz 3:99631e445ec4 12 DigitalOut led(PC_13);
hudakz 2:171926fbefb3 13 DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
hudakz 2:171926fbefb3 14 float temp = 0;
hudakz 2:171926fbefb3 15
hudakz 2:171926fbefb3 16 int main()
hudakz 2:171926fbefb3 17 {
hudakz 3:99631e445ec4 18 int error = 0;
hudakz 0:ce867244a530 19
hudakz 0:ce867244a530 20 if(ds1820.begin()) {
hudakz 0:ce867244a530 21 while(1) {
hudakz 1:93ec3038c226 22 ds1820.startConversion(); // start temperature conversion from analog to digital
hudakz 1:93ec3038c226 23 wait(1.0); // let DS1820 complete the temperature conversion
hudakz 0:ce867244a530 24 error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
hudakz 0:ce867244a530 25 switch(error) {
hudakz 0:ce867244a530 26 case 0: // no errors -> 'temp' contains the value of measured temperature
hudakz 0:ce867244a530 27 pc.printf("temp = %3.1f%cC\r\n", temp, 176);
hudakz 0:ce867244a530 28 break;
hudakz 0:ce867244a530 29 case 1: // no sensor present -> 'temp' is not updated
hudakz 0:ce867244a530 30 pc.printf("no sensor present\n\r");
hudakz 0:ce867244a530 31 break;
hudakz 0:ce867244a530 32 case 2: // CRC error -> 'temp' is not updated
hudakz 0:ce867244a530 33 pc.printf("CRC error\r\n");
hudakz 0:ce867244a530 34 }
hudakz 1:93ec3038c226 35 led = !led;
hudakz 0:ce867244a530 36 }
hudakz 0:ce867244a530 37 } else
hudakz 0:ce867244a530 38 pc.printf("No DS1820 sensor found!\r\n");
hudakz 0:ce867244a530 39 }
hudakz 0:ce867244a530 40