STM8 MCU ADC continuous sampling mode

Publisher:世界因你而精彩Latest update time:2021-09-16 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:

image.png

The ADC continuous mode conversion timing diagram is as follows:

image.png

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:

image.png

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.

Keywords:STM8 Reference address:STM8 MCU ADC continuous sampling mode

Previous article:STM8 MCU ADC continuous sampling mode with cache
Next article:STM8 MCU ADC single sampling mode

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号