当前位置:网站首页>ADC of stm32
ADC of stm32
2022-07-02 23:10:00 【What's your name】


List of articles
One 、ADC brief introduction
ADC (Analog-Digital Converter) simulation - Numbers converter
ADC The continuous changing analog voltage on the pin can be converted into digital variables stored in memory , Build a bridge from analog circuit to digital circuit .12 position Successive approximation type ADC**,1us Conversion time
Input voltage range ∶0-3.3V, Conversion result range ∶0~4095.18 Input channels , Can be measured 16 Outside and 2 There are two conversion units of internal signal source rule group and injection group
Simulation watchdog Automatically monitor the input voltage range **
12 position ADC Value , The scope of quantification is 0 - 2^12-1 , namely 0-4095. The higher the number of digits . The more refined the quantitative results . The higher the corresponding resolution .
About this simulated watchdog , We can set the threshold . When AD When the value is higher than the upper threshold or lower than the lower threshold , It will apply for interruption , You can perform the corresponding operation in the interrupt function .
1、 Successive approximation type ADC
STM32 Of ADC The principle of is the same as this , This is ADC0809 The internal structure of 
First, select the channel switch . There is a channel selector switch on the left . The number of the selected channel exists below ADDA,ADDB,ADDC in . You want to choose IN0-IN7 Which channel in , Just put the channel number in these three .ALE It is a latch signal , to ALE Set up , The corresponding path switch here above can be automatically dialed .
Then came to the upper line of the comparator .
Use the successive approximation method in the comparator to compare one by one
The function of the comparator is to judge the relationship between the two input signal voltages .
The upper line of the comparator is input by an external channel , Unknown encoded voltage .
The lower line of the comparator is a DAC Output , Known encoded voltage .
If DAC The output voltage is relatively small . I will grow DAC data . until DAC The output voltage is equal to the input voltage of the external channel . such DAC The input data is the encoded data of the external voltage .( Here is the input to 8 Bit tristate latch buffer )
And this increase or decrease to adjust DAC The process of data , It's the successive approximation register SAR It's working .
It usually uses dichotomy to find , For example, here is 8 Bit ADC, Namely 0-255. When comparing for the first time , We'll give it to DAC Input 255 Half of . The second comparison . Give it again 128 Half of 64. Just continue to compare . and 128,64,32 Just the bit weight of each bit in binary . in other words , This process of comparison , It is to judge binary from high to low in order 1 yes 0 The process of . The for 8 Bit ADC, Judge from high to low 8 The code of unknown voltage can be found once .12 Bit is judgment 12 Time .
EOC yes End Of Gonvert Conversion end signal
START Is to start the conversion . Give an input pulse , Start conversion .
CLOCK yes ADC The clock .
below .VRVF+ and VREF- yes DAC The reference voltage of .
For example, you give a data 255. It's corresponding to 5V still 3.3V Well , It depends on the reference voltage .
This DAC The reference voltage of also determines ADC Input range of , So it's also ADC Reference voltage .
Generally, the positive pole of the reference voltage and VCC It's the same , Will connect . The negative pole of the reference electricity and GND It's the same . Also connected .
2、ADC Block diagram


3、ADC The basic structure

On the left is the input channel ,16 individual GPO mouth , Plus two internal channels .
Then enter AD converter
AD There are two groups in the converter , One is the rule group , One is the injection group
Rule groups can be selected at most 16 Channels . The injection group can select at most 4 Channels .
Then the conversion results can be stored in AD In the data register , The rule group has 1 Data registers , The injection group has 4 Data registers .
Then here is the trigger control . Provides the... To start the conversion START The signal .
Trigger control can select hardware trigger and software trigger , Hardware trigger mainly comes from timer and external interrupt trigger .
On the right here is from RCC Of ADC The clock CLOCK,ADC The process of successive comparison is driven by this clock
There are three ways to apply for interruption , First of all, there will be EOC The signal , A flag bit will be set . Next is the watchdog , If the threshold is exceeded , Control through interrupt output , image NVIC Application interruption .
4、 Conversion mode



Rule group 
Trigger — Enter the first sequence position ( Choose 1 individual )---- Then the conversion is finished
At this point, a EOC The signal of
If you want to convert, you need to trigger again

