我根据网上的例程改写的程序,但是串口助手显示的电压都是0v,求大神指导 /* Includes------------------------------------------------------------------*/ #include "stm32f10x.h" #include "systick.h" #include <stdio.h> #include "usart.h" /* Private define------------------------------------------------------------*/ #define ADC1_DR_Address ((u32)0x4001244C) /* Private function prototypes-----------------------------------------------*/ void USART_Configuration(void); void ADC_Configuration(void); void filter(void); /* Private variables---------------------------------------------------------*/ #define N 50 //每通道采50次 #define M 3 //为12个通道 vu16 AD_Value[N][M]; //用来存放ADC转换结果,也是DMA的目标地址 vu16 After_filter[M]; //用来存放求平均值之后的结果 int i; u16 GetVolt(u16 advalue) { return(u16)(advalue * 330 / 4096); //求的结果扩大了100倍,方便下面求出小数 } /******************************************************************************* * Function Name : main * Description :Main program * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ int main(void) { u16value[M]; Delay_Init(); usart_Configuration(); ADC_Configuration(); ADC_SoftwareStartConvCmd(ADC1,ENABLE); DMA_Cmd(DMA1_Channel1,ENABLE); //启动DMA通道 printf("\r\n****************************************************************\r\n"); /*Infinite loop */ while (1) { while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待传输完成否则第一位数据容易丢失 filter(); for(i=0;i<3;i++) { value=GetVolt(After_filter); printf("value[%d]:%fv\r ",i,value) ; Delay(1000); } } } /******************************************************************************* * Function Name : ADC_Configuration * Description : Configure the ADC. * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void ADC_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO, ENABLE); /*Configure PA.06 (ADC Channel6), PA.07 (ADC Channel7) as analog input-------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); /*DMA channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Value; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //内存作为数据传输的目的地 DMA_InitStructure.DMA_BufferSize = N*M; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /*Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /*ADC1 configuration ------------------------------------------------------*/ ADC_DeInit(ADC1); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 3; ADC_Init(ADC1, &ADC_InitStructure); /*ADC1 regular channel6 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1,ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2,ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 3,ADC_SampleTime_239Cycles5); /*Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /*Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /*Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /*Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /*Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /*Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); } void filter(void) { intsum = 0; u8count; for(i=0;i<12;i++) { for( count=0;count<N;count++) sum+= AD_Value[count]; } After_filter=sum/N; sum=0; } void Rcc_Configuration(void) { ErrorStatusHSEStartUpStatus; RCC_DeInit();//RCC 系统复位 RCC_HSEConfig(RCC_HSE_ON);//开启HSE HSEStartUpStatus= RCC_WaitForHSEStartUp(); //等待HSE准备好 if(HSEStartUpStatus== SUCCESS) { FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//Enable Prefetch Buffer FLASH_SetLatency(FLASH_Latency_2);//Set 2 Latency cycles RCC_HCLKConfig(RCC_SYSCLK_Div1);//AHB clock = SYSCLK RCC_PCLK2Config(RCC_HCLK_Div1);//APB2 clock = HCLK RCC_PCLK1Config(RCC_HCLK_Div2);//APB1 clock = HCLK/2 RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_6); //PLLCLK = 12MHz * 6 = 72 MHz RCC_PLLCmd(ENABLE);//Enable PLL while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)== RESET); //Wait till PLL is ready RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//Select PLL as system clock source while(RCC_GetSYSCLKSource()!= 0x08); //Wait till PLL is used as system clock source RCC_ADCCLKConfig(RCC_PCLK2_Div6); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); //使能DMA传输 } } void UsartGPIO_Configuration(void) { GPIO_InitTypeDefGPIO_InitStruct; GPIO_InitStruct.GPIO_Mode= GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Pin= GPIO_Pin_9; GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_InitStruct.GPIO_Mode= GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Pin= GPIO_Pin_10; GPIO_Init(GPIOA,&GPIO_InitStruct); } void usart_Configuration(void) { USART_InitTypeDefUSART_InitStruct; Rcc_Configuration(); USART_InitStruct.USART_BaudRate= 115200; USART_InitStruct.USART_StopBits= USART_StopBits_1; USART_InitStruct.USART_WordLength= USART_WordLength_8b; USART_InitStruct.USART_Parity= USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl= USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode= USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1,&USART_InitStruct); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_Cmd(USART1,ENABLE); UsartGPIO_Configuration(); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the sourcefile and the source line number * where the assert_param errorhas occurred. *@param file: pointer to the source filename *@param line: assert_param error linesource number *@retval None */ void assert_failed(uint8_t* file, uint32_tline) { /*User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n",file, line) */ /*Infinite loop */ while (1) { } } #endif /********************************************************************************************************* END FILE *********************************************************************************************************/ |