Introduction
I recently used stm8s003k3 to develop a project and used the ADC module. I would like to record some notes and share my experience.
Software environment: STVD+COSMIC
Hardware environment: STM8S003K3 TSSOP20 package
Module: ADC
1.ADC function and block diagram
Several pieces of information can be obtained from the STM8S reference manual:
1. There are two ADC1 and ADC2 (it depends on the package, my model only has ADC1)
2. There are up to 16 input channels (depending on the package, my model only has 2~6 inputs)
3. There are several extended functions
The specific functions are as follows
*From the block diagram, we can see that there is a little difference between ADC1 and ADC2. In some multi-pin chips, ADC2 has analog reference positive and negative electrodes. In analog amplification, it can provide greater resolution by reducing the reference voltage. Since it is not used, it is not considered.
* Interrupt can be generated when conversion is finished
*f MASTER can be divided by 2 to 18
*ADC input voltage range: VSSA ≤ VIN ≤ VDDA
*You can choose ADC dedicated external interrupt (ADC_ETR) or timer trigger signal (TRGO) as
External trigger signal
2. Functional Description
2.1 Enable/Disable ADC
2.2 Conversion Mode
ADC has five conversion modes: single mode, continuous mode, continuous mode with buffer, word scan mode, and continuous scan mode. The above modes can be classified for easy understanding.
2.2.1ADC single channel:
A. Requirement: Perform an ADC conversion, configure it to enable single-shot mode, and disable scan mode, so that the ADC stops converting after converting this channel once.
B. Requirement: To perform continuous ADC conversion, configure the continuous mode to be enabled and the scan mode to be disabled. This way, after the ADC channel is converted once, the next conversion will be performed continuously.
2.2.2ADC multi-channel:
C. Requirement: Perform a round of ADC conversion: configure to enable single mode and enable scan mode, so that multiple channels of ADC will convert once in the configured order and then stop.
D. Requirement: To perform continuous ADC conversion: configure the continuous mode to enable, and the scan mode to enable, so that multiple channels of the ADC are converted once in the configured order, and then the next conversion is performed continuously.
2.2.3 Conclusion:
Scan mode is only valid under multi-channel conditions, so that each channel is converted in turn according to the configuration cycle, while single mode only converts these channels once regardless of single or multi-channel conditions.
2.3 Alignment
The alignment is controlled by the ALIGN bit of ADC_CR2
2.4 Schmitt Trigger
The Schmitt trigger is used for input signal shaping. The ADC should not use the Schmitt trigger to maintain the continuity of the input analog signal. Note that if the Schmitt trigger is disabled for the unused ADC input port, such as the RX of UART, the Schmitt trigger is disabled during ADC initialization, and the pin is reused as UART-RX. At this time, the RX signal cannot enter the UART receiving module at all, and the UART receive interrupt cannot be generated. Later, when it turns on the Schmitt trigger, the URAT-RX reception will be normal.
3.ADC initialization code implementation
static Adc_Config_t Adc_Config;
void Adc_Init(const Adc_Config_t * config)
{
memcpy(&Adc_Config, config, sizeof(Adc_Config));
/* Clear the alignment flag */
ADC_CR2 &= (uint8_t)(~0x08);
/* ----------------------------------Alignment----------------------------------*/
ADC_CR2 |= (uint8_t)Adc_Config.Align;
/* ---------------------------------Conversion method----------------------------------*/
if (Adc_Config.Mode == 0x01)
{
/* Set up continuity */
ADC_CR1 |= 0x02;
}
else
{
/*Set single conversion mode*/
ADC_CR1 &= (uint8_t)(~0x02);
}
/* Clear ADC channel */
ADC_CSR &= (uint8_t)(~ADC_CSR_CH);
/* Set the ADC channel */
ADC_CSR |= (uint8_t)(Adc_Config.Channel);
/* ------------------------------------ADC frequency division------------------------------ */
/*Clear register*/
ADC_CR1 &= (uint8_t)(~0x70);
/*Configuration register*/
ADC_CR1 |= (uint8_t)(Adc_Config.Prescaler);
/* ------------------------------------ADC sub-trigger mode------------------------------ */
/* Clear registers */
ADC_CR2 &= (uint8_t)(~0x30);
/* Select trigger mode or turn it off*/
ADC_CR2 |= (uint8_t)(Adc_Config.ExtTrig);
/* ----------------------------------Configure the Schmitt trigger to be turned off--------------------------------- */
ADC_TDRL |= (uint8_t)((uint8_t)0x01 << (uint8_t)Adc_Config.SchmittTrigg);
/* Enable ADC */
ADC_CR1 |= 0x01;
/* Enable ADC interrupt */
ADC_CSR |= 0x20;
/* Start conversion, start for the second time*/
ADC_CR1 |= ((uint8_t)0x01)
}
Then define the enumeration and structure we want in .h
typedef enum
{
ADC_ALIGN_LEFT = (uint8_t)0x00,
ADC_ALIGN_RIGHT = (uint8_t)0x08
} Adc_AlignType_t;
typedef enum
{
ADCDRV_CHANNEL2 = 0x02,
ADCDRV_CHANNEL3 = 0x03,
ADCDRV_CHANNEL4 = 0x04,
ADCDRV_CHANNEL5 = 0x05,
ADCDRV_CHANNEL6 = 0x06
} Adc_Channel_t;
typedef enum
{
ADC_EXTTRIG_TIM = (uint8_t)0x40,
ADC_EXTTRIG_GPIO = (uint8_t)0x50,
ADC_EXTTRIG_DISABLE = (uint8_t)0x00
} Adc_ExtTrig_TypeDef;
typedef enum
{
ADC_SCHMITTTRIG_CHANNEL2 = 0x02,
ADC_SCHMITTTRIG_CHANNEL3 = 0x03,
ADC_SCHMITTTRIG_CHANNEL4 = 0x04,
ADC_SCHMITTTRIG_CHANNEL5 = 0x05,
ADC_SCHMITTTRIG_CHANNEL6 = 0x06
} Adc_SchmittTrigg_TypeDef;
typedef enum
{
ADC_CONVERSIONMODE_SINGLE = (uint8_t)0x00,
ADC_CONVERSIONMODE_CONTINUOUS = (uint8_t)0x01
} Adc_ConversionMode_t;
typedef enum
{
ADC_PRESSEL_FCPU_D2 = (uint8_t)0x00,
ADC_PRESSEL_FCPU_D3 = (uint8_t)0x10,
ADC_PRESSEL_FCPU_D4 = (uint8_t)0x20,
ADC_PRESSEL_FCPU_D6 = (uint8_t)0x30,
ADC_PRESSEL_FCPU_D8 = (uint8_t)0x40,
ADC_PRESSEL_FCPU_D10 = (uint8_t)0x50,
ADC_PRESSEL_FCPU_D12 = (uint8_t)0x60,
ADC_PRESSEL_FCPU_D18 = (uint8_t)0x70
} Adc_PrescalerType_t;
typedef struct
{
Adc_ConversionMode_t Mode;
Adc_Channel_t Channel;
Adc_PrescalerType_t Prescaler;
Adc_AlignType_t Align;
Adc_ExtTrig_TypeDef ExtTrig;
Adc_SchmittTrigg_TypeDef SchmittTrigg;
} Adc_Config_t;
Then we define a structure with configured parameters and pass the first address of this structure into the initialization function.
Previous article:STM8S uses external clock to cause serial port data transmission errors
Next article:Notes on STM8S003F3P6 development
- 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
- 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
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- Want to buy Infineon products? Go to Infineon Tmall flagship store! New store opening, limited time discount!
- It's Chinese Valentine's Day. Who are you spending it with today? How are you spending it?
- Is there anyone who uses Raspberry Pi to make products directly?
- Practical operation of gate valve in high and low temperature test chamber
- Answer the questions to win prizes | Infineon takes you into the world of silicon carbide (SiC)
- [Domestic RISC-V Linux Banfang·Starlight VisionFive Trial Report] Use USB camera to establish MJPEG streaming service
- Play with AT32F437 (2) --- Simple identification of QR code
- 【GD32F307E-START】+ button test
- The problem of brushless DC motor without Hall no-carry waveform
- TL16C754C application consultation