STM32F103C8T6_DS1820
Dependencies: DS1820 mbed-STM32F103C8T6 mbed
Fork of STM32F103C8T6_DS1820 by
main.cpp@1:93ec3038c226, 2017-01-06 (annotated)
- Committer:
- hudakz
- Date:
- Fri Jan 06 19:27:54 2017 +0000
- Revision:
- 1:93ec3038c226
- Parent:
- 0:ce867244a530
Updated.
Who changed what in which revision?
User | Revision | Line number | New 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 "stm32f103c8t6.h" |
hudakz | 0:ce867244a530 | 9 | #include "mbed.h" |
hudakz | 0:ce867244a530 | 10 | #include "DS1820.h" |
hudakz | 0:ce867244a530 | 11 | |
hudakz | 0:ce867244a530 | 12 | int main() { |
hudakz | 0:ce867244a530 | 13 | confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
hudakz | 0:ce867244a530 | 14 | |
hudakz | 0:ce867244a530 | 15 | Serial pc(PA_2, PA_3); |
hudakz | 1:93ec3038c226 | 16 | DigitalOut led(LED1); |
hudakz | 0:ce867244a530 | 17 | DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin |
hudakz | 0:ce867244a530 | 18 | float temp = 0; |
hudakz | 0:ce867244a530 | 19 | int error = 0; |
hudakz | 0:ce867244a530 | 20 | |
hudakz | 0:ce867244a530 | 21 | if(ds1820.begin()) { |
hudakz | 0:ce867244a530 | 22 | while(1) { |
hudakz | 1:93ec3038c226 | 23 | ds1820.startConversion(); // start temperature conversion from analog to digital |
hudakz | 1:93ec3038c226 | 24 | wait(1.0); // let DS1820 complete the temperature conversion |
hudakz | 0:ce867244a530 | 25 | error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
hudakz | 0:ce867244a530 | 26 | switch(error) { |
hudakz | 0:ce867244a530 | 27 | case 0: // no errors -> 'temp' contains the value of measured temperature |
hudakz | 0:ce867244a530 | 28 | pc.printf("temp = %3.1f%cC\r\n", temp, 176); |
hudakz | 0:ce867244a530 | 29 | break; |
hudakz | 0:ce867244a530 | 30 | case 1: // no sensor present -> 'temp' is not updated |
hudakz | 0:ce867244a530 | 31 | pc.printf("no sensor present\n\r"); |
hudakz | 0:ce867244a530 | 32 | break; |
hudakz | 0:ce867244a530 | 33 | case 2: // CRC error -> 'temp' is not updated |
hudakz | 0:ce867244a530 | 34 | pc.printf("CRC error\r\n"); |
hudakz | 0:ce867244a530 | 35 | } |
hudakz | 1:93ec3038c226 | 36 | led = !led; |
hudakz | 0:ce867244a530 | 37 | } |
hudakz | 0:ce867244a530 | 38 | } else |
hudakz | 0:ce867244a530 | 39 | pc.printf("No DS1820 sensor found!\r\n"); |
hudakz | 0:ce867244a530 | 40 | } |
hudakz | 0:ce867244a530 | 41 |