![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test
main.cpp@8:ee559cfe0a28, 2020-12-22 (annotated)
- Committer:
- kapliasergii
- Date:
- Tue Dec 22 19:58:40 2020 +0000
- Revision:
- 8:ee559cfe0a28
- Parent:
- 7:fabc8bd0dbcd
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 2:bd6594739176 | 1 | /* |
hudakz | 4:f5c604b5eceb | 2 | * Reading built-in temperature sensor of STM32F103RB chip (on a NUCLEO-F103RB board) |
hudakz | 2:bd6594739176 | 3 | */ |
hudakz | 0:dfdf46225b14 | 4 | |
hudakz | 0:dfdf46225b14 | 5 | #include "mbed.h" |
kapliasergii | 7:fabc8bd0dbcd | 6 | #include "LIS3DH.h" |
hudakz | 0:dfdf46225b14 | 7 | |
kapliasergii | 7:fabc8bd0dbcd | 8 | |
kapliasergii | 7:fabc8bd0dbcd | 9 | #define MOSI PA_7 |
kapliasergii | 7:fabc8bd0dbcd | 10 | #define MISO PA_6 |
kapliasergii | 7:fabc8bd0dbcd | 11 | #define CS PA_4 |
kapliasergii | 7:fabc8bd0dbcd | 12 | #define SCLK PA_5 |
kapliasergii | 7:fabc8bd0dbcd | 13 | |
kapliasergii | 7:fabc8bd0dbcd | 14 | |
kapliasergii | 7:fabc8bd0dbcd | 15 | |
kapliasergii | 7:fabc8bd0dbcd | 16 | |
kapliasergii | 7:fabc8bd0dbcd | 17 | LIS3DH acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G); |
kapliasergii | 7:fabc8bd0dbcd | 18 | |
kapliasergii | 7:fabc8bd0dbcd | 19 | /* |
hudakz | 2:bd6594739176 | 20 | * STM32F103x data-sheet: |
kapliasergii | 7:fabc8bd0dbcd | 21 | * 5.3.19 Temperature sensor characteristics |
hudakz | 2:bd6594739176 | 22 | * Table 50. TS characteristics, Page 80 |
hudakz | 2:bd6594739176 | 23 | */ |
kapliasergii | 7:fabc8bd0dbcd | 24 | const float AVG_SLOPE = 10E-03; // slope (gradient) of temperature line function [V/°C] |
kapliasergii | 7:fabc8bd0dbcd | 25 | const float V25 = 0.75; // sensor's output voltage at 25°C [V] |
kapliasergii | 7:fabc8bd0dbcd | 26 | const float ADC_TO_VOLT = 3.3 / 4096; // conversion coefficient of digital value to voltage [V] |
kapliasergii | 7:fabc8bd0dbcd | 27 | // when using 3.3V ref. voltage at 12-bit resolution (2^12 = 4096) |
kapliasergii | 7:fabc8bd0dbcd | 28 | |
kapliasergii | 7:fabc8bd0dbcd | 29 | |
hudakz | 0:dfdf46225b14 | 30 | |
kapliasergii | 7:fabc8bd0dbcd | 31 | float accX = 0; |
kapliasergii | 7:fabc8bd0dbcd | 32 | float accY = 0; |
kapliasergii | 7:fabc8bd0dbcd | 33 | float accZ = 0; |
kapliasergii | 7:fabc8bd0dbcd | 34 | |
kapliasergii | 7:fabc8bd0dbcd | 35 | |
kapliasergii | 7:fabc8bd0dbcd | 36 | |
kapliasergii | 7:fabc8bd0dbcd | 37 | |
kapliasergii | 7:fabc8bd0dbcd | 38 | Serial pc(PA_9, PA_10); |
hudakz | 5:27071c2166bb | 39 | DigitalOut led(PC_13); |
hudakz | 4:f5c604b5eceb | 40 | ADC_HandleTypeDef hadc1; // ADC handle |
hudakz | 4:f5c604b5eceb | 41 | uint16_t adcValue; // digital value of sensor |
hudakz | 4:f5c604b5eceb | 42 | float vSense; // sensor's output voltage [V] |
hudakz | 4:f5c604b5eceb | 43 | float temp; // sensor's temperature [°C] |
hudakz | 0:dfdf46225b14 | 44 | |
hudakz | 0:dfdf46225b14 | 45 | /* ADC1 init function */ |
hudakz | 5:27071c2166bb | 46 | void MX_ADC1_Init(void) |
hudakz | 5:27071c2166bb | 47 | { |
kapliasergii | 7:fabc8bd0dbcd | 48 | pc.baud(115200); |
kapliasergii | 7:fabc8bd0dbcd | 49 | pc.format(8,SerialBase::Odd,1); |
hudakz | 0:dfdf46225b14 | 50 | ADC_ChannelConfTypeDef sConfig; |
hudakz | 0:dfdf46225b14 | 51 | |
hudakz | 0:dfdf46225b14 | 52 | /**Common config |
hudakz | 0:dfdf46225b14 | 53 | */ |
hudakz | 0:dfdf46225b14 | 54 | hadc1.Instance = ADC1; |
hudakz | 0:dfdf46225b14 | 55 | hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; |
hudakz | 0:dfdf46225b14 | 56 | hadc1.Init.ContinuousConvMode = DISABLE; |
hudakz | 0:dfdf46225b14 | 57 | hadc1.Init.DiscontinuousConvMode = DISABLE; |
hudakz | 0:dfdf46225b14 | 58 | hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; |
hudakz | 0:dfdf46225b14 | 59 | hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
hudakz | 0:dfdf46225b14 | 60 | hadc1.Init.NbrOfConversion = 1; |
hudakz | 0:dfdf46225b14 | 61 | HAL_ADC_Init(&hadc1); |
hudakz | 0:dfdf46225b14 | 62 | |
hudakz | 0:dfdf46225b14 | 63 | /**Configure Regular Channel |
hudakz | 0:dfdf46225b14 | 64 | */ |
kapliasergii | 7:fabc8bd0dbcd | 65 | sConfig.Channel = 9; |
hudakz | 0:dfdf46225b14 | 66 | sConfig.Rank = 1; |
hudakz | 0:dfdf46225b14 | 67 | sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; |
hudakz | 0:dfdf46225b14 | 68 | HAL_ADC_ConfigChannel(&hadc1, &sConfig); |
hudakz | 0:dfdf46225b14 | 69 | } |
hudakz | 0:dfdf46225b14 | 70 | |
kapliasergii | 7:fabc8bd0dbcd | 71 | |
kapliasergii | 7:fabc8bd0dbcd | 72 | |
kapliasergii | 7:fabc8bd0dbcd | 73 | // When characteristic input changing |
kapliasergii | 7:fabc8bd0dbcd | 74 | void Accupdate() |
kapliasergii | 7:fabc8bd0dbcd | 75 | { |
kapliasergii | 7:fabc8bd0dbcd | 76 | |
kapliasergii | 7:fabc8bd0dbcd | 77 | accX = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15; |
kapliasergii | 7:fabc8bd0dbcd | 78 | accY = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15; |
kapliasergii | 7:fabc8bd0dbcd | 79 | accZ = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15; |
kapliasergii | 7:fabc8bd0dbcd | 80 | |
kapliasergii | 7:fabc8bd0dbcd | 81 | |
kapliasergii | 7:fabc8bd0dbcd | 82 | pc.printf("X acceleration = "); |
kapliasergii | 7:fabc8bd0dbcd | 83 | pc.printf("%5.2f",accX); |
kapliasergii | 7:fabc8bd0dbcd | 84 | pc.printf("Y acceleration = "); |
kapliasergii | 7:fabc8bd0dbcd | 85 | pc.printf("%5.2f",accY); |
kapliasergii | 7:fabc8bd0dbcd | 86 | pc.printf("Z acceleration = "); |
kapliasergii | 7:fabc8bd0dbcd | 87 | pc.printf("%5.2f",accZ); |
kapliasergii | 7:fabc8bd0dbcd | 88 | } |
kapliasergii | 7:fabc8bd0dbcd | 89 | |
kapliasergii | 7:fabc8bd0dbcd | 90 | |
hudakz | 5:27071c2166bb | 91 | int main() |
hudakz | 5:27071c2166bb | 92 | { |
hudakz | 4:f5c604b5eceb | 93 | MX_ADC1_Init(); // initialize AD convertor |
hudakz | 4:f5c604b5eceb | 94 | while(HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK); // calibrate AD convertor |
hudakz | 0:dfdf46225b14 | 95 | |
kapliasergii | 7:fabc8bd0dbcd | 96 | while(1) { |
hudakz | 4:f5c604b5eceb | 97 | HAL_ADC_Start(&hadc1); // start analog to digital conversion |
hudakz | 4:f5c604b5eceb | 98 | while(HAL_ADC_PollForConversion(&hadc1, 1000000) != HAL_OK);// wait for completing the conversion |
hudakz | 4:f5c604b5eceb | 99 | adcValue = HAL_ADC_GetValue(&hadc1); // read sensor's digital value |
hudakz | 4:f5c604b5eceb | 100 | vSense = adcValue * ADC_TO_VOLT; // convert sensor's digital value to voltage [V] |
hudakz | 2:bd6594739176 | 101 | /* |
hudakz | 2:bd6594739176 | 102 | * STM32F103xx Reference Manual: |
hudakz | 2:bd6594739176 | 103 | * 11.10 Temperature sensor |
hudakz | 2:bd6594739176 | 104 | * Reading the temperature, Page 235 |
hudakz | 4:f5c604b5eceb | 105 | * Temperature (in °C) = {(V25 - Vsense) / Avg_Slope} + 25 |
hudakz | 2:bd6594739176 | 106 | */ |
kapliasergii | 7:fabc8bd0dbcd | 107 | temp = (vSense - V25) / AVG_SLOPE + 25.0f; // convert sensor's output voltage to temperature [°C] |
kapliasergii | 7:fabc8bd0dbcd | 108 | pc.printf("temp = %3.1f%cC, voltage = %f\n", temp, 176, vSense); // display chip's temperature [°C] |
kapliasergii | 7:fabc8bd0dbcd | 109 | Accupdate(); |
hudakz | 0:dfdf46225b14 | 110 | wait_ms(1000); |
kapliasergii | 7:fabc8bd0dbcd | 111 | } |
hudakz | 0:dfdf46225b14 | 112 | } |