STM8L ADC
/***********************************************************************************
*Name: Hal_Comm_GetBatADC
*Function: Get battery power information
*Entry parameters: None
*Export parameter: AD value of power
*illustrate:
***********************************************************************************/
unsigned int Hal_Comm_GetBatADC(void)
{
unsigned int Result = 0, value_adc = 0;
unsigned char index = 0;
GPIO_Init(VOL_ADC_PORT,VOL_ADC_PIN,GPIO_Mode_In_FL_No_IT);//VOL_ADC
ADC1->CR1 |= 0x01; // Enable AD
ADC1->CR3 &= ~0x1F; // Select sampling time 384 cycles Channel clear
ADC1->CR3 |= 0x00; // channel 0 is selected
ADC1->SQR[0] = ADC1->SQR[1] = ADC1->SQR[2] = ADC1->SQR[3] = 0x00;
ADC1->SQR[3] = 0x01; // Select channel 0 to scan
ADC1->SR &= 0x00; // conversion not complete
ADC1->SR = 0; // conversion flag cleared
ADC1->CR1 |= 0x03; // Enable AD and start conversion
while(!(ADC1->SR & 0x01)); //waitloop bit0: 1 complete 0 not complete
for(index = 0; index < 10; index++)
{
ADC1->SR = 0; // conversion flag cleared
ADC1->CR1 |= 0x03; // Enable AD and start conversion
while(!(ADC1->SR & 0x01));//waitloop conversion complete
Result = ADC1->DRH; //Calculate a total of 16 bits, but ADC is 12 bits long
Result <<= 8; //ADC1->DRH is the high 8 bits,
Result += ADC1->DRL; //ADC1->DRL is the lower 8 bits
value_adc += Result;
if(index != 0)value_adc = value_adc/2; //Get multiple data and calculate the average value
index++;
}
ADC1->CR1 &= 0xFE; // turn off AD
ADC1->CR3 &= ~0x1F; //Select channel
ADC1->CR3 |= 0x11;
return value_adc;
}
When designing schematics, we usually set the ADC input voltage range to 0~3.3v. If the ADC is set to 12 bits, then the 12-bit full scale corresponds to 3.3V, and the digital value corresponding to the 12-bit full scale is 2^12. The value 0 corresponds to 0V. If the converted ADC value is X, and the analog voltage corresponding to X is Y, then there will be such an equation: 2^12/3.3=X/Y, => Y=(3.3*X)/2^12
In our smart lock project, the full-scale Vref is 3.31V, total voltage (M6V)/test voltage (VOL_ADC) = 3
4096/3.31 = value_adc/Va
Va = value_adc * 3.31 / 4096
Total voltage = Va * 3
That is, vin in the program = value_adc * 3.31 * 3 / 4096
Previous article:STM8 low power design
Next article:STM8S timer TIM4 timing 1kHz output
- Popular Resources
- Popular amplifiers
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
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- What is the difference between SOP package and DIP package!
- Which is more effective in eliminating the back EMF of inductive loads, TVS or varistor?
- The chip cannot be removed
- A must-read summary of drone principles (IV) Professional terminology for drones
- MicroPython Hands-on (36) - MixPY Hello world
- 【i.MX6ULL】Driver Development 12——Capacitive Touch Driver Practice (Part 1)
- MicroPython now supports STM32F413 controller
- Sofa stickers
- Introduction to the ports of MSP430 microcontroller
- Two highlights of EFM32PG22