求教(急)DMA AD采样 电压串口打印出来,采集的电压始终显示0v

2019-07-20 22:50发布

搞不懂  
    ad=0;
         for(i=0;i<50;i++)
         {
                        ad=ad+ADCData[i];
         }           
          ad=ad/50;
这段话出来始终为0
哪位大哥给点建议,是哪里的问题,我采集的是3.3v电源电压


#include "stm32f10x.h"
#include "define.h"
#include "delay.h"
#include "stdio.h"


u16 ADCData[50];
void RCC_Configuration(void)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
        RCC_ADCCLKConfig(RCC_PCLK2_Div6);//12M  ×&#238;′ó14M
       
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
}

//GPIO3&#245;ê&#188;&#187;ˉ
void GPIO_Configuration(void)
{
   GPIO_InitTypeDef GPIO_InitStructure;       

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA,&GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA,&GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;//ADμ&#196;C41ü&#189;&#197;       
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//&#196;£&#196;aá&#191;ê&#228;è&#235;
        GPIO_Init(GPIOC,&GPIO_InitStructure);
        }

//&#214;D&#182;&#207;ó&#197;&#207;è&#188;&#182;éè&#214;&#195;
void NVIC_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//&#214;D&#182;&#207;·&#214;×é2
       
        NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; ;//′&#174;&#191;ú1í¨μà
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//&#199;à&#213;&#188;0
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//&#207;ìó|1
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQí¨μà±&#187;ê1&#196;ü
        NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
  USART_InitTypeDef  USART_InitStructure;
        USART_InitStructure.USART_BaudRate=9600;
        USART_InitStructure.USART_WordLength=USART_WordLength_8b;
        USART_InitStructure.USART_StopBits=USART_StopBits_1;
        USART_InitStructure.USART_Parity=USART_Parity_No;
        USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;

        USART_Init(USART1,&USART_InitStructure);
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
        USART_Cmd(USART1,ENABLE);
        USART_ClearFlag(USART1,USART_FLAG_TC);
}

void ADC_Configuration(void)
{
        ADC_InitTypeDef ADC_InitStructure;

        ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//&#182;àá¢&#196;£ê&#189;
        ADC_InitStructure.ADC_ScanConvMode=DISABLE;//μ¥í¨μà&#196;£ê&#189;
        ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;//&#209;-&#187;·2é&#188;ˉ
        ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//èí&#188;t′¥·¢
        ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//óò&#182;&#212;&#198;&#235;
        ADC_InitStructure.ADC_NbrOfChannel=1;//×a&#187;&#187;í¨μàêy&#196;&#191;&#206;a1

        ADC_Init(ADC1,&ADC_InitStructure);

        ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_239Cycles5);//1&#230;&#212;ò×é,ADC1,í¨μà10£&#172;ò&#187;&#184;&#246;í¨μà£&#172;239.5&#184;&#246;&#214;ü&#198;ú

        ADC_DMACmd(ADC1,ENABLE);//&#214;&#216;òa °&#209;ADoíDMA°ó&#182;¨ò&#187;&#198;e
       
        ADC_Cmd(ADC1,ENABLE);//ê1&#196;üADC1

        ADC_ResetCalibration(ADC1);//&#184;′&#206;&#187;
        while(ADC_GetResetCalibrationStatus(ADC1)); //&#161;¢μè′yê&#199;·&#241;&#184;′&#206;&#187;íê3é

        ADC_StartCalibration(ADC1);//D£×&#188;ADC1
        while(ADC_GetCalibrationStatus(ADC1));//&#188;ì&#209;éê&#199;·&#241;D£×&#188;íê3é

        ADC_SoftwareStartConvCmd(ADC1,ENABLE);//&#198;&#244;&#182;ˉADC1

}
void DMA_Configuration(void)
{
         DMA_InitTypeDef DMA_InitStructure;
         
         DMA_DeInit(DMA1_Channel1);//ADC11ò&#212;úDMA1μ&#196;í¨μàé&#207;
         
         DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&ADC1->DR;//íaéèμ&#216;&#214;·ADC1&#203;í&#184;&#248;DMA
         DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADCData;//&#196;ú′&#230;μ&#216;&#214;· &#203;í&#184;&#248;êy×é êy×é&#195;&#251;μèóúêy×éμ&#196;μ&#216;&#214;·
       
          DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;//íaéèADC1×÷&#206;aêy&#190;Yμ&#196;à′&#212;′
         DMA_InitStructure.DMA_BufferSize=50;//&#187;o3&#229;&#199;&#248;&#206;aêy×éμ&#196;′óD&#161;
         
         DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//íaéèADC1μ&#196;′&#230;′¢μ&#216;&#214;·&#206;a2&#187;±&#228;μ&#196;
         DMA_InitStructure.DMA_MemoryInc=DMA_PeripheralInc_Enable;//&#196;ú′&#230;μ&#196;μ&#216;&#214;·ê&#199;μY&#212;&#246;μ&#196;
       
   DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;//íaéèêy&#190;Y&#191;í&#182;è16
         DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;//&#196;ú′&#230;êy&#190;Y&#191;í&#182;è16
         DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;//á&#172;D&#248;1¤×÷&#196;£ê&#189;£&#172;&#209;-&#187;·2é&#188;ˉ
         DMA_InitStructure.DMA_Priority=DMA_Priority_High;//ó&#197;&#207;è&#188;&#182;
         DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//2&#187;ê&#199;&#196;ú′&#230;μ&#189;&#196;ú′&#230; íaéèμ&#189;&#196;ú′&#230;
         
         DMA_Init(DMA1_Channel1,&DMA_InitStructure);
       
         DMA_Cmd(DMA1_Channel1,ENABLE);//ê1&#196;üDMA
}
int main(void)
{
   u32 ad=0;
   u8 i=0;
         delay_init();
   RCC_Configuration();        //&#207;μí3ê±&#214;ó3&#245;ê&#188;&#187;ˉ
   GPIO_Configuration();//&#182;&#203;&#191;ú3&#245;ê&#188;&#187;ˉ
   USART_Configuration();
   NVIC_Configuration();
  DMA_Configuration();
        ADC_Configuration();

   while(1)
   {
     ad=0;
         for(i=0;i<50;i++)
         {
                        ad=ad+ADCData[i];
         }           
          ad=ad/50;       
          printf("ad =%f ",3.3/4095*ad);//êμ&#188;êμ&#231;&#209;1&#214;μ         
          delay_ms(1000);
          delay_ms(1000);
                
   }       
}


0条回答

一周热门 更多>