资源简介
在用STM32做开发或学习的时候难免需要同时采集多路AD信号,在此仅以两路AD配置为例,多路AD按照相同的方法配置。
代码片段和文件信息
/*********************************************************************************
* Copyright: (C) 2016 SCUEC
* All rights reserved.
*
* Filename: stm32_adc.c
* Description:
*
* Version: 1.0.0(31/10/16)
* Author: LI WJNG
* ChangeLog: 1 Release initial version on “31/10/16 17:59:18“
*
********************************************************************************/
#include “stm32f4_adc.h“
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C) //ADC1 DR寄存器基地址
extern u16 ADCvalue[2];//两路DMA通道
void ADC_config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_4; //配置PA6 和 PA4 分别为AD通道6和通道4的输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1 ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1 DISABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;//开启扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//开启连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 2;//设定要转换的AD通道数目
ADC_Init(ADC1 &ADC_InitStructure);
/*******配置ADC1的通道6和通道4的转换先后顺序以及采样间隔时间为15个采样周期*******/
ADC_RegularChannelConfig(ADC1 ADC_Channel_6 1 ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC1 ADC_Channel_4 2 ADC_SampleTime_15Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1 ENABLE);
ADC_DMACmd(ADC1 ENABLE);
ADC_Cmd(ADC1 ENABLE);
ADC_SoftwareStartConv(ADC1);
}
void DMA_config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 ENABLE);
DMA_DeInit(DMA2_Stream0);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralbaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0baseAddr = (uint32_t)&ADCvalue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 2; //设定要转换的DMA通道数目
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructur
- 上一篇:Zedboard的USB转串口驱动
- 下一篇:换装小游戏flash源码
评论
共有 条评论