ADC1 使用DMA2传输数据,DMA中断了,但数据全是“000”,ADC输入有电压的。不知问题在什么地方,请高手帮忙看看,谢谢!
[mw_shl_code=c,true]
//初始化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_5;//PA5 通道5
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); //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;//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 = ENABLE;//连续转换
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);//ADC1初始化
ADC_RegularChannelConfig(ADC1, 5, 1, ADC_SampleTime_144Cycles ); // 144 ADC1通道,480个周期
ADC_DMACmd(ADC1, ENABLE); //使能ADC1的DMA
ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
ADC_Cmd(ADC1, ENABLE);//开启AD转换器
ADC_SoftwareStartConv(ADC1); //使能指定的ADC1的软件转换启动功能
ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE); //源数据变化时开启DMA传输
=======下面是DMA部分程序======
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C)
void NVIC_Config(void);
void MYDMA_Config(u32 AD1_Value);
void NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
/* DMA中断配置 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //嵌套优先级分组为 1
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn; //嵌套通道为DMA2_Stream0_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢jian优先级 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);
}
void MYDMA_Config(u32 AD1_Value) //DMA配置函数
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_DeInit(DMA2_Stream0);
DMA_StructInit( &DMA_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //选择Channel_0
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;// 数据传输的外设首地址,ADC1
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&AD1_Value;//ADCoverVaule; //自己定义待发送到数据首地址,强制转换为32位
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //数据传输方式选择为外设到内存
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_BufferSize =510; //传输数据大小为42,单位由以下确定,大小要配合定义的数组类型和外设数据类型
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址禁止增加
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址自己增加打开
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //选外设ADC1_DR为16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //选16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA选Circular,循环传输
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级High
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE); //使能DMA2_Stream0通道
DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE); /* DMA中断开 */
ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE); //源数据变化时开启DMA传输
}
//DMA2中断
//中断发生了,AD1_Value[520] 内全是0
void DMA2_Stream0_IRQHandler(void)
{
if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
}
}
下面是主程序;
u16 AD1_Value[520];
u16 AD2_Value[520];
int main(void)
{
u16 adcx=0,adcx1=0;
u16 adjs=0,adjs1=0;
float temp;
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LED_Init(); //初始化LED
LCD_Init(); //初始化LCD接口
Adc_Init(); //初始化ADC
NVIC_Config();
MYDMA_Config((u32)AD1_Value); //初始化DMA
while(1)
{
........
}
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>