微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 使用STM32F4的ADC采集4路电压信号,通过DMA传输,可是串口助手一直不能接受数据

使用STM32F4的ADC采集4路电压信号,通过DMA传输,可是串口助手一直不能接受数据

时间:10-02 整理:3721RD 点击:

  1. 主程序
  2. int main(void)
  3. {
  4.   delay_init(168);
  5.         uart_init(115200);
  6.   Adc_Init();
  7.         LED_Init();
  8.         delay_ms(10);
  9.         LED0=0;
  10.         MYDMA_Config(DMA2_Stream0,(u32)&ADC1->DR,(u32)&ADC_V);
  11.         while(1)
  12.         {
  13.           if(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0)!=RESET)
  14.     {
  15.       printf("adc0=%d  ",ADC_V[0]);
  16.       printf("adc1=%d  ",ADC_V[1]);
  17.                         printf("adc0=%d  ",ADC_V[2]);
  18.       printf("adc1=%d  ",ADC_V[3]);
  19.      DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
  20.     }
  21.                 delay_ms(1);
  22.         }
  23. }

  24. ADC的程序
  25. void  Adc_Init(void)
  26. {   
  27.   GPIO_InitTypeDef  GPIO_InitStructure;
  28.         ADC_CommonInitTypeDef ADC_CommonInitStructure;
  29.         ADC_InitTypeDef       ADC_InitStructure;
  30.        
  31.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1?üGPIOAê±?ó
  32.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ê1?üADC1ê±?ó


  33.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_5|GPIO_Pin_6;
  34.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//?£?aê?è?
  35.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//2?′?é???à-
  36.   GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉ  

  37.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);          //ADC1?′??
  38.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);        //?′???áê?         

  39.        
  40.   ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//?àá¢?£ê?
  41.   ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//á???2é?ù?×??????μ??ó3ù5??ê±?ó
  42.   ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
  43.   ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//?¤·??μ4·??μ?£ADCCLK=PCLK2/4=84/4=21Mhz,ADCê±?ó×?o?2?òa3?1y36Mhz
  44.   ADC_CommonInit(&ADC_CommonInitStructure);//3?ê??ˉ
  45.        
  46.   ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12???£ê?
  47.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  48.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  49.   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//???1′¥·¢?ì2a£?ê1ó?èí?t′¥·¢
  50.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò????       
  51.   ADC_InitStructure.ADC_NbrOfConversion = 4;
  52.   ADC_Init(ADC1, &ADC_InitStructure);//ADC3?ê??ˉ
  53.        
  54.         ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles );        //ADC1,ADCí¨μà,480???ü?ú,ìá??2é?ùê±???éò?ìá????è·?è                       
  55.   ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles );
  56.   ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_480Cycles );
  57.   ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 4, ADC_SampleTime_480Cycles );       
  58.        
  59.        
  60.         ADC_Cmd(ADC1, ENABLE);       
  61.         ADC_SoftwareStartConv(ADC1);
  62.         ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
  63.         ADC_DMACmd(ADC1,ENABLE);
  64.   

  65. }

  66. DMA程序
  67. void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 par,u32 mar)
  68. {

  69.         DMA_InitTypeDef  DMA_InitStructure;
  70.         NVIC_InitTypeDef NVIC_InitStructure;
  71.        
  72.         if((u32)DMA_Streamx>(u32)DMA2)//μ?μ?μ±?°streamê?ê?óúDMA2?1ê?DMA1
  73.         {
  74.           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2ê±?óê1?ü
  75.                
  76.         }else
  77.         {
  78.           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1ê±?óê1?ü
  79.         }
  80.   DMA_DeInit(DMA_Streamx);
  81.        
  82.         while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//μè′yDMA?é????
  83.        
  84.   /* ???? DMA Stream */
  85.   DMA_InitStructure.DMA_Channel = DMA_Channel_0;  //í¨μà????
  86.   DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMAíaéèμ??·
  87.   DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA ′?′¢?÷0μ??·
  88.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//íaéèμ?′?′¢?÷
  89.   DMA_InitStructure.DMA_BufferSize = 4;//êy?Y′?ê?á?
  90.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//íaéè·???á??£ê?
  91.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//′?′¢?÷??á??£ê?
  92.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//íaéèêy?Y3¤?è:16??
  93.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//′?′¢?÷êy?Y3¤?è:16??
  94.   DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//?-?·?£ê?
  95.   DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//?Dμèó??è??
  96.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  97.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  98.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//′?′¢?÷í?·¢μ¥′?′?ê?
  99.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//íaéèí?·¢μ¥′?′?ê?
  100.   DMA_Init(DMA_Streamx, &DMA_InitStructure);//3?ê??ˉDMA Stream

  101.         DMA_Cmd(DMA_Streamx, ENABLE);                      //?a??DMA′?ê?
  102.                

  103. }         

复制代码



已退回3积分

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

网站地图

Top