1.接线图:
PA0接电位器,PA1~PA3接三个传感器模块的AO输出。
代码总逻辑:将AD多通道代码和DMA转运部分代码结合起来,修改参数,即可完成AD+DMA转运
2.AD.c代码部分:
#include "stm32f10x.h" // Device header uint16_t AD_Value[4]; void AD_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//将RCC开启DMA放在此处 RCC_ADCCLKConfig(RCC_PCLK2_Div6); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);//通道选择,选择4个 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5); ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续扫描 ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式是连续扫描,不再是只要第一个了 ADC_InitStructure.ADC_NbrOfChannel = 4;//规定有效扫描范围4 ADC_Init(ADC1, &ADC_InitStructure); DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; //外设基地址,ADC.DR寄存器的地址,使用取地址符&,并使用指针->表示参数 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //低十六位数据,转运半字,16位。 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //不自增,始终转运同一位置 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)AD_Value;//存储器基地址(目的地地址) DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 4; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //不使用软件触发,使用硬件触发,触发源是ADC1 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_Cmd(DMA1_Channel1, ENABLE); ADC_DMACmd(ADC1, ENABLE);//开启DMA触发信号 ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while (ADC_GetResetCalibrationStatus(ADC1) == SET); ADC_StartCalibration(ADC1); while (ADC_GetCalibrationStatus(ADC1) == SET); ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
3.AD.h代码部分如下:
#ifndef __AD_H #define __AD_H extern uint16_t AD_Value[4]; void AD_Init(void); #endif
4.主函数代码部分如下:
#include "stm32f10x.h" // Device header #include "Delay.h" #include "OLED.h" #include "AD.h" int main(void) { OLED_Init(); AD_Init(); OLED_ShowString(1, 1, "AD0:"); OLED_ShowString(2, 1, "AD1:"); OLED_ShowString(3, 1, "AD2:"); OLED_ShowString(4, 1, "AD3:"); while (1) { OLED_ShowNum(1, 5, AD_Value[0], 4); OLED_ShowNum(2, 5, AD_Value[1], 4); OLED_ShowNum(3, 5, AD_Value[2], 4); OLED_ShowNum(4, 5, AD_Value[3], 4); Delay_ms(100); } }