` 本帖最后由 eyeahe 于 2017-1-9 21:32 编辑
前段时间比较忙,耽搁了。这周分享一下STM32F4自带外设ADC的使用,结合着上一篇LCD1602。这次的功能是利用ADC1的一个通道把电压采集过来通过LCD1206来显示。
(动图做过处理速度比正常速度快,不然体积太大传不了)
程序的思路是使用ADC1的通道三(PA3引脚)把数据采集过来
利用DMA2 Stream0 Channel0 把数据保存到ADC1_DMA_Buf1[8]数组中
在DMA传输完成中断中把标志位置1,在主程序中判断标志位,如果数据传输完成就对保存下来的八个数据做平均,然后把数据显示出来
具体代码如下
bsp_adc.h头文件
- #ifndef __BSP_ADC_H
- #define __BSP_ADC_H
- #define ADC1_DR_Address ((uint32_t)ADC1_BASE+0x4c)
- void bsp_InitAdc(void);
- void ADC1_Config(void); //ADC1初始化及引脚初始化
- void DMA_Config(void); //DMA的初始化
- #endif
复制代码
bsp_adc.c
- #include "bsp.h"
- void ADC1_Config(void)
- {
- /*使能GPIOA时钟*/
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- /*初始化GPIOA3端口为模拟量的输入*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//不带上下拉
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
- ADC_InitTypeDef ADC_InitStructure;
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
-
- ADC_DeInit();
-
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
- ADC_InitStructure.ADC_ScanConvMode = DISABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
-
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
- ADC_InitStructure.ADC_NbrOfConversion = 1;//选用转换通道数为1
- ADC_Init(ADC1, &ADC_InitStructure);//将以上设置应用于对应的寄存器
-
- ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
- ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
- //两个采样阶段之间的延迟5个时钟
- ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
- //禁止多重ADC DMA
- ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
- //预分频2分频。ADCCLK = PCLK2/2 = 42/2 = 21Mhz,ADC时钟最好不要超过36Mhz
- ADC_CommonInit(&ADC_CommonInitStructure);//初始化
-
- ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_480Cycles);
-
- ADC_Cmd(ADC1, ENABLE);//开启AD转换器
-
- ADC_DMACmd(ADC1,ENABLE);//使能ADC1的DMA模式
-
- ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);//使能DMA的数据快速转移
- }
- void DMA_Config(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
- DMA_DeInit(DMA2_Stream0);
- while (DMA_GetCmdStatus(DMA2_Stream0) != DISABLE);//等待可配置
- DMA_InitStructure.DMA_Channel = DMA_Channel_0;//选取DMA通道0,数据流0
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
- //数据传入地址->ADC基地址加上DR寄存器偏移地址
- DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC1_DMA_Buf1;//数据送入地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//数据传送方向为外设到寄存器
- DMA_InitStructure.DMA_BufferSize = 8;//数据缓冲区1
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址固定
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存地址自增
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- //数据类型为半字16位
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
-
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环缓存模式
- DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//最高优先级
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//不使用FIFO模式
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_Init(DMA2_Stream0, &DMA_InitStructure);
- //将以上的设置应用于DMA2,通道0,数据流0
- DMA_Cmd(DMA2_Stream0, ENABLE);//使能DMA
-
-
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//抢占优先级0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //子优先级0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道使能
- NVIC_Init(&NVIC_InitStructure);//根据指定的参数初始化VIC寄存器、
- DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);//DMA中断开
- }
- void DMA2_Stream0_IRQHandler(void)
- {
- if(DMA_GetITStatus(DMA2_Stream0,DMA_IT_TCIF0) != RESET)
- //判断传输完成中断标志
- {
- DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);//清除传输完成中断标志
- Buf_Ok = 1;
- }
- }
- void bsp_InitAdc(void)
- {
- ADC1_Config();
- DMA_Config();
- ADC_SoftwareStartConv(ADC1);
- }
复制代码
主程序
- #include "bsp.h" /* 底层硬件驱动 */
- uint16_t AD_Val;
- float temp1, temp2, temp3;
- int main(void)
- {
- /*
- ST固件库中的启动文件已经执行了 SystemInit() 函数,该函数在 system_stm32f4xx.c 文件,主要功能是
- 配置CPU系统的时钟
- */
- bsp_Init(); /* 硬件初始化 */
- LCD_Write_String(0, 0, "Voltage Meter");
- LCD_Write_String(0, 1, "Voltage is 0.00V");
- while(1)
- {
- if(Buf_Ok)
- {
- AD_Val = ADC1_DMA_Buf1[ 0] + ADC1_DMA_Buf1[ 1] + ADC1_DMA_Buf1[ 2] + ADC1_DMA_Buf1[3]
- +ADC1_DMA_Buf1[ 4] + ADC1_DMA_Buf1[ 5] + ADC1_DMA_Buf1[ 6] + ADC1_DMA_Buf1[7];
- Buf_Ok = 0;
- temp1 = (AD_Val >> 3) * (3.3 / 4095);
- GPIO_ToggleBits(GPIOB, GPIO_Pin_0);
- LCD_Write_Char(11, 1,(uint8_t)temp1 + 0x30);
- temp2 = (temp1 - ((int)temp1)) * 10;
- Delay(40);
- LCD_Write_Char(13, 1,(uint8_t)temp2 + 0x30);
- temp3 = (temp2 - (int)temp2) * 10;
- Delay(40);
- LCD_Write_Char(14, 1,(uint8_t)temp3 + 0x30);
- Delay(40);
- }
- }
- }
复制代码
`
|