以参考过网上很多程序,都没成功,不说废话,以下是详情,盼教!
设备:探索者STM32F407ZG
目标:用ADC连续模式+DMA循环模式 来实现ADC一直采样并且不断刷新存储区内存储的采样值,以使得我可以实时从存储区获得采样值
代码:
DMA配置(两个函数):
[mw_shl_code=c,true]void MYDMA_p2m_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar,u16 ndtr)
{
DMA_InitTypeDef DMA_InitStructure;
if((u32)DMA_Streamx>(u32)DMA2)//
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//
}else
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//
}
DMA_DeInit(DMA_Streamx);
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//
/* ÅäÖÃ DMA Stream */
DMA_InitStructure.DMA_Channel = chx;
DMA_InitStructure.DMA_PeripheralBaseAddr = par;
DMA_InitStructure.DMA_Memory0BaseAddr = mar;
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_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式
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);
}
void MYDMA_p2m_Enable(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
{
DMA_Cmd(DMA_Streamx, DISABLE);
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
DMA_SetCurrDataCounter(DMA_Streamx,ndtr);
DMA_Cmd(DMA_Streamx, ENABLE); //使能DMA传输
} [/mw_shl_code]
ADC配置:
[mw_shl_code=applescript,true]void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//Ïè3õê¼»ˉADC1í¨μà5 IO¿ú
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_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); //
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; //开启DMA模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//
ADC_CommonInit(&ADC_CommonInitStructure);//
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
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;//
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);//开启ADC的DMA功能
ADC_SoftwareStartConv(ADC1);//软件触发开始转换
} [/mw_shl_code]
测试用主函数:
[mw_shl_code=applescript,true] NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);/
delay_init(168); /
uart_init(115200); /
/*--------------------------------------------------------------------------------------------*/
Adc_Init();
MYDMA_p2m_Config(DMA2_Stream0,DMA_Channel_0,(u32)&ADC1->DR,(u32)SendBuff,1);//一个字节循环传输
MYDMA_p2m_Enable(DMA2_Stream0, 1);
while(1)
{//每隔一段时间打印一次储存区的值,SendBuff是个数组,用它第0个位置当存储区
delay_ms(1000);
printf("the SendBuff[0]: %d
",SendBuff[0]);
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
确定了啊:
MYDMA_p2m_Config(DMA2_Stream0,DMA_Channel_0,(u32)&ADC1->DR,(u32)SendBuff,1);//一个字节循环传输
一周热门 更多>