先祝大家新年快乐,新的一年里,划水不被捉,牛码不Bug,电脑不卡机,下班不漏卡.
这次使用ADC采集结果并使用DMA传输结果(有小弟当然要他搬砖了).最后OLED显示出来,还是上回的GPIO例程接着改,有关ADC的配置参数可以参考官方文档配置ADC配置教程和DMA配置例程,里面有每个函数的使用说明,DMA例程是储存器到储存器的,改一下就可以了.其详细初始化代码如下:
/*******************************************************************************
* Function Name : ADC_Function_Init
* Description : Initializes ADC collection.
* Input : None
* Return : None
*******************************************************************************/
void ADC_Function_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE );
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE); // ADC DMA 使能
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
/*******************************************************************************
* Function Name : DMA_Tx_Init
* Description : Initializes the DMAy Channelx configuration.
* Input : DMA_CHx:
* x can be 1 to 7.
* ppadr: Peripheral base address.
* memadr: Memory base address.
* bufsize: DMA channel buffer size.
* Return : None
*******************************************************************************/
void DMA_Tx_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1, ENABLE );
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->RDATAR;
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_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_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure );
DMA_Cmd(DMA1_Channel1, ENABLE);
}
main.c中声明一个全局变量__IO uint16_t ADC_Value;用来储存ADC的转换结果,ADC转换后会请求DMA传输到变量ADC_Value中.
由于转换为电压之后的结果是小数所以这里拆成两个整数 给OLED来显示,
uint16_t Vrl = 3.3f * ADC_Value / 4095.f;
uint16_t Vrl_t = ((3.3f * ADC_Value / 4095.f)*1000-Vrl);
可以看到电压结果已经输出了.第一张接VCC电压,第二张接MQ传感器电压.
值得一提的是如果采用5伏供电,那么ADC的电压范围就是0-5伏了,这点非常好.
https://m.eeworld.com.cn/bbs/forum.php?mod=attachment&aid=NTI1MDYzfDJlNmRjNGY5NzZmNjdhOTRlZjQ1MjE3ZjQxMmQxMThifDE3MzIyMjgxMTI%3D&request=yes&_f=.7z
感谢分享,期待后续的测评计划帖子哈,我也会持续关注你的哈!!!