The study of STM32ADC in "Blue Bridge Cup Preparation" is worth a look [complete source code] (5)

1. STM32F10xxx microcontroller series product capacity

  • Small products refer to STM32F101xx, STM32F102xx and STM32F103xx microcontrollers with flash memory capacities between 16K and 32K bytes .
  • Medium-density products refer to STM32F101xx, STM32F102xx and STM32F103xx microcontrollers with flash memory capacities between 64K and 128K bytes .
  • High-density products refer to STM32F101xx and STM32F103xx microcontrollers with flash memory capacities between 256K and 512K bytes .
  • Interconnected products refer to STM32F105xx and STM32F107xx microcontrollers.

2. Introduction to ADC

       Check out ST's official STM32F10xxx reference manual , the 12-bit ADC is a successive approximation analog-to-digital converter. It has up to 18 channels and can measure 16 external and 2 internal sources . A/D conversion of each channel can be performed in single, continuous, sweep or discontinuous mode . The result of the ADC can be stored in a 16-bit data register either left-aligned or right-aligned .
       An analog watchdog feature allows the application to detect if the input voltage exceeds a user-defined high/low threshold.
       The input clock of the ADC must not exceed 14MHz, which is generated by dividing the frequency of PCLK2.

3. ADC main features

  • 12-bit resolution
  • Interrupt on end of conversion, end of injected conversion, and analog watchdog event
  • Single and Continuous Conversion Modes
  • Auto scan mode from channel 0 to channel n
  • self-calibration
  • Data alignment with embedded data consistency
  • Sampling interval can be programmed individually by channel
  • Both rule transitions and injection transitions have external trigger options
  • intermittent mode
  • Dual Mode (Devices with 2 or More ADCs)
  • ADC conversion time:
    ─STM32F103xx enhanced products: 1μs when clock is 56MHz (1.17μs when clock is 72MHz)
    ─ STM32F101xx basic products: 1μs when clock is 28MHz (1.55μs when clock is 36MHz)
    ─ STM32F102xxUSB type products: clock 1.2μs at 48MHz
    ─ STM32F105xx and STM32F107xx products: 1μs at 56MHz clock (1.17μs at 72MHz clock)
  • ADC Power Requirements: 2.4V to 3.6V
  • ADC input range: VREF- ≤ VIN ≤ VREF+
  • A DMA request is generated during regular channel conversion.

4. The difference between regular channels and injection channels

STRT Regular channel Start flag (Regular channel Start flag)
JSTRT: Injected channel Start flag (Injected channel Start flag)
       When learning ADC, I have not understood why ADC channels are divided into regular channels and injection channels? This rule and injection are not only difficult to remember but also difficult to speak, and its meaning is even more puzzling. Consult the English reference manual to know that the Regular channel in English is translated as a regular channel, and the injected channel is translated as an injection channel. Well, in fact, the thinking of foreigners is really a bit different from ours. The meaning you want to express is bluntly translated into two words: rule and injection due to differences in culture and language. Well, the name is just for better expression. When describing something, just like Zhang San and Li Si, the meaning of its application is far greater than the meaning of the name. You don't need to pay too much attention to it. If you feel blunt, call it Regular and injected!
       After understanding the past and present life of the name, let's discuss the meaning of its expression. Here I quote the opinions of netizens who I think are better. Original link: http://bbs.ednchina.com/BLOG_ARTICLE_225912.HTM

-------------------------------------Dividing line----------- --------------------------------------------

Netizens' insights:
       Each ADC module of STM32 can switch to different input channels and convert through the internal analog multiplexer. STM32 has specially added a variety of group conversion modes, which can be automatically sampled and converted one by one for multiple analog channels after being set by the program.

       There are 2 ways to divide transformation groups: regular channel group and injection channel group. Usually, a maximum of 16 channels can be arranged in a regular channel group, and a maximum of 4 channels can be arranged in an injection channel group.
ADC functional description excerpt block diagram

       When performing a regular channel group scan conversion, the conversion that injects the channel group can be enabled if there is additional processing.
       An inappropriate analogy is that the conversion of the regular channel group is like the normal execution of the program, and the conversion of the injection channel group is like an interrupt handler outside the normal execution of the program.

Another example that is not necessarily used:
       if you put 5 temperature probes in the yard at home and 3 temperature probes indoors; you need to monitor the outdoor temperature all the time, but occasionally you want to check the indoor temperature; So you can use the regular channel group to cycle through 5 probes outside and display the AD conversion results, when you want to see the indoor temperature, start the injection conversion group (3 indoor probes) with a button and temporarily display the indoor temperature, when you put After this button is turned on, the system will return to the regular channel group to continue to detect the outdoor temperature.

       From the perspective of system design, the process of measuring and displaying the indoor temperature interrupts the process of measuring and displaying the outdoor temperature, but in the program design, different conversion groups can be set up in the initialization stage, and the configuration of the cyclic conversion does not need to be changed during the system operation, so that Achieve the result that the two tasks do not interfere with each other and switch quickly. It can be imagined that if there is no division of rule groups and injection groups, when you press the button, you need to reconfigure the channels of AD cycle scan, and then you need to configure the channels of AD cycle scan again after releasing the button.

       The above example cannot fully reflect the benefits of this distinction (rule group and injection group) because of its slow speed, but in industrial applications there are many detection and monitoring probes that need to be processed faster, so the grouping of AD conversions will simplify events. process and increase the speed of event processing.

-------------------------------------Dividing line----------- --------------------------------------------

5. Blue Bridge Cup inspects ADC points

       CT117E embedded competition board (in the case of not using the extended version), it can be seen from the circuit schematic diagram of CT117Ev1.1 that the acquisition voltage pin PB0 of the ADC adjusts the voltage in the voltage divider circuit through the R37 potentiometer.
       
