源码:
u32 adc_data;
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ê1ÄüADC1ê±Öó
//Ïè3õê¼»ˉADC1í¨μà5 IO¿ú
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PA5 í¨μà5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//Ä£Äaêäèë
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//2»′øéÏÏÂà-
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉ
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;//ὸö2éÑù½×¶ÎÖ®¼äμÄÑó3ù5¸öê±Öó
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; //DMAê§Äü
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//Ô¤·ÖÆμ4·ÖÆμ¡£ADCCLK=PCLK2/4=84/4=21Mhz,ADCê±Öó×îoÃ2»òa3¬1y36Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//3õê¼»ˉ
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12λÄ£ê½
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//·Çé¨ÃèÄ£ê½
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//¿aÆôá¬Dø×a»»
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//½ûÖ1′¥·¢¼ì2a£¬ê1óÃèí¼t′¥·¢
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò¶ÔÆë
ADC_InitStructure.ADC_NbrOfConversion = 1;//1¸ö×a»»Ôú1æÔòDòáDÖD ò2¾íêÇÖ»×a»»1æÔòDòáD1
ADC_Init(ADC1, &ADC_InitStructure);//ADC3õê¼»ˉ
ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);//ADCÖD¶Ï¿aÆô
ADC_Cmd(ADC1, ENABLE);//¿aÆôAD×a»»Æ÷
ADC_DMACmd(ADC1,ENABLE);//¿aÆôADCμÄDMA
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
ADC_RegularChannelConfig(ADC1, 4, 1, ADC_SampleTime_480Cycles);
}
void ADC_IRQHandler(void)
{
if(ADC_GetITStatus(ADC1,ADC_IT_EOC)==SET)
{
usart4.printf("the orginal data:%d
",ADC_GetConversionValue(ADC1));
MYDMA_Enable(DMA2_Stream0,32); /*??DMA*/
usart4.printf("the dma data:%d
",adc_data);
ADC_ClearITPendingBit(ADC1, ADC_IT_EOC); //????????
//usart4.printf("flag
");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
#include "..includedma.h"
#include "..includeusart.h"
#include "delay.h"
// streamxêy¾Yá÷ chxí¨μàÑ¡Ôñ paríaéèμØÖ· mar′æ′¢Æ÷μØÖ· ndtrêy¾Y′«êäá¿
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
if((u32)DMA_Streamx>(u32)DMA2)//????stream???DMA2??DMA1
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2????
}else
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1????
}
DMA_DeInit(DMA_Streamx);
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//??DMA???
/* ?? DMA Stream */
DMA_InitStructure.DMA_Channel = chx; //????
DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMA????
DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA ???0??
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//????????
DMA_InitStructure.DMA_BufferSize = ndtr;//?????
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//???????
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//???????
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//??????:8?
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//???????:8?
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// ??????
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//?????
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//?????????
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//????????
DMA_Init(DMA_Streamx, &DMA_InitStructure);//???DMA Stream
DMA_ITConfig(DMA2_Stream0,DMA_IT_TC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure);
}
//????DMA??
//DMA_Streamx
MA???,DMA1_Stream0~7/DMA2_Stream0~7
//ndtr:?????
void MYDMA_Enable(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
{
DMA_Cmd(DMA_Streamx, DISABLE); //??DMA??
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){} //??DMA?????
DMA_SetCurrDataCounter(DMA_Streamx,ndtr); //?????
DMA_Cmd(DMA_Streamx, ENABLE);
//??DMA??
}
void DMA2_Stream0_IRQHandler(void)
{
usart4.printf("flag
");
if(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0)!=RESET)//μè′yDMA2_Steam7′«êäíê3é
{
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);//Çå3yDMA2_Steam7′«êäíê3é±êÖ¾
}
}
为什么只传输一次就不传输了?
我最近也是遇到这个问题,但和你做的不一样,你解决了吗?
一周热门 更多>