Introduction and initialization of using STM8S003K3 ADC

Publisher:科技创造者Latest update time:2020-02-07 Source: eefocusKeywords:STM8S003K3  ADC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

insert image description here

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

insert image description here
insert image description here
insert image description here
insert image description here

*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

insert image description here

2. Functional Description

2.1 Enable/Disable ADC

insert image description here

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

insert image description here

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.

Keywords:STM8S003K3  ADC Reference address:Introduction and initialization of using STM8S003K3 ADC

Previous article:STM8S uses external clock to cause serial port data transmission errors
Next article:Notes on STM8S003F3P6 development

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号