Trigger — Enter the first sequence position ( Choose 1 individual )---- This conversion is completed ----- Generate EOC ----- Do not need to trigger and continue to repeat the above process

This mode is also triggered once
But there are several sequences at a time
So give a number of channels , There are several sequences that can be used , There are several from the sequence 1 Start to row down a few
After each trigger , Just before this 7 individual ( Number of channels ) Conduct AD transformation , And to prevent data coverage , Use it in time DMA Remove the data
that 7 After the conversion of channels , produce EOC The signal , End of conversion .

This is the above process , When triggered, it keeps turning
5、 Trigger control


6、 Data alignment

Generally, it is right aligned , Add in front 0.
There are also cases of left alignment
7、 Channel sampling time



8、 calibration
1、ADC There is a built-in self calibration mode . Calibration can greatly reduce the quasi precision error caused by the change of internal capacitor bank . During calibration , An error correction code will be calculated on each capacitor ( Numerical value ), This code is used to eliminate the error on each capacitor in the subsequent conversion .
2、 It is recommended to perform a calibration after each power on .
3、 Before starting calibration ,ADC Must be in power off state for at least two ADC Clock cycle .
Two 、 Code
The board I use is a mini board of punctual atoms
STM32 The model number is STM32F103RCT
There are three ADC

We use it here ADC1 The passage of 1
1、 Some functions





ADC Rule group channel configuration
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
Whether to allow external trigger conversion
void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
Get the conversion value
uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
double ADC Mode read conversion result
uint32_t ADC_GetDualModeConversionValue(void);

2、ADC initialization
#include "stm32f10x.h"
void Adc_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
//1、 Turn on the clock ADC1 Clock and GPIOA The clock of
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );
//2、 Configure frequency division , Because the maximum cannot exceed 14MHz 72/6=12
RCC_ADCCLKConfig(RCC_PCLK2_Div6); // Set up ADC Division factor 6 72M/6=12,ADC The maximum time cannot exceed 14M
//3、 To configure GPIOA and ADC Structure PA1 As an analog channel input pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // Analog input pins
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1); // Reset ADC1
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC Working mode :ADC1 and ADC2 Working in independent mode
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // Analog to digital conversion works in single channel mode
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // A / D conversion works in single conversion mode
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // The transformation is initiated by software rather than external triggers
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC Data right alignment
ADC_InitStructure.ADC_NbrOfChannel = 1; // In order to change the rules ADC The number of channels
ADC_Init(ADC1, &ADC_InitStructure); // according to ADC_InitStruct The parameter specified in ADCx The register of
//4、 Can make ADC1
ADC_Cmd(ADC1, ENABLE);
//5、 Calibrate and wait for the calibration to end
ADC_ResetCalibration(ADC1); // Enable reset calibration
while(ADC_GetResetCalibrationStatus(ADC1)); // Wait for the reset calibration to finish
ADC_StartCalibration(ADC1); // Turn on AD calibration
while(ADC_GetCalibrationStatus(ADC1)); // Wait for the calibration to finish
}
About frequency division 

3、 Experimental acquisition PA1 And display
First PA1 yes ADC passageway 1
So in this function ADC_Channel Select as shown in the figure
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)

