The STM8 microcontroller ADC supports five conversion modes: single mode, continuous mode, continuous mode with buffer, single scan mode, and continuous scan mode.
Single scan mode
This mode is used to convert a series of analog channels from AIN0 to AIN1, where 'n' is the channel number in the ADC_CSR register. It is started by setting the ADON bit while the CONT bit is cleared.
Note: When using the scan mode, you cannot set the I/O ports corresponding to the channels between AIN0 and AINn to output status, because the ADC multiplexer has disabled the output modules of these I/O ports.
For single scan mode, the conversion starts from AIN0 channel and the result data is stored in the data buffer register ADC_DBxR. When the last channel (channel 'n') is converted, the EOC (end of conversion) flag is set and an interrupt is generated when the EOCIE bit is set.
The conversion result values of each channel can be read from the buffer register. If a data buffer register is overwritten before being read, the OVR flag will be set to 1. (See 24.5.5)
Do not clear the SCAN bit while a conversion sequence is in progress; single scan mode can be stopped immediately by clearing the ADON bit.
To start a new SCAN scan conversion, this can be achieved by clearing the EOC bit in the ADC_CR1 register and setting the ADON bit.
When reading the sampling results, you can do so by querying or by interrupting.
Single scan mode reads data code by querying:
#include "adc.h"
#include "main.h"
#include "led.h"
_Bool ADC_flag = 0; //ADC conversion success flag
u16 ADC_DB[10] = {0};
u16 adc_data[5] = {0};
//AD channel pin initialization
void ADC_GPIO_Init( void )
{
PD_DDR &= ~( 1 << 2 ); //PD2 is set as input
PD_CR1 &= ~( 1 << 2 ); //PD2 is set to floating input
PD_DDR &= ~( 1 << 3 ); //PD3 is set as input
PD_CR1 &= ~( 1 << 3 ); //PD3 is set to floating input
PC_DDR &= ~( 1 << 4 ); // PC4 is set as input
PC_CR1 &= ~( 1 << 4 ); //PC4 is set to floating input
}
//Set to single scan mode
//ch is the ADC channel that continuously converts the data of AIN0---AINch channel
void ADC_CH_Init( u8 ch )
{
char l = 0;
ADC_GPIO_Init();
ADC_CR1 &= ~( 7 << 4 ); //Prescaler 2
ADC_CR2 &= ~( 1 << 6 ); //Do not use external trigger
//Disable the Schmitt trigger of AIN2 AIN4 to reduce IO static power consumption. If the Schmitt mode of the channels on PD5 and PD6 is disabled, the serial port will be unable to send and receive data!
ADC_TDRL |= ( 1 << 2 );
ADC_TDRL |= ( 1 << 4 );
ADC_CR1 &= ~( 1 << 1 ); //single conversion
ADC_CSR |= 0x04; //Configure the channel with the largest number
ADC_CR2 |= ( 1 << 3 ); //right-align
ADC_CR1 |= ( 1 << 0 ); //Turn on ADC
ADC_CR2 |= ( 1 << 1 ); // SCAN = 1 to turn on scan mode
//When the ADON bit is set for the first time, the ADC wakes up from low power mode. To start the conversion, the ADON bit of the ADC_CR1 register must be set a second time using the write instruction.
for( l = 0; l < 10; l++ ); //Delay to ensure that the ADC module is powered on for at least 7us
ADC_CR1 |= ( 1 << 0 ); //Set the lowest bit of the CR1 register to 1 again to enable ADC and start conversion
}
u16 ain2_val = 0,ain3_val = 0,ain4_val = 0;
//Read the sampled voltage value
u16 ReadVol_CHx( void )
{
u16 voltage = 0;
u16 temph = 0;
u8 templ = 0;
while( 1 )
{
LED = !LED; //The program takes 15us to run one circle
ADC_CR1 |= 0x01; //Start a conversion
while( ( ADC_CSR & 0x80 ) == 0 ); //Wait for the conversion to end
ADC_CSR &= ~( 1 << 7 ); // Clear the conversion end flag EOC
//Read the value of AIN2
templ = ADC_DB2RL;
temph = ADC_DB2RH;
temph = ( u16 )( templ | ( u16 )( temph << ( u16 )8 ) );
ain2_val = temph;
//Read the value of AIN3
templ = ADC_DB3RL;
temph = ADC_DB3RH;
temph = ( u16 )( templ | ( u16 )( temph << ( u16 )8 ) );
ain3_val = temph;
//Read the value of AIN4
templ = ADC_DB4RL;
temph = ADC_DB4RH;
temph = ( u16 )( templ | ( u16 )( temph << ( u16 )8 ) );
ain4_val = temph;
}
return voltage;
}
Single scan mode reads data code through interruption:
#include "adc.h"
#include "main.h"
#include "led.h"
_Bool ADC_flag = 0; //ADC conversion success flag
u16 ADC_DB[10] = {0};
u16 adc_data[5] = {0};
//AD channel pin initialization
void ADC_GPIO_Init( void )
{
PD_DDR &= ~( 1 << 2 ); //PD2 is set as input
PD_CR1 &= ~( 1 << 2 ); //PD2 is set to floating input
PD_DDR &= ~( 1 << 3 ); //PD3 is set as input
PD_CR1 &= ~( 1 << 3 ); //PD3 is set to floating input
PC_DDR &= ~( 1 << 4 ); // PC4 is set as input
PC_CR1 &= ~( 1 << 4 ); //PC4 is set to floating input
}
//Set to single scan mode
//ch is the ADC channel that continuously converts the data of AIN0---AINch channel
void ADC_CH_Init( u8 ch )
{
char l = 0;
ADC_GPIO_Init();
ADC_CR1 &= ~( 7 << 4 ); //Prescaler 2
ADC_CR2 &= ~( 1 << 6 ); //Do not use external trigger
//Disable the Schmitt trigger of AIN2 AIN4 to reduce IO static power consumption. If the Schmitt mode of the channels on PD5 and PD6 is disabled, the serial port will be unable to send and receive data!
ADC_TDRL |= ( 1 << 2 );
ADC_TDRL |= ( 1 << 4 );
ADC_CR1 &= ~( 1 << 1 ); //single conversion
ADC_CSR |= 0x04; //Configure the channel with the largest number
ADC_CR2 |= ( 1 << 3 ); //right-align
ADC_CR1 |= ( 1 << 0 ); //Turn on ADC
ADC_CR2 |= ( 1 << 1 ); // SCAN = 1 to turn on scan mode
ADC_CSR |= ( 1 << 5 ); //EOCIE enable conversion end interrupt
//When the ADON bit is set for the first time, the ADC wakes up from low power mode. To start the conversion, the ADON bit of the ADC_CR1 register must be set a second time using the write instruction.
for( l = 0; l < 10; l++ ); //Delay to ensure that the ADC module is powered on for at least 7us
ADC_CR1 |= ( 1 << 0 ); //Set the lowest bit of the CR1 register to 1 again to enable ADC and start conversion
}
//AD interrupt service function interrupt number 22
#pragma vector = 24 // The interrupt number in IAR needs to be added to the interrupt number in STVD by 2
__interrupt void ADC_Handle( void )
{
ADC_CSR &= ~( 1 << 7 ); // Clear the conversion end flag EOC
//Single channel scanning mode, the conversion result is stored in the ADC_DBxR register
LED = !LED; //The program takes 20us to run one circle
//Read the value of AIN2
templ = ADC_DB2RL;
temph = ADC_DB2RH;
temph = ( u16 )( templ | ( u16 )( temph << ( u16 )8 ) );
ain2_val = temph;
//Read the value of AIN3
templ = ADC_DB3RL;
temph = ADC_DB3RH;
temph = ( u16 )( templ | ( u16 )( temph << ( u16 )8 ) );
ain3_val = temph;
//Read the value of AIN4
templ = ADC_DB4RL;
temph = ADC_DB4RH;
temph = ( u16 )( templ | ( u16 )( temph << ( u16 )8 ) );
ain4_val = temph;
ADC_CR1 |= 0x01; //Start a conversion
ADC_flag = 1; // ADC interrupt flag is set to 1
}
Previous article:STM8 ADC conversion mode ------- continuous scan mode
Next article:STM8 ADC conversion mode ------- continuous mode with cache
- 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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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
- BlueNRG LP turned off radio, app not working?
- EEWorld invites you to attend the 2019 STM32 Summit and Fan Carnival!
- Switching Power Supply Interest Group 16th Task
- A brief introduction to the internal structure of photoelectric sensors
- Geek Black Technology, Shopping Carnival | Here are the deals you can’t miss!
- I need help from an expert. "When designing a microcontroller hardware circuit using STC89C51, should I design the EEPROM circuit part?"
- Claiming that software is open source when it is not is false advertising
- Zigbee 3.0 universal gateway coordinator based on CC2652R
- Ask a question about the LM35CZ temperature sensor
- MSP430FR2311 LaunchPad Development Kit