#include "adc.h"
#include "delay.h"
#define ADC1_DR_Address ((u32)0x40012400+0x4c)
__IO uint16_t adcvalue[10];
__IO uint16_t ADC_ConvertedValue;
int adc;
void DMA_INIT(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2时钟使能
DMA_DeInit(DMA2_Stream0 );//数据流配置0
DMA_DeInit(DMA2_Stream4 ); //数据流配置1
DMA_InitStructure.DMA_Channel=0; //通道0,对应adc1
DMA_InitStructure.DMA_DIR= DMA_DIR_PeripheralToMemory;//从外设(adc)到暂时记忆 (adcvalue)
/**********************************************************************/
DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address;//外设地址
DMA_InitStructure.DMA_Memory0BaseAddr=(u32)&adcvalue;//暂时记忆
/**********************************************************************/
DMA_InitStructure.DMA_BufferSize=6000;//数据传输量 现在还不确定
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//外设非增量模式(固定)
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable; //存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //存储器数据长度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不开启FIFO模式
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; //模式全开
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
DMA_Init(DMA2_Stream0, &DMA_InitStructure); //初始化DMA Stream
DMA_Init(DMA2_Stream4, &DMA_InitStructure); //初始化DMA Stream
}
void GET_DMA(void )
{
DMA_Cmd(DMA2_Stream0, DISABLE);
DMA_Cmd(DMA2_Stream4, DISABLE);
//while (DMA_GetCmdStatus(DMA2_Stream0) != DISABLE){} //确保DMA可以被设置
DMA_Cmd(DMA2_Stream0, ENABLE);
DMA_Cmd(DMA2_Stream4, ENABLE);
DMA_SetCurrDataCounter(DMA2_Stream0,6000); //数据传输量6000,然并不确定
DMA_SetCurrDataCounter(DMA2_Stream4,6000); //数据传输量
}
void guan_DMA(void)
{
DMA_Cmd(DMA2_Stream0, DISABLE);
DMA_Cmd(DMA2_Stream4, DISABLE);
}
//初始化ADC
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
//先初始化ADC1通道5 IO口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;//PA5 通道5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PA5 通道5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC1复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //复位结束
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//初始化
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//非扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//关闭连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测,使用软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStructure.ADC_NbrOfConversion = 1;//1个转换在规则序列中 也就是只转换规则序列1
ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
ADC_Cmd(ADC1, ENABLE);//开启AD转换器
}
//获得ADC值
//ch: @ref ADC_channels
//通道值 0~16取值范围为:ADC_Channel_0~ADC_Channel_16
//返回值:转换结果
void Get_Adc(void)
{
//设置指定ADC的规则组通道,一个序列,采样时间
/*1*/ ADC_RegularChannelConfig(ADC1, 1, 1, ADC_SampleTime_480Cycles ); //ADC1,ADC通道,480个周期,提高采样时间可以提高精确度
/*2*/ ADC_RegularChannelConfig(ADC1, 2, 2, ADC_SampleTime_480Cycles );
/*3*/ ADC_RegularChannelConfig(ADC1, 3, 3, ADC_SampleTime_480Cycles );
/*4*/ ADC_RegularChannelConfig(ADC1, 4, 4, ADC_SampleTime_480Cycles );
/*5*/ ADC_RegularChannelConfig(ADC1, 5, 5, ADC_SampleTime_480Cycles );
/*6*/ ADC_RegularChannelConfig(ADC1, 6, 6, ADC_SampleTime_480Cycles );
/*7*/ ADC_RegularChannelConfig(ADC1, 7, 7, ADC_SampleTime_480Cycles );
/*8*/ ADC_RegularChannelConfig(ADC1, 8, 8, ADC_SampleTime_480Cycles );
/*9*/ ADC_RegularChannelConfig(ADC1, 9, 9, ADC_SampleTime_480Cycles );
/*10*/ADC_RegularChannelConfig(ADC1, 10, 10, ADC_SampleTime_480Cycles );
ADC_SoftwareStartConv(ADC1); //使能指定的ADC1的软件转换启动功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
adc=ADC_GetConversionValue(ADC1);
}
while(1)
{
Get_Adc();
GET_DMA();
//起始坐标,起始坐标,数值,长度,大小,模式。
LCD_ShowString(30,30,150,12,24,"drink");
LCD_ShowxNum(60,100,adcvalue[1],6,16,0);
LCD_ShowxNum(60,120,adcvalue[2],6,16,0);
LCD_ShowxNum(90,120,adcvalue[3],6,16,0);
LCD_ShowxNum(90,170,adc,6,16,0);
这个dma通道传输,无法将adc寄存器的置转到变量上,新手求解,大神在哪里》?
一周热门 更多>