adc.c
#include "stm32f10x.h"
void Adc_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
//1、 Turn on the clock ADC1 Clock and GPIOA The clock of
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );
//2、 Configure frequency division , Because the maximum cannot exceed 14MHz 72/6=12
RCC_ADCCLKConfig(RCC_PCLK2_Div6); // Set up ADC Division factor 6 72M/6=12,ADC The maximum time cannot exceed 14M
//3、 To configure GPIOA and ADC Structure PA1 As an analog channel input pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // Analog input pins
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1); // Reset ADC1
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC Working mode :ADC1 and ADC2 Working in independent mode
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // Analog to digital conversion works in single channel mode
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // A / D conversion works in single conversion mode
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; // The transformation is initiated by software rather than external triggers
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC Data right alignment
ADC_InitStructure.ADC_NbrOfChannel = 1; // In order to change the rules ADC The number of channels
ADC_Init(ADC1, &ADC_InitStructure); // according to ADC_InitStruct The parameter specified in ADCx The register of
//4、 Can make ADC1
ADC_Cmd(ADC1, ENABLE);
//5、 Calibrate and wait for the calibration to end
ADC_ResetCalibration(ADC1); // Enable reset calibration
while(ADC_GetResetCalibrationStatus(ADC1)); // Wait for the reset calibration to finish
ADC_StartCalibration(ADC1); // Turn on AD calibration
while(ADC_GetCalibrationStatus(ADC1)); // Wait for the calibration to finish
}
uint16_t AD_GetValue(u8 ch)
{
// Set the specified ADC The rule group channel for , A sequence , Sampling time
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC passageway , The sampling time is 239.5 cycle
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Enable to designate ADC1 Software conversion start function
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));// Wait for the conversion to finish
return ADC_GetConversionValue(ADC1); // Go back to the last time ADC1 Conversion result of rule group
}
// It is more stable to obtain the average value by obtaining more data
uint16_t Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=AD_GetValue(ch);
delay_ms(5);
}
return temp_val/times;
}
main.c
#include "stm32f10x.h"
#include "delay.h"
#include "ADC.h"
#include "lcd.h"
int main(void)
{
float temp;
u16 adcx;
delay_init(); // Delay function initialization
LCD_Init();
Adc_Init();
LCD_ShowString(10,70,200,16,16,"AD_GetValue");
LCD_ShowString(10,100,200,16,16,"AD_Average");
LCD_ShowString(10,150,200,16,16,"ADC_CH1_VOL:0.000V");
while(1)
{
LCD_ShowxNum(110,70,AD_GetValue(ADC_Channel_1),4,16,0);
LCD_ShowxNum(110,100,Get_Adc_Average(ADC_Channel_1,10),4,16,0);
adcx=Get_Adc_Average(ADC_Channel_1,10);
temp=(float)adcx*(3.3/4096);
adcx=temp;
LCD_ShowxNum(106,150,adcx,1,16,0);// Show integers
temp-=adcx;
temp*=1000;
LCD_ShowxNum(122,150,temp,3,16,0X80); // Display decimal
}
}
Pick up 3.3v And then GND And if not, it's floating , The level is unstable and keeps changing 


边栏推荐
- Application of containerization technology in embedded field
- [Solved] Splunk: Cannot get username when all users are selected“
- 为什么RTOS系统要使用MPU?
- Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
- Distributed monitoring system ZABBIX
- [NPUCTF2020]ezlogin xPATH注入
- easyclick,ec权朗网络验证源码
- 密码技术---分组密码的模式
- 成功改变splunk 默认URL root path
- [adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
猜你喜欢
![[NPUCTF2020]ezlogin xPATH注入](/img/6e/dac4dfa0970829775084bada740542.png)
[NPUCTF2020]ezlogin xPATH注入

Qt QSplitter拆分器

海思 VI接入视频流程

MySQL查询附近的数据.并按距离进行排序.

Chow-Liu Tree

Explain promise usage in detail

Brief introduction of emotional dialogue recognition and generation

Learning records of data analysis (II) -- simple use of response surface method and design expert

MySQL reset password, forget password, reset root password, reset MySQL password

Webrtc audio and video capture and playback examples and mediastream media stream analysis
随机推荐
详解Promise使用
Kubernetes uses the host name to allocate the pod on the specified node
設置單擊右鍵可以選擇用VS Code打開文件
PMP project integration management
在SOUI里使用真窗口时使用SOUI的滚动条
MySQL查询附近的数据.并按距离进行排序.
LeetCode 968. 监控二叉树
密码技术---分组密码的模式
Detailed explanation and application of merging and sorting
Splunk audit 的设定
2016. 增量元素之间的最大差值
Distributed monitoring system ZABBIX
编辑卡顿
數據分析學習記錄--用EXCEL完成簡單的單因素方差分析
Jerry's built-in short press and long press, no matter how long it is, it is a short press [chapter]
Go语言sqlx库操作SQLite3数据库增删改查
Generics and reflection, this is enough
How does Jerry test the wrong touch rate of keys [chapter]
Lc173. Binary search tree iterator
门牌制作 C语言