EVGENIY BREYNER / Mbed 2 deprecated FROST

Dependencies:   mbed DS1820

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  #include "DS1820.h"
00003  
00004  Serial      pc(USBTX, USBRX);
00005  DigitalOut  led(LED1);
00006  DS1820      ds1820(D8);  // substitute D8 with actual mbed pin name connected to 1-wire bus
00007  float       temp = 0;
00008  int         result = 0;
00009  
00010  int main()
00011  {
00012      pc.printf("\r\n--Starting--\r\n");
00013      if (ds1820.begin()) {
00014          while (1) {
00015              ds1820.startConversion();   // start temperature conversion from analog to digital
00016              wait(1.0);                  // let DS1820 complete the temperature conversion
00017              result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
00018              switch (result) {
00019                  case 0:                 // no errors -> 'temp' contains the value of measured temperature
00020                      pc.printf("temp = %3.1f%cC\r\n", temp, 176);
00021                      break;
00022  
00023                  case 1:                 // no sensor present -> 'temp' is not updated
00024                      pc.printf("no sensor present\n\r");
00025                      break;
00026  
00027                  case 2:                 // CRC error -> 'temp' is not updated
00028                      pc.printf("CRC error\r\n");
00029              }
00030  
00031              led = !led;
00032          }
00033      }
00034      else
00035          pc.printf("No DS1820 sensor found!\r\n");
00036  }