用的f4跑ucos,用内部AD+DMA做两路AD转换,为啥DMA不工作,,给跪了,修改了好几天了都没有成效,望哥哥们给看看,不胜感激
[mw_shl_code=c,true]#include <stdio.h>
#include "LCD_RA8875.h"
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_gpio.h"
#include "AD.h"
#include "Key.h"
#include "adc_1.h"
#include "ucos_ii.h"
#include "GRAPH.h"
#define ADC_CONV_COUNT 5 //×???????
#define ADC_CH_NUM 2
uint16_t adc_value_final[ADC_CH_NUM];
uint16_t adc_value[ADC_CH_NUM * ADC_CONV_COUNT];
void ADC_DMA_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADCx, DMA and GPIO clocks ****************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
ADC_DeInit();
DMA_DeInit(DMA2_Stream3);//????DMA2?????÷0
/* DMA2 Stream0 channel1 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)(0x4001214C);
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&adc_value;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize =(ADC_CH_NUM * ADC_CONV_COUNT);
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_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream3, &DMA_InitStructure);
//DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_ITConfig(DMA2_Stream3,DMA_IT_TC , ENABLE);
DMA_Cmd(DMA2_Stream3, ENABLE);
/* Configure ADC3 Channel7 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;//ADC_DMAAccessMode_1;ADC_DMAAccessMode_Disabled
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC3 Init ****************************************************************/
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_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = ADC_CH_NUM;//???¨?????ò???????ò×?????ADC?¨????????
ADC_Init(ADC2, &ADC_InitStructure);
//ADC_TempSensorVrefintCmd(ENABLE);//????????????
/* ADC3 regular channel7 configuration *************************************/
ADC_RegularChannelConfig(ADC2, ADC_Channel_8, 1, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 2, ADC_SampleTime_3Cycles);
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);
DMA_ITConfig(DMA2_Stream3,DMA_IT_TC | DMA_IT_HT , ENABLE);
ADC_DMACmd(ADC2, ENABLE); /* Enable ADC3 DMA */
ADC_Cmd(ADC2, ENABLE); /* Enable ADC3 */
ADC_SoftwareStartConv(ADC2);/* Start ADC Software Conversion */
}
/*
????????AD????????
*/
void ADC_Sampling(void)
{
u8 i,j;
uint16_t sum;
GUI_DispDecAt( DMA_GetFlagStatus(DMA2_Stream3,DMA_FLAG_TCIF3) ,730,220,5);
ADC_SoftwareStartConv(ADC2);/* Start ADC Software Conversion */
//if(DMA_GetFlagStatus(DMA2_Stream3,DMA_FLAG_TCIF2) == SET)
//{
for(i=0; i<ADC_CH_NUM; i++)
{
sum=0;
for(j=0; j<ADC_CONV_COUNT; j++)
sum+=adc_value[j*ADC_CONV_COUNT+i];
adc_value_final=sum/ADC_CONV_COUNT;
}
DMA_ClearFlag(DMA2_Stream3,DMA_FLAG_TCIF3);
//}
}
/****
???????¨?¨??????
*/
int ADC_GetChannelValue(u8 ch)
{
if(ch<=0 || ch >ADC_CH_NUM)
{
return -1;
}
else
{
return adc_value_final[ch-1];
}
}
void NVIC_Config_DMA2(void)
{
NVIC_InitTypeDef NVIC_InitStructure;//???????í?????????????????á?????ü???????????á???????????? TIM_GenerateEvent
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);/*?è??NVIC??????·?×é??·?????
// ×???1?????????¨??????????????×???3?????????¨?ì????????
// ??????????·?×é???????????????????????ì?????????????????????????÷????????·????÷??????????×??ù????????
// ???????ò?????????è?¨???? */
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream3_IRQn; //?????????à?????????¨??????TIM2 ??????????????????IRQn_Type
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //?è????????????
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //?è???ì????????
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //???????¨????????
NVIC_Init(&NVIC_InitStructure);//?÷??????????????????????????
}
void DMA2_Stream3_IRQHandler(void)
{
GUI_DispDecAt(DMA_GetITStatus(DMA2_Stream3 , DMA_IT_TCIF3) ,750,190,5);
if(DMA_GetITStatus(DMA2_Stream3 , DMA_IT_TCIF3) == SET)
{
DMA_ClearITPendingBit(DMA2_Stream3 , DMA_IT_TCIF3);
ADC_Sampling();
}
}
[/mw_shl_code]
,,
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
注释显示不出来是因为字体是中文的原因,把代码复制到keil中就可以看到注释了,程序运行和代码没关系啊,,
一周热门 更多>