![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Temperature
main.cpp@2:e999eca5c095, 2021-02-09 (annotated)
- Committer:
- srdjansimic
- Date:
- Tue Feb 09 01:12:23 2021 +0000
- Revision:
- 2:e999eca5c095
- Parent:
- 1:57ff098da1dd
- Child:
- 3:fff97541a6f2
Digitalin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
srdjansimic | 0:1ffbfe56f438 | 1 | #include "mbed.h" |
srdjansimic | 0:1ffbfe56f438 | 2 | |
srdjansimic | 0:1ffbfe56f438 | 3 | /* |
srdjansimic | 0:1ffbfe56f438 | 4 | This basic example just shows how to read the ADC internal channels raw values. |
srdjansimic | 0:1ffbfe56f438 | 5 | Please look in the corresponding device reference manual for a complete |
srdjansimic | 0:1ffbfe56f438 | 6 | description of how to make a temperature sensor, VBat or Vref measurement. |
srdjansimic | 0:1ffbfe56f438 | 7 | */ |
srdjansimic | 0:1ffbfe56f438 | 8 | |
srdjansimic | 0:1ffbfe56f438 | 9 | AnalogIn adc_temp(ADC_TEMP); |
srdjansimic | 0:1ffbfe56f438 | 10 | AnalogIn adc_vref(ADC_VREF); |
srdjansimic | 0:1ffbfe56f438 | 11 | #if defined(TARGET_DISCO_F051R8) || defined(TARGET_NUCLEO_F030R8) || defined(TARGET_NUCLEO_F031K6) || defined(TARGET_NUCLEO_F042K6) || defined(TARGET_NUCLEO_F070RB) || defined(TARGET_NUCLEO_F072RB) || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F207ZG) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_F303K8) || defined(TARGET_DISCO_F303VC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F303ZE) || defined(TARGET_DISCO_F334C8) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_MTS_DRAGONFLY_F411RE) || defined(TARGET_MTS_MDOT_F405RG) || defined(TARGET_MTS_MDOT_F411RE) || defined(TARGET_DISCO_F401VC) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_ARCH_MAX) || defined(TARGET_DISCO_F407VG) || defined(TARGET_NUCLEO_F410RB) || defined(TARGET_ELMO_F411RE) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F412ZG) || defined(TARGET_DISCO_F413ZH) || defined(TARGET_DISCO_F429ZI) || defined(TARGET_NUCLEO_F429ZI) || defined(TARGET_NUCLEO_F439ZI) || defined(TARGET_B96B_F446VE) || defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F446ZE) || defined(TARGET_DISCO_F469NI) || defined(TARGET_DISCO_F746NG) || defined(TARGET_NUCLEO_F746ZG) || defined(TARGET_NUCLEO_F756ZG) || defined(TARGET_NUCLEO_F767ZI) || defined(TARGET_DISCO_F769NI) || defined(TARGET_DISCO_L072CZ_LRWAN1) || defined(TARGET_NUCLEO_L432KC) || defined(TARGET_DISCO_L476VG) || defined(TARGET_NUCLEO_L476RG) || defined(TARGET_NUCLEO_L486RG) |
srdjansimic | 0:1ffbfe56f438 | 12 | AnalogIn adc_vbat(ADC_VBAT); // Warning: Not available on all devices |
srdjansimic | 0:1ffbfe56f438 | 13 | #endif |
srdjansimic | 0:1ffbfe56f438 | 14 | |
srdjansimic | 0:1ffbfe56f438 | 15 | DigitalOut led(LED1); |
srdjansimic | 1:57ff098da1dd | 16 | AnalogIn analog_light_sensor_read(A1); |
srdjansimic | 2:e999eca5c095 | 17 | DigitalIn digital_light_sensor_read(D8); |
srdjansimic | 0:1ffbfe56f438 | 18 | |
srdjansimic | 0:1ffbfe56f438 | 19 | int main() |
srdjansimic | 0:1ffbfe56f438 | 20 | { |
srdjansimic | 1:57ff098da1dd | 21 | int adc_scale = 4096; |
srdjansimic | 1:57ff098da1dd | 22 | int sensorValue; |
srdjansimic | 1:57ff098da1dd | 23 | float Rsensor; |
srdjansimic | 1:57ff098da1dd | 24 | |
srdjansimic | 0:1ffbfe56f438 | 25 | printf("\nSTM32 ADC internal channels reading example\n"); |
srdjansimic | 0:1ffbfe56f438 | 26 | while(1) { |
srdjansimic | 0:1ffbfe56f438 | 27 | printf("ADC Temp = %f\n", (adc_temp.read()*100)); |
srdjansimic | 2:e999eca5c095 | 28 | // printf("ADC VRef = %f\n", adc_vref.read()); |
srdjansimic | 0:1ffbfe56f438 | 29 | #if defined(TARGET_DISCO_F051R8) || defined(TARGET_NUCLEO_F030R8) || defined(TARGET_NUCLEO_F031K6) || defined(TARGET_NUCLEO_F042K6) || defined(TARGET_NUCLEO_F070RB) || defined(TARGET_NUCLEO_F072RB) || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F207ZG) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_F303K8) || defined(TARGET_DISCO_F303VC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F303ZE) || defined(TARGET_DISCO_F334C8) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_MTS_DRAGONFLY_F411RE) || defined(TARGET_MTS_MDOT_F405RG) || defined(TARGET_MTS_MDOT_F411RE) || defined(TARGET_DISCO_F401VC) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_ARCH_MAX) || defined(TARGET_DISCO_F407VG) || defined(TARGET_NUCLEO_F410RB) || defined(TARGET_ELMO_F411RE) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F412ZG) || defined(TARGET_DISCO_F413ZH) || defined(TARGET_DISCO_F429ZI) || defined(TARGET_NUCLEO_F429ZI) || defined(TARGET_NUCLEO_F439ZI) || defined(TARGET_B96B_F446VE) || defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F446ZE) || defined(TARGET_DISCO_F469NI) || defined(TARGET_DISCO_F746NG) || defined(TARGET_NUCLEO_F746ZG) || defined(TARGET_NUCLEO_F756ZG) || defined(TARGET_NUCLEO_F767ZI) || defined(TARGET_DISCO_F769NI) || defined(TARGET_DISCO_L072CZ_LRWAN1) || defined(TARGET_NUCLEO_L432KC) || defined(TARGET_DISCO_L476VG) || defined(TARGET_NUCLEO_L476RG) || defined(TARGET_NUCLEO_L486RG) |
srdjansimic | 2:e999eca5c095 | 30 | // printf("ADC VBat = %f\n", adc_vbat.read()); |
srdjansimic | 2:e999eca5c095 | 31 | // printf("\033[3A"); |
srdjansimic | 0:1ffbfe56f438 | 32 | #else |
srdjansimic | 2:e999eca5c095 | 33 | // printf("\033[2A"); |
srdjansimic | 0:1ffbfe56f438 | 34 | #endif |
srdjansimic | 0:1ffbfe56f438 | 35 | led = !led; |
srdjansimic | 0:1ffbfe56f438 | 36 | if ((adc_temp.read()*100) > 20) { |
srdjansimic | 0:1ffbfe56f438 | 37 | wait(1.0); |
srdjansimic | 0:1ffbfe56f438 | 38 | } else { |
srdjansimic | 0:1ffbfe56f438 | 39 | wait(0.25); |
srdjansimic | 0:1ffbfe56f438 | 40 | } |
srdjansimic | 1:57ff098da1dd | 41 | sensorValue = analog_light_sensor_read.read_u16(); |
srdjansimic | 1:57ff098da1dd | 42 | Rsensor=(float)(adc_scale-sensorValue)*10/sensorValue; |
srdjansimic | 1:57ff098da1dd | 43 | printf("Light Sensor Analog Reading is 0x%X = %d ", sensorValue, sensorValue); |
srdjansimic | 1:57ff098da1dd | 44 | printf("The sensor resistance is %f \n\n\r", Rsensor); |
srdjansimic | 2:e999eca5c095 | 45 | if (digital_light_sensor_read) { |
srdjansimic | 2:e999eca5c095 | 46 | printf("Digital sensor is ON\r\n"); |
srdjansimic | 2:e999eca5c095 | 47 | } else { |
srdjansimic | 2:e999eca5c095 | 48 | printf("Digital sensor is OFF\r\n"); |
srdjansimic | 2:e999eca5c095 | 49 | } |
srdjansimic | 2:e999eca5c095 | 50 | // digital_light_sensor_read = !digital_light_sensor_read; |
srdjansimic | 2:e999eca5c095 | 51 | |
srdjansimic | 2:e999eca5c095 | 52 | printf("Digital sensor is %d \r\n", digital_light_sensor_read); |
srdjansimic | 1:57ff098da1dd | 53 | // wait(1); // 1s |
srdjansimic | 0:1ffbfe56f438 | 54 | } |
srdjansimic | 0:1ffbfe56f438 | 55 | } |