使用STM32F4的ADC采集4路电压信号,通过DMA传输,可是串口助手一直不能接受数据
时间:10-02
整理:3721RD
点击:
- 主程序
- int main(void)
- {
- delay_init(168);
- uart_init(115200);
- Adc_Init();
- LED_Init();
- delay_ms(10);
- LED0=0;
- MYDMA_Config(DMA2_Stream0,(u32)&ADC1->DR,(u32)&ADC_V);
- while(1)
- {
- if(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0)!=RESET)
- {
- printf("adc0=%d ",ADC_V[0]);
- printf("adc1=%d ",ADC_V[1]);
- printf("adc0=%d ",ADC_V[2]);
- printf("adc1=%d ",ADC_V[3]);
- DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
- }
- delay_ms(1);
- }
- }
- ADC的程序
- void Adc_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- ADC_InitTypeDef ADC_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1?üGPIOAê±?ó
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ê1?üADC1ê±?ó
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_5|GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//?£?aê?è?
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//2?′?é???à-
- GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉ
-
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC1?′??
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //?′???áê?
-
-
- ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//?àá¢?£ê?
- ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//á???2é?ù?×??????μ??ó3ù5??ê±?ó
- ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
- ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//?¤·??μ4·??μ?£ADCCLK=PCLK2/4=84/4=21Mhz,ADCê±?ó×?o?2?òa3?1y36Mhz
- ADC_CommonInit(&ADC_CommonInitStructure);//3?ê??ˉ
-
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12???£ê?
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//???1′¥·¢?ì2a£?ê1ó?èí?t′¥·¢
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò????
- ADC_InitStructure.ADC_NbrOfConversion = 4;
- ADC_Init(ADC1, &ADC_InitStructure);//ADC3?ê??ˉ
-
- ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles ); //ADC1,ADCí¨μà,480???ü?ú,ìá??2é?ùê±???éò?ìá????è·?è
- ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles );
- ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_480Cycles );
- ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 4, ADC_SampleTime_480Cycles );
-
-
- ADC_Cmd(ADC1, ENABLE);
- ADC_SoftwareStartConv(ADC1);
- ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
- ADC_DMACmd(ADC1,ENABLE);
-
- }
- DMA程序
- void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 par,u32 mar)
- {
-
- DMA_InitTypeDef DMA_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- if((u32)DMA_Streamx>(u32)DMA2)//μ?μ?μ±?°streamê?ê?óúDMA2?1ê?DMA1
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2ê±?óê1?ü
-
- }else
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1ê±?óê1?ü
- }
- DMA_DeInit(DMA_Streamx);
-
- while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//μè′yDMA?é????
-
- /* ???? DMA Stream */
- DMA_InitStructure.DMA_Channel = DMA_Channel_0; //í¨μà????
- DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMAíaéèμ??·
- DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA ′?′¢?÷0μ??·
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//íaéèμ?′?′¢?÷
- DMA_InitStructure.DMA_BufferSize = 4;//êy?Y′?ê?á?
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//íaéè·???á??£ê?
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//′?′¢?÷??á??£ê?
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//íaéèêy?Y3¤?è:16??
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//′?′¢?÷êy?Y3¤?è:16??
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//?-?·?£ê?
- DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//?Dμèó??è??
- 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;//íaéèí?·¢μ¥′?′?ê?
- DMA_Init(DMA_Streamx, &DMA_InitStructure);//3?ê??ˉDMA Stream
- DMA_Cmd(DMA_Streamx, ENABLE); //?a??DMA′?ê?
-
- }
已退回3积分