insert image description here
Check out the Datasheet of stm32f103rbt6
(1) Overall resource distribution
insert image description here
(2) ADC resource distribution
PC0~PC3
insert image description here
PA0~PA3
insert image description here
PA4~PA7; PC4~PC5; PB0~PB1 (this time PB0 pin is used!)
insert image description here

6. Understand the configuration ADC mode

(1) Let's start with the STM32 library function and take a look at
ADC_InitTypeDef ADC_InitStructure;

typedef struct
{
    
    
  uint32_t ADC_Mode;                      /*!< Configures the ADC to operate in independent or
                                               dual mode. 
                                               This parameter can be a value of @ref ADC_mode */

  FunctionalState ADC_ScanConvMode;       /*!< Specifies whether the conversion is performed in
                                               Scan (multichannels) or Single (one channel) mode.
                                               This parameter can be set to ENABLE or DISABLE */

  FunctionalState ADC_ContinuousConvMode; /*!< Specifies whether the conversion is performed in
                                               Continuous or Single mode.
                                               This parameter can be set to ENABLE or DISABLE. */

  uint32_t ADC_ExternalTrigConv;          /*!< Defines the external trigger used to start the analog
                                               to digital conversion of regular channels. This parameter
                                               can be a value of @ref ADC_external_trigger_sources_for_regular_channels_conversion */

  uint32_t ADC_DataAlign;                 /*!< Specifies whether the ADC data alignment is left or right.
                                               This parameter can be a value of @ref ADC_data_align */

  uint8_t ADC_NbrOfChannel;               /*!< Specifies the number of ADC channels that will be converted
                                               using the sequencer for regular channel group.
                                               This parameter must range from 1 to 16. */
}ADC_InitTypeDef;

7. Configure ADC template

Steps to configure ADC:

1. Configure ADC-related GPIO pins, such as collecting voltage signals, GPIO pins are configured as analog inputs.

GPIO_InitTypeDef    GPIO_InitStructure; 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);

2. ADC configuration

  • ADC mode (independent mode, scan mode, continuous conversion mode)

    ADC_InitTypeDef     ADC_InitStructure;
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;      //扫描模式
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; 
    /* 连续转换模式---不难理解---就是不停地采集---一次接一次 */
    
  • make without using an external trigger? (ADC_ExternalTrigConv); Triggers are divided into external triggers, such as interrupts and timers. Software trigger -> use dedicated function.

  •   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    
  • The alignment of data collected by the ADC and the number of channels converted

  •   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//采集的数据右对齐---方便计算
      ADC_InitStructure.ADC_NbrOfChannel = 1;  //总共需要转换的通道个数
      ADC_Init(ADC1, &ADC_InitStructure);
    
  • To ADC_Init(ADC1, &ADC_InitStructure); In this step, we initialize the structure DC_InitStructure that we worked hard to configure.

Error summary: I encapsulated the various configurations of ADC initialization into a function, which is not a problem, but I named this function ADC_Init() and there was an error, because this function name has been used, ADC_Init(ADC1 , &ADC_InitStructure); so changed to adc_Init()

  /* ADC1 regular channel_8 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_13Cycles5);
  ADC_Cmd(ADC1, ENABLE);
  ADC_ResetCalibration(ADC1);
  while(ADC_GetResetCalibrationStatus(ADC1));
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));

8. Program code

Implementation function: AD samples the voltage on PB0 every 100ms and calculate the voltage value (retain 2 decimals), and display it on the Line1 line of the LCD.
Reference blog : https://blog.csdn.net/Zach_z/article/details /80548423
(1) ADC.c file


/* PB0———ADC_IN8 / TIM3_CH3  */

#include "ADC.h"

void adc_Init()
{
    
    
	ADC_InitTypeDef     ADC_InitStructure;
	GPIO_InitTypeDef    GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOB,ENABLE);
	
   /*配置ADC相应的GPIO,这里配置PB0(ADC_IN8)作为模拟输入------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	/* ADC1 configuration -----------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;      //扫描模式
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //连续转换模式---不难理解---就是不停地采集---一次接一次
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	/* 不使用外部触发转换---触发分为外部触发---比如中断与定时器。
	软件触发---后面有专用函数 */
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//采集的数据右对齐---方便计算
  ADC_InitStructure.ADC_NbrOfChannel = 1;  //总共需要转换的通道个数
  ADC_Init(ADC1, &ADC_InitStructure);
	
	  /* ADC1 regular channel_8 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_13Cycles5);
  ADC_Cmd(ADC1, ENABLE);
	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));
	
}


u16 get_adc()
{
    
    
	u16 value;
	
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	while(!ADC_GetFlagStatus(ADC1,  ADC_FLAG_EOC));
	value = ADC_GetConversionValue(ADC1);
	
	return value;
}

(2) main.c file

#include "stm32f10x.h"
#include "stdio.h"
#include "ADC.h"

void Delay_ms(unsigned int ms)
{
    
    
  unsigned int i,j;
  
  for(i=0; i<ms; i++)
  {
    
    
    for(j=0; j<8450; j++)  ;
  }
}

int main()
{
    
    
	 u16 adc_value;
     u8 str[20];
	 STM3210B_LCD_Init();
	 Delay_ms(100);
	 LCD_SetTextColor(White);
     LCD_SetBackColor(Black);
	 LCD_ClearLine(Line0);
     adc_Init();
    while(1)
    {
    
    
       adc_value=get_adc();
       sprintf(str," PB0: %.2f V",(float)adc_value/4096*3.3);
       LCD_DisplayStringLine(Line1, str);
       Delay_ms(100);
		}

}

Guess you like

Origin blog.csdn.net/Eterlove/article/details/122619260