The internal ADC of the STM8S003 microcontroller is 12 bits, and each channel of the A/D conversion can perform single and continuous conversion modes.
Single conversion mode means that the ADC will stop converting after converting data once. If it needs to continue converting, it is necessary to manually start the second conversion function.
The continuous conversion mode means that after each conversion, the system will automatically start the second conversion, and there is no need to manually set the start of the second conversion. In other words, the continuous conversion mode only needs to be turned on once.
The ADC block diagram is as follows:
The ADC continuous mode conversion timing diagram is as follows:
It can be seen from the timing diagram that the result of the first sampling will not be output until the second conversion is completed, that is, the result output lags behind the sampling by one cycle.
The following code is used to implement the continuous conversion mode of ADC:
#include "adc.h"
#include "main.h"
u16 DATAH = 0; //ADC conversion value high 8 bits
u16 DATAL = 0; //ADC conversion value lower 8 bits
_Bool ADC_flag = 0; //ADC conversion success flag
//AD channel pin initialization
void ADC_GPIO_Init( void )
{
PD_DDR &= ~( 1 << 3 ); //PD3 is set to input current
PD_CR1 &= ~( 1 << 3 ); //PD3 is set to floating input
}
//ch is the corresponding pin of the microcontroller
void ADC_CH_Init( u8 ch )
{
char l = 0;
ADC_CR1 = 0x00; //fADC = fMASTER/2, 8Mhz single conversion, conversion disabled
ADC_CR1 |= ( 1 << 1 ); //Start continuous conversion mode
ADC_CSR = ch + 1; //Control status register selects AD input channel, such as: PD2 (AIN3)
ADC_CR2 = 0x00; //Default left alignment when reading data, read high first and then low
ADC_TDRL = ( 1 << ( ch + 1 ) ); //Disable the corresponding channel Schmitt trigger function 1 left shift ch + 1 bit
ADC_CR1 |= 0x01; // Enable ADC and start conversion
//ADC_CSR |= 0x20; //EOCIE Enable conversion end interrupt EOC interrupt enable
for( l = 0; l < 100; l++ ); // Delay to ensure that the ADC module is powered on for at least 7us
ADC_CR1 = ADC_CR1 | 0x01; // Set the lowest bit of the CR1 register to 1 again to enable ADC and start conversion
}
//Collect PD3 voltage value
u16 ReadVol_CH3( void )
{
u16 voltage = 0;
if( ADC_CSR & 0x80 )
{
DATAH = ADC_DRH; // Read the high 8 bits of the ADC result
DATAL = ADC_DRL; // Read the lower 8 bits of the ADC result
voltage = ( DATAH << 2 ) + DATAL ; //Get data with ten-digit precision 0--1024
ADC_CSR &= 0x7F;
};
return voltage;
}
In continuous conversion mode, you only need to start the conversion once, and then wait for the data conversion to end and read the data. Since the continuous conversion mode has a fast conversion speed, if interrupts are used, the system interrupts too frequently, affecting the execution of other codes. Therefore, you do not need interrupts to read data. When you need data, you can directly judge the conversion end flag to read the data.
The ADC_CSR register is as follows:
When the ADC conversion is completed, the EOC bit of the ADC_CSR register will be set to 1. After reading the sampling results, the EOC bit needs to be manually cleared.
Since the continuous conversion mode will only convert the current channel after initialization, you need to reinitialize the ADC if you want to switch to other channels.
Just call the channel read function directly in the main program.
#include "iostm8s103F3.h"
#include "led.h"
#include "adc.h"
#include "stdio.h"
void SysClkInit( void )
{
CLK_SWR = 0xe1; //HSI is the main clock source 16MHz CPU clock frequency
CLK_CKDIVR = 0x00; //CPU clock divided by 0, system clock divided by 0
}
void main( void )
{
u16 val1 = 0;
u16 i=0;
u16 value[450]={0};
SysClkInit();
__asm( "sim" ); //Disable interrupts
LED_GPIO_Init();
ADC_CH_Init( 3 );
__asm( "rim" ); // Enable interrupt
while( 1 )
{
LED = !LED;
for(i=0;i<450;i++)
{
val1 = ReadVol_CH3();
value[i]=val1;
}
i=0;
}
}
In the main function, the data of 450 samples are read continuously and stored in an array.
Previous article:STM8 MCU ADC continuous sampling mode with cache
Next article:STM8 MCU ADC single sampling mode
- Popular Resources
- Popular amplifiers
- 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
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Add OpenOCD Flash driver for domestic chips --- take AIC8800 as an example
- [Erha Image Recognition Artificial Intelligence Vision Sensor] The final report is based on Erha Image Recognition Vision Sensor and STM32 Smart Car
- Which TI model is the chip with silkscreen AO34?
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board review] Touch button
- Maintaining Common Equipment - Power Meters and Power Sensors
- [Open Source] Introduction to the whole board resources - FengShell ARM dual processor development board series
- Does the reverse gate-source voltage of an enhanced NMOS have an effect on the drain-source leakage current?
- Bluetooth core 5.1 standard
- SinlinxA33 development board Linux bus device driver implementation process (with code)
- Newbies to automotive electronics, look here first!