STM32F407 HAL库配置ADC_DMA三通道定时器触发
以下是配置定时器触发频率的关键代码,以TIM2触发ADC1为例,实现三通道DMA传输。假设系统时钟为168 MHz,目标采样率为10 kHz。
1. 定时器配置(TIM2)
TIM_HandleTypeDef htim2;
void MX_TIM2_Init(void)
{
htim2.Instance = TIM2;
htim2.Init.Prescaler = 83; // 分频系数
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 99; // 自动重载值
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim2);
// 配置主模式触发输出
TIM_MasterConfigTypeDef sMasterConfig = {0};
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; // 更新事件触发
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
}
触发频率计算:
$$f_{trigger} = \frac{f_{TIM}}{(PSC+1) \times (ARR+1)}$$
其中:
- $f_{TIM} = 84\text{ MHz}$(APB1定时器时钟)
- $PSC = 83$
- $ARR = 99$
- $f_{trigger} = \frac{84 \times 10^6}{(83+1) \times (99+1)} = 10\text{ kHz}$
2. ADC配置(三通道+DMA)
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc;
void MX_ADC1_Init(void)
{
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; // ADC时钟=21 MHz
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE; // 扫描模式
hadc1.Init.ContinuousConvMode = DISABLE; // 禁用连续转换
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; // 上升沿触发
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO; // TIM2触发
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 3; // 3个转换通道
hadc1.Init.DMAContinuousRequests = ENABLE; // 连续DMA请求
HAL_ADC_Init(&hadc1);
// 配置ADC通道(示例:通道0/1/2)
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Rank = 1; // 序列位置1
sConfig.Channel = ADC_CHANNEL_0;
sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
sConfig.Rank = 2; // 序列位置2
sConfig.Channel = ADC_CHANNEL_1;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
sConfig.Rank = 3; // 序列位置3
sConfig.Channel = ADC_CHANNEL_2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
}
void MX_DMA_Init(void)
{
__HAL_RCC_DMA2_CLK_ENABLE();
hdma_adc.Instance = DMA2_Stream0; // 使用DMA2 Stream0
hdma_adc.Init.Channel = DMA_CHANNEL_0; // ADC1对应通道0
hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc.Init.MemInc = DMA_MINC_ENABLE; // 内存地址自增
hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc.Init.Mode = DMA_CIRCULAR; // 循环模式
hdma_adc.Init.Priority = DMA_PRIORITY_HIGH;
HAL_DMA_Init(&hdma_adc);
__HAL_LINKDMA(&hadc1, DMA_Handle, hdma_adc); // 关联ADC和DMA
}
3. 启动转换
uint16_t adc_buffer[3]; // 存储三通道数据的缓冲区
void Start_Conversion(void)
{
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buffer, 3); // 启动DMA传输
HAL_TIM_Base_Start(&htim2); // 启动定时器触发
}
关键参数调整
-
修改采样率:
调整PSC和ARR值,例如:- 20 kHz采样率:
PSC=41,ARR=99
$$f_{trigger} = \frac{84\text{ MHz}}{(41+1) \times (99+1)} = 20\text{ kHz}$$
- 20 kHz采样率:
-
ADC采样时间:
确保满足最小采样时间要求(取决于输入阻抗),例如:sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; // 缩短采样时间 -
时钟验证:
通过RCC配置确认实际时钟:RCC_ClkInitTypeDef clkconfig; HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
注意:
- 定时器时钟源需与APB1总线匹配(STM32F407中TIM2挂载在APB1)
- 使用
HAL_ADC_ConvCpltCallback处理转换完成中断- DMA缓冲区大小需匹配通道数量(本例中
uint16_t[3])
STM32F407 ADC DMA三通道定时触发配置
6454

被折叠的 条评论
为什么被折叠?



