STM32双重ADC快速交叉模式采集一个规则组通道数据,采用定时器触发主ADC,软件触发从ADC,库函数编写
时间: 2023-06-14 20:03:19 浏览: 324
下面是采用定时器触发主ADC,软件触发从ADC的库函数编写代码:
```c
#include "stm32f10x.h"
/* 定义ADC采样时间 */
#define ADC_SAMPLE_TIME ADC_SampleTime_239Cycles5
/* 定义ADC转换结果存储数组 */
__IO uint16_t ADC_ConvertedValue[2];
int main(void)
{
/* 定义GPIO初始化结构体 */
GPIO_InitTypeDef GPIO_InitStructure;
/* 定义NVIC初始化结构体 */
NVIC_InitTypeDef NVIC_InitStructure;
/* 定义定时器初始化结构体 */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* 定义ADC初始化结构体 */
ADC_InitTypeDef ADC_InitStructure;
/* 定义DMA初始化结构体 */
DMA_InitTypeDef DMA_InitStructure;
/* 使能GPIOA和DMA1时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* 配置GPIOA.0和GPIOA.1为模拟输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 使能DMA1通道1时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* 配置DMA1通道1 */
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &ADC_ConvertedValue[1];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* 配置定时器2 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 35999; // 定时器周期为36000
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* 配置NVIC中断优先级 */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* 配置ADC1 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* 配置ADC1规则通道0 */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SAMPLE_TIME);
/* 配置ADC2 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
/* 配置ADC2规则通道1 */
ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SAMPLE_TIME);
/* 使能DMA1通道1中断 */
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
/* 使能DMA1通道1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* 启动定时器2 */
TIM_Cmd(TIM2, ENABLE);
/* 启动ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* 启动ADC2 */
ADC_Cmd(ADC2, ENABLE);
while (1)
{
/* 等待DMA1通道1中断 */
while (DMA_GetITStatus(DMA1_IT_TC1) == RESET);
/* 清除DMA1通道1中断标志位 */
DMA_ClearITPendingBit(DMA1_IT_TC1);
/* 交换ADC转换结果存储数组指针 */
if (ADC_ConvertedValue[0] > ADC_ConvertedValue[1])
{
ADC_ConvertedValue[0] = ADC_ConvertedValue[1];
}
}
}
/* DMA1通道1中断服务函数 */
void DMA1_Channel1_IRQHandler(void)
{
/* 清除DMA1通道1中断标志位 */
DMA_ClearITPendingBit(DMA1_IT_TC1);
/* 触发ADC2转换 */
ADC_SoftwareStartConv(ADC2);
}
```
代码中使用了定时器2作为主ADC的触发信号,每次定时器溢出后触发一次主ADC转换;同时,使用软件触发从ADC的转换,当主ADC转换完成后,通过DMA将转换结果存储到数组中,并触发从ADC的转换。在主函数中,通过比较两个通道的转换结果,选择较小的值作为最终结果。需要注意的是,为了保证双重ADC的同步采样,需要将两个ADC的配置参数设置为相同。
阅读全文