STM32双ADC交错采集代码标准库
时间: 2023-10-28 10:04:52 浏览: 129
以下是STM32双ADC交错采集的代码,使用标准库实现:
```
#include "stm32f10x.h"
ADC_InitTypeDef ADC_InitStructure;
uint16_t ADC1ConvertedValue[2];
uint16_t ADC2ConvertedValue[2];
void RCC_Configuration(void)
{
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_DeInit(ADC1);
ADC_DeInit(ADC2);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div6;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Init(ADC2, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_7Cycles5);
ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5);
ADC_ExternalTrigConvCmd(ADC1, DISABLE);
ADC_ExternalTrigConvCmd(ADC2, DISABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_ADEN));
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC_Configuration();
while(1)
{
ADC_SoftwareStartConv(ADC1);
ADC_SoftwareStartConv(ADC2);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC));
ADC1ConvertedValue[0] = ADC_GetConversionValue(ADC1);
ADC2ConvertedValue[0] = ADC_GetConversionValue(ADC2);
ADC_SoftwareStartConv(ADC1);
ADC_SoftwareStartConv(ADC2);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC));
ADC1ConvertedValue[1] = ADC_GetConversionValue(ADC1);
ADC2ConvertedValue[1] = ADC_GetConversionValue(ADC2);
}
}
```
这段代码配置了两个ADC实例,使用交错扫描模式进行采集,每次采集两个通道的数据,交错采集的原理是在采集两次后交替使用两个ADC实例,以提高采样速率。在主循环中,首先启动两个ADC实例进行采集,等待采集完成后,将结果存储在相应的数组中。然后再次启动采集,采集另外两个通道的数据,并存储在相应的数组中。这种方式可以实现比较高的采样速率,适用于一些要求高速采样的应用场景。
阅读全文