微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 关于使用STM32F4定时器触发内部AD采集的问题

关于使用STM32F4定时器触发内部AD采集的问题

时间:10-02 整理:3721RD 点击:
我想通过定时器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);        //?ú???ˉ?¨ê±?÷oˉêyà???ê1?üTIM2
//        TIM_InternalClockConfig(TIM2);//éè??TIM2?ú2?ê±?ó
//        TIM_UpdateDisableConfig(TIM2,ENABLE);//ê§?üTIM2?üD?ê??t
//        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;        /*????òa????μ?GPIOAòy??*/               
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; /*éè??òy???£ê??a?£?aê?è?*/  
  GPIO_Init(GPIOA, &GPIO_InitStructure);/*μ÷ó??aoˉêy£?3?ê??ˉ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);//3?ê??ˉ?÷??′??÷????
        DMA_StructInit( &DMA_InitStructure);
        /* DMA2 Stream0 channel0 configuration **************************************/
        //??è?DMAí¨μà0.êy?Yá÷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;
  //·??ò ′ó?ú′?μ?íaéè.íaéèê?êy?Y?′
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;   
        //êy?Y?o3???
        DMA_InitStructure.DMA_BufferSize = 1000;  
        //íaéèμ??·2???
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
        //?ú′?μ??·???ó
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;   //DMA_MemoryInc_Enable DMA_MemoryInc_Disable
        //êy?YààDí?a°?×?
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //íaéèêy?Yμ¥??  
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //?ú′?êy?Yμ¥??£o8b
       
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA?£ê?£o?-?·′?ê?
  DMA_InitStructure.DMA_Priority = DMA_Priority_High; //ó??è??£o?D
        //2?ê1ó?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£?êy?Yá÷0
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
        //ê1?üDMA
        DMA_Cmd(DMA2_Stream0, ENABLE);
       
       
       
        //????DMA2í¨μàêy?Yá÷0
        NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream0_IRQn;//DMA2_Stream0_IRQHandler;
        //?à??ó??è??0
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
        //?ìó|ó??è??
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=4;
        //í¨μàê1?ü
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        //??ò?é?????ó|ó?óúNVIC
        NVIC_Init(&NVIC_InitStructure);
        //ê1?üDMA′?ê?íê3é?D??
        DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
       
        /*        -----------------------------------------------------------------------
        ADC×a??ê±??:T = T1+T2 = 0.7us + 0.24us = 0.94us?£′ó??ê?1us,×a???ù?êμèóú£o1M
        -----------------------------------------------------------------------*/       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ADC1ê1ó?APB2ê±?ó£oHCLK2 = 84M?£
        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;    //éè??12??·?±??ê
        ADC_InitStructure.ADC_ScanConvMode = DISABLE;   //μ¥í¨μà£o1?±?é¨?è?£ê?
        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;      //á?D??£ê?ENABLE,1?±??-?·?£ê?£?2éó??¨ê±?÷′¥·¢
        ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;   //é?éy??′¥·¢
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;//TIMò?3?′¥·¢?£ADC_ExternalTrigConv_T2_CC2;ADC_ExternalTrigConv_T2_TRGO
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;         //êy?Yóò?????£ê?
        ADC_InitStructure.ADC_NbrOfConversion = 1;  //éè??ADC×a??í¨μàêy
        ADC_Init(ADC1,&ADC_InitStructure);
        //ê1?üADC1μ??úDMA?£ê???á?D?×a??
        ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
        ADC_DMACmd(ADC1, ENABLE);
        /* ADC3 regular channel2 configuration £o*/  
        ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_3Cycles); //????1??ò×é£?μ?3?μ¥′?×¥??ê±??: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ê?2á153ò3£oVDDA?ú2.4μ?3.6V????ê±£?×¥???μ?ê×?′ó36M£??è?¨ê?30M?£
        ---------------------------------------------------------------------*/
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;  //×¥???μ?ê = HCLK2 / ADC_Prescaler_Div4 = 21M?£
        //ê1ó?ADCμ?DMA?′ó?
        ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;//ADC_DMAAccessMode_2 ADC_DMAAccessMode_Disabled
  //á?′?×¥??????ê±???a5??ADCê±?ó?ü?ú:T2 = 5/21 = 0.24us      
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
        //??ò?é?????ó|ó?óúADC_CommonInitStructure
        ADC_CommonInit(&ADC_CommonInitStructure);
}
采集不出来数据,还请有高手指点。扣扣:1849962273

现在感觉已经可以读值了,但是感觉不对,还希望有高手能够指点

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top