关于使用STM32F4定时器触发内部AD采集的问题
2019-07-14 22:23发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
我想通过定时器2,控制内部AD进行64点采样,感觉AD,定时器,DMA都已经配置好了,但是不知道什么原因总是读不出数据。(如果不适用定时器触发,可以通过串口读出AD采样结果),附上代码还请高手帮忙解答,谢谢。
void timer2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM2_TimeBaseStructure;
TIM_OCInitTypeDef TIM2_OCInitStructure;
TIM_DeInit(TIM2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_TimeBaseStructInit(&TIM2_TimeBaseStructure)
TIM2_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1
TIM2_TimeBaseStructure.TIM_Prescaler = 42-1;
TIM2_TimeBaseStructure.TIM_Period = 625-1;
TIM2_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM2_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update)
////////////////////////////////////////////////////////////////////
TIM2_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM2_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable
TIM2_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low
TIM2_OCInitStructure.TIM_Pulse = CCR
TIM_OC2Init(TIM2, & TIM2_OCInitStructure);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable)
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_Cmd(TIM2,ENABLE);
////////////////////////////////////////////////////////////////////////////////
// TIM_Cmd(TIM2,ENABLE); //ÔÚÆô¶¯¶¨Ê±Æ÷º¯ÊýÀïÃæʹÄÜTIM2
// TIM_InternalClockConfig(TIM2);//ÉèÖÃTIM2ÄÚ²¿Ê±ÖÓ
// TIM_UpdateDisableConfig(TIM2,ENABLE);//ʧÄÜTIM2¸üÐÂʼþ
// TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
}
void ADC_IO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /*Ñ¡ÔñÒª¿ØÖƵÄGPIOAÒý½Å*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; /*ÉèÖÃÒý½ÅģʽΪģÄâÊäÈë*/
GPIO_Init(GPIOA, &GPIO_InitStructure);/*µ÷Óÿ⺯Êý£¬³õʼ»¯GPIOA*/
}
void ADC1_Mode_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_DeInit(DMA2_Stream0);//³õʼ»¯¸÷¼Ä´æÆ÷ÅäÖÃ
DMA_StructInit( &DMA_InitStructure);
/* DMA2 Stream0 channel0 configuration **************************************/
//Ñ¡È¡DMAͨµÀ0.Êý¾ÝÁ÷0
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
//ADCµØÖ·
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
//ÄÚ´æµØÖ·
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_ConvertedValue;
//·½Ïò ´ÓÄÚ´æµ½ÍâÉè.ÍâÉèÊÇÊý¾ÝÔ´
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
//Êý¾Ý»º³åÇø
DMA_InitStructure.DMA_BufferSize = 1000;
//ÍâÉèµØÖ·²»Ôö
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//ÄÚ´æµØÖ·Ôö¼Ó
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //DMA_MemoryInc_Enable DMA_MemoryInc_Disable
//Êý¾ÝÀàÐÍΪ°ë×Ö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //ÍâÉèÊý¾Ýµ¥Î»
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //ÄÚ´æÊý¾Ýµ¥Î»£º8b
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMAģʽ£ºÑ»·´«Êä
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //ÓÅÏȼ¶£ºÖÐ
//²»Ê¹ÓÃFIFOģʽ
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;
//½«ÒÔÉÏÉèÖÃÓ¦ÓÃÓÚDMA2£¬Í¨µÀ0£¬Êý¾ÝÁ÷0
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
//ʹÄÜDMA
DMA_Cmd(DMA2_Stream0, ENABLE);
//Ñ¡ÔñDMA2ͨµÀÊý¾ÝÁ÷0
NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream0_IRQn;//DMA2_Stream0_IRQHandler;
//ÇÀÕ¼ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
//ÏìÓ¦ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=4;
//ͨµÀʹÄÜ
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
//½«ÒÔÉÏÅäÖÃÓ¦ÓÃÓÚNVIC
NVIC_Init(&NVIC_InitStructure);
//ʹÄÜDMA´«ÊäÍê³ÉÖжÏ
DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
/* -----------------------------------------------------------------------
ADCת»»Ê±¼ä:T = T1+T2 = 0.7us + 0.24us = 0.94us¡£´óÔ¼ÊÇ1us,ת»»ËÙÂʵÈÓÚ£º1M
-----------------------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ADC1ʹÓÃAPB2ʱÖÓ£ºHCLK2 = 84M¡£
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //ÉèÖÃ12λ·Ö±æÂÊ
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //µ¥Í¨µÀ£º¹Ø±ÕɨÃèģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Á¬ÐøģʽENABLE,¹Ø±ÕÑ»·Ä£Ê½£¬²ÉÓö¨Ê±Æ÷´¥·¢
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //ÉÏÉýÑØ´¥·¢
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;//TIMÒç³ö´¥·¢¡£ADC_ExternalTrigConv_T2_CC2;ADC_ExternalTrigConv_T2_TRGO
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //Êý¾ÝÓÒ¶ÔÆëģʽ
ADC_InitStructure.ADC_NbrOfConversion = 1; //ÉèÖÃADCת»»Í¨µÀÊý
ADC_Init(ADC1,&ADC_InitStructure);
//ʹÄÜADC1µÄÔÚDMAģʽÏÂÁ¬Ðøת»»
ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
ADC_DMACmd(ADC1, ENABLE);
/* ADC3 regular channel2 configuration £º*/
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_3Cycles); //ÅäÖùæÔò×飬µÃ³öµ¥´Î×¥»»Ê±¼ä:T1 = (3+12)/21 = 0.7us
/* Enable ADC1 DMA */
ADC_Cmd(ADC1,ENABLE);
ADC_ExternalTrigInjectedConvConfig(ADC1, TIM_TRGOSource_Update);//TIM_CC1
// ADTIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
// ADC_ExternalTrigConvCmd(ADC1, ENABLE);
}
void ADC1_Com_Configuration(void)
{
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//¶ÀÁ¢Ä£Ê½
/* ---------------------------------------------------------------------
STM32F439ÊÖ²á153Ò³£ºVDDAÔÚ2.4µ½3.6VÖ®¼äʱ£¬×¥»»ÆµÂÊ×î´ó36M£¬Îȶ¨ÊÇ30M¡£
---------------------------------------------------------------------*/
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //×¥»»ÆµÂÊ = HCLK2 / ADC_Prescaler_Div4 = 21M¡£
//ʹÓÃADCµÄDMA¸´ÓÃ
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;//ADC_DMAAccessMode_2 ADC_DMAAccessMode_Disabled
//Á½´Î×¥»»¼ä¸ôʱ¼äΪ5¸öADCʱÖÓÖÜÆÚ:T2 = 5/21 = 0.24us
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
//½«ÒÔÉÏÅäÖÃÓ¦ÓÃÓÚADC_CommonInitStructure
ADC_CommonInit(&ADC_CommonInitStructure);
}
采集不出来数据,还请有高手指点。扣扣:1849962273
一周热门 更多>