/**************************************************************
ADC PB1_ADC9
***************************************************************/
#include "STM32Lib\\stm32f10x.h"
#include "STM32Lib\\stm32f10x_adc.h"
#include "STM32Lib\\stm32f10x_dma.h"
#include "main.h"
u16 ADCCov[caiyangdianshu*2];
volatile bool ADC_Ok=FALSE;
static DMA_InitTypeDef DMA_InitStructure;
//static ADC_InitTypeDef ADC_InitStructure;
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* PA0,1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC1 */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //连续多通道模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换不受外界决定
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐
ADC_InitStructure.ADC_NbrOfChannel = 2; //扫描通道数
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 Regular Channel1 Configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5); //通道X,采用时间为55.5周期,1代表规则通道第1个
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5); //通道X,采用时间为55.5周期,2代表规则通道第2个
/* 允许ADC1的DMA模式 */
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd (ADC1, ENABLE); /* Enable ADC1 */
ADC_SoftwareStartConvCmd(ADC1,ENABLE);/* Start ADC1 Software Conversion */ //使能转换开始
}
//DMA的配置
void DMA_Configuration(void)
{
/* 允许 DMA1 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA通道1*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)( &(ADC1->DR)); //ADC1数据寄存器
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADCCov; //获取ADC的数组
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //片内外设作源头
DMA_InitStructure.DMA_BufferSize = caiyangdianshu*2; //每次DMA16个数据
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不增加
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址增加
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //半字
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //半字
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //非内存到内存
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); //DMA通道1传输完成中断
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
ADC_Cmd (ADC1, ENABLE);
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}
//重新允许DMA,
void DMAReConfig(void)
{
DMA_DeInit(DMA1_Channel1);
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
用到PA0 |