STM8 gets voltage value
(1) Port definition
static void ADC_POWER_Config()
{
/* Init GPIO for ADC1 */
GPIO_Init(GPIOB, GPIO_PIN_2, GPIO_MODE_IN_FL_NO_IT); //Variable resistor
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_2, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL0, ENABLE);
ADC1_Cmd(ENABLE);
}
(2) Obtain the voltage value (here the voltage value is collected five times and the average value is taken)
#define BATTERY_CHECK_COUNT_PER_COLLECT 5 //Number of times each collection
u16 Get_ADC_POWER_Value(void)
{
u16 nBattery_Check_Value = 0;
u16 nNum = 0;
for(nNum = 0; nNum < BATTERY_CHECK_COUNT_PER_COLLECT; nNum++) // Collect BATTERY_CHECK_COUNT_PER_COLLECT times and accumulate the collected power values
{
ADC1_StartConversion(); //Start conversion
nBattery_Check_Value += ADC1_GetConversionValue(); //Accumulate and read the conversion value of adc
}
nBattery_Check_Value = nBattery_Check_Value/(BATTERY_CHECK_COUNT_PER_COLLECT*10); //Calculate the average power
return nBattery_Check_Value;
}
(3) main function
void main(void)
{
ADC_POWER_Config();
u16 nBattery_Check_Value2 = 0;
nBattery_Check_Value2 = Get_ADC_POWER_Value(); //Read the power
printf("nBattery_Check_Value2 = %d\n",nBattery_Check_Value2);
}
Previous article:Several key programs for MCU to simulate I2C bus communication
Next article:STM8L15x series MCU determines the level of the specified I/O pin
Recommended ReadingLatest update time:2024-11-15 14:49
- Popular Resources
- Popular amplifiers
- STM8 C language programming (1) - basic program and startup code analysis
- Description of the BLDC back-EMF sampling method based on the STM8 official library
- STM32 MCU project example: Smart watch design based on TouchGFX (8) Associating the underlying driver with the UI
- uCOS-III Kernel Implementation and Application Development Practical Guide - Based on STM32 (Wildfire)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- LED life issues
- How big is the driving capability of the DSP chip?
- Analysis and solution of temperature rise factors of CITIC Hua circuit board
- MSP430AFE235
- [RVB2601 Creative Application Development] Three-in-one OLED display for calendar, time, temperature and humidity
- GD32E103 eval board download error
- EEWORLD University - Brushless Motor Tutorial
- EEWORLD University----[High Precision Laboratory] Interface: RS-485
- 【Perf-V Review】VIVADO Flowing Light
- Evaluation report summary: Mir MYS-8MMX