stm32 ADC模数转换 ADC多通道 ADC DMA
通过调节电位器,改变AD转换值和电压值
STM32F1 ADC 配置步骤
1.使能GPIO时钟和ADC时钟
2.配置引脚模式为模拟输入
3.配置ADC的分频因子
4.初始化ADC参数,ADC_InitTypeDef
5.使能ADC
6.执行ADC校准
7.设置ADC软件启动
8.读取ADC转换值
9.设置ADC规则,采样时间等
10.使能ADC的软件转换
11.读取ADC转换结果
举例
u16 ADC_value(u8 time)
{
u8 i = 0;
u16 value;
for(i = 0; i < time; i++)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能ADC的软件转换
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != SET);
value += ADC_GetConversionValue(ADC1); //读取ADC转换结果
}
return value/time;
}
typedef struct
{
uint32_t ADC_Mode; //双模式选择
FunctionalState ADC_ScanConvMode; //扫描模式
FunctionalState ADC_ContinuousConvMode; //连续转换
uint32_t ADC_ExternalTrigConv; //注入通道的外部触发转换模式
uint32_t ADC_DataAlign; //数据对齐
uint8_t ADC_NbrOfChannel; //规则通道序列长度
}ADC_InitTypeDef;
void ADC_init()
{
GPIO_InitTypeDef gpio =
{
GPIO_Pin_1,
GPIO_Speed_50MHz,
GPIO_Mode_AIN //模拟输入
};
ADC_InitTypeDef adc =
{
ADC_Mode_Independent, //独立模式
DISABLE, //关闭扫描模式
DISABLE, //单次转换模式
ADC_ExternalTrigConv_None, //不用外部事件启动转换
ADC_DataAlign_Right, //右对齐
1, //通道数目1
};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE); //使能GPIO时钟和ADC时钟
GPIO_Init(GPIOA, &gpio); //配置引脚模式
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //配置ADC的分频因子 72/6=12(通常)
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5); //设置ADC规则,采样周期
ADC_Init(ADC1, &adc); //初始化ADC参数
ADC_Cmd(ADC1, ENABLE); //使能ADC
ADC_ResetCalibration(ADC1); //执行ADC复位校准
while(ADC_GetResetCalibrationStatus(ADC1) == SET); //等待校准完成
ADC_StartCalibration(ADC1); //执行ADC校准
while(ADC_GetCalibrationStatus(ADC1) == SET);
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ADC软件启动
}
int main(void)
{
ADC_init();
while(1)
{
adc = ADC_value(15); //15次平均值
printf("ADC_value is %d.\n", adc);
printf("vol is %.3fV.\n", adc * (3.3 / 4096)); //电压值
delay_ms(500);
led1 = ~led1;
}
}
电压计算
vol = ADC * (3.3 / 4096);
ADC的参考电压VREF+为3.3V。ADC为12位转换精度, 2^12为4096
DMA方式
ADC_DMACmd开启DMA,在ADC初始化之后
ADC_RegularChannelConfig通道配置,在ADC初始化之后
DMA_MemoryInc要设置为DMA_MemoryInc_Enable,存储器地址递增
DMA_BufferSize大小,是定义DMA_MemoryBaseAddr内存的大小。根据DMA_MemoryDataSize存储器数据宽度,HalfWord占16bit
#define CHANNEL_NUM 4
volatile u16 AD_Bufer[4];
volatile u8 adc1_ok;
//多通道配置。4路输入
void ADC_init()
{
...
ADC_InitTypeDef adc =
{
ADC_Mode_Independent, //独立模式
ENABLE, //开启扫描模式
ENABLE, //开启连续转换模式
ADC_ExternalTrigConv_None, //不用外部事件启动转换
ADC_DataAlign_Right, //右对齐
CHANNEL_NUM, //通道数目4
};
ADC_Init(ADC1, &adc);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_55Cycles5);
DMA_adc_init(ADC1, (u32)AD_Bufer, CHANNEL_NUM);
...
}
void DMA_adc_init(ADC_TypeDef *ADCx, u32 mem_addr, u32 size)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA控制器时钟
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
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);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADCx->DR; //外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = mem_addr; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = size; //4个缓存大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
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_ITConfig(DMA1_Channel1, DMA1_IT_TC1, ENABLE); //开启中断
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_DMACmd(ADCx, ENABLE); //开启ADC1 DMA采集
}
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC1) != RESET)
{
DMA_ClearITPendingBit(DMA1_IT_TC1);
adc1_ok = 1;
}
}
int main(void)
{
ADC_init();
while(1)
{
if(adc1_ok == 1)
{
v0 = AD_Bufer[0];
v1 = AD_Bufer[1];
v2 = AD_Bufer[2];
v3 = AD_Bufer[3];
printf("v0 is %.3fV.\n", v0 * (3.3 / 4096)); //电压值
printf("v1 is %.3fV.\n", v1 * (3.3 / 4096));
printf("v2 is %.3fV.\n", v2 * (3.3 / 4096));
printf("v3 is %.3fV.\n", v3 * (3.3 / 4096));
led1 = ~led1;
adc1_ok = 0;
}
delay_ms(500);
}
}