Demo for reading DS1820 temperature sensor with STM32F103C8T6 board.

Dependencies:   mbed DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * DS1820 sensor demo
00003  *
00004  * Note: Don't forget to connect a 4.7k Ohm resistor 
00005  *       between the DS1820's data pin and the +3.3V pin
00006  *
00007  */
00008 #include "mbed.h"
00009 #include "DS1820.h"
00010   
00011 Serial      pc(PA_2, PA_3);
00012 DigitalOut  led(PC_13);   
00013 DS1820      ds1820(PA_9);    // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin                             
00014 float       temp = 0;
00015 
00016 int main() 
00017 {                          
00018     int  error = 0; 
00019     
00020     if(ds1820.begin()) {
00021         while(1) {
00022             ds1820.startConversion();  // start temperature conversion from analog to digital
00023             wait(1.0);                 // let DS1820 complete the temperature conversion 
00024             error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
00025             switch(error) {
00026             case 0:    // no errors -> 'temp' contains the value of measured temperature
00027                 pc.printf("temp = %3.1f%cC\r\n", temp, 176);
00028                 break;
00029             case 1:    // no sensor present -> 'temp' is not updated
00030                 pc.printf("no sensor present\n\r");
00031                 break;
00032             case 2:    // CRC error -> 'temp' is not updated
00033                 pc.printf("CRC error\r\n");
00034             } 
00035             led = !led;
00036         }
00037     } else
00038         pc.printf("No DS1820 sensor found!\r\n");
00039 }
00040