使用stm32cumemx配置stm32f103ze的PF8脚做ADC采集,使用DMA方式,详细配置步骤及代码
时间: 2023-06-15 11:03:24 浏览: 433
以下是配置步骤及代码:
1. 配置ADC外设和GPIO外设
```c
/* 使能 ADC1 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* 使能 GPIOF 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
/* 配置 PF8 为模拟输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* 配置 ADC1 */
ADC_InitTypeDef ADC_InitStructure;
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 通道 6 */
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_55Cycles5);
```
2. 配置DMA外设
```c
/* 使能 DMA1 时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(ADC1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) ADC_Value;
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_Enable;
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);
/* 使能 DMA 通道 */
DMA_Cmd(DMA1_Channel1, ENABLE);
```
3. 启动ADC采集和DMA传输
```c
/* 启动 ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* 启动 DMA 传输 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* 启动 ADC1 转换 */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
```
4. 在中断处理函数中处理数据
```c
void DMA1_Channel1_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC1))
{
/* 清除 DMA1 通道 1 的传输完成中断标志位 */
DMA_ClearITPendingBit(DMA1_IT_TC1);
/* 处理数据 */
uint16_t adc_value = ADC_Value[0];
}
}
```
完整代码如下:
```c
#include "stm32f10x.h"
uint16_t ADC_Value[1];
int main(void)
{
/* 使能 ADC1 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* 使能 GPIOF 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
/* 配置 PF8 为模拟输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* 配置 ADC1 */
ADC_InitTypeDef ADC_InitStructure;
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 通道 6 */
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_55Cycles5);
/* 使能 DMA1 时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(ADC1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) ADC_Value;
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_Enable;
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);
/* 使能 DMA 传输完成中断 */
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
/* 使能 DMA 通道 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* 启动 ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* 启动 ADC1 转换 */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (1)
{
}
}
void DMA1_Channel1_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_IT_TC1))
{
/* 清除 DMA1 通道 1 的传输完成中断标志位 */
DMA_ClearITPendingBit(DMA1_IT_TC1);
/* 处理数据 */
uint16_t adc_value = ADC_Value[0];
}
}
```
阅读全文