1. CubeMX configuration
First we configure ADC1 in CubeMX,
Set a timer TIM2 to sample once in 1s and refresh OLED once,
Turn on the IIC to drive the OLED display.
2. Procedure
Add the oled display library in Keil5, as well as the functions used for display, initialization functions, screen clearing functions, etc. Initialize oled in the main program and clear the screen.
Open the ADC in the timer interrupt callback function, convert the sampling and other operations, convert the voltage sent by the smoke sensor, use sprintf to obtain the real-time changing value, and multiply it to the 12th power of 3.3/2. Then display on OLED.
3. Library functions used
HAL_ADC_Start(&hadc1); // Start ADC1 conversion
HAL_ADC_PollForConversion(&hadc1, 50); // Wait for ADC conversion to complete
value= HAL_ADC_GetValue(&hadc1); // Read ADC conversion data
HAL_TIM_Base_Start_IT(&htim2);//start timer