初中生
最后登录1970-1-1
在线时间 小时
注册时间2015-7-25
|
利用ADC做多路采集,出来其他两路是对的,有一路出来总是为0,不知道怎么回事,烦请大家帮忙解决一下问题- /**
- ******************************************************************************
- * @file main.c
- * @author fire
- * @version V1.0
- * @date 2013-xx-xx
- * @brief adc1 2é¼ˉêμÑé
- ******************************************************************************
- * @attention
- *
- * êμÑéƽì¨:ò°»e iSO STM32 ¿a·¢°å
- * ÂÛì3 :http://www.firebbs.cn
- * ìÔ±| :http://firestm32.taobao.com
- *
- ******************************************************************************
- */
- //time : pc1-1 0 1us
- // pc1-2 0 0.5us 1.5us
- // pc1-3 0 0.5us 0.8us
- #include "stm32f10x.h"
- #include "bsp_usart1.h"
- #include "bsp_adc.h"
- // ADC1×a»»μÄμçÑ1Öμí¨1yMDA·½ê½′«μ½SRAM
- extern __IO uint16_t ADC_ConvertedValue[3];
- // ¾Ö2¿±äá¿£¬óÃóú±£′æ×a»»¼ÆËãoóμÄμçÑ1Öμ
- float ADC_ConvertedValueLocal[3];
- // èí¼tÑóê±
- void Delay(__IO uint32_t nCount)
- {
- for(; nCount != 0; nCount--);
- }
- /**
- * @brief Ö÷oˉêy
- * @param ÎT
- * @retval ÎT
- */
- int main(void)
- {
- uint8_t i;
- /* USART1 config */
- USART1_Config();
-
- /* enable adc1 and config adc1 to dma mode */
- ADC1_Init();
-
- printf("\r\n ----ÕaêÇò»¸öADCêμÑé(DMA′«êä)----\r\n");
-
- while (1)
- {
- for(i=3;i>0;i--)
- {
- ADC_ConvertedValueLocal[i] =(float) ADC_ConvertedValue[i]/4096*3.3; // ¶áè¡×a»»μÄADÖμ
-
- printf("\r\n The current AD value = 0x%04X \r\n", ADC_ConvertedValue[i]);
- printf("\r\n The current AD value = %f V \r\n",ADC_ConvertedValueLocal[i]);
- Delay(0xffffee);
- }
- }
- }
- /*********************************************END OF FILE**********************/
- /**
- ******************************************************************************
- * @file bsp_usart1.c
- * @author fire
- * @version V1.0
- * @date 2013-xx-xx
- * @brief ÖØÏÖc¿aprintfoˉêyμ½usart¶Ë¿ú
- ******************************************************************************
- * @attention
- *
- * êμÑéƽì¨:ò°»e iSO STM32 ¿a·¢°å
- * ÂÛì3 :http://www.firebbs.cn
- * ìÔ±| :http://firestm32.taobao.com
- *
- ******************************************************************************
- */
-
- #include "bsp_usart1.h"
- /**
- * @brief USART1 GPIO ÅäÖÃ,1¤×÷Ä£ê½ÅäÖá£115200 8-N-1
- * @param ÎT
- * @retval ÎT
- */
- void USART1_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- /* config USART1 clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
-
- /* USART1 GPIO config */
- /* Configure USART1 Tx (PA.09) as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure USART1 Rx (PA.10) as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* USART1 mode config */
- USART_InitStructure.USART_BaudRate = 115200;
- 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_Cmd(USART1, ENABLE);
- }
- ///Öض¨Ïòc¿aoˉêyprintfμ½USART1
- int fputc(int ch, FILE *f)
- {
- /* ·¢Ëíò»¸ö×Ö½úêy¾Yμ½USART1 */
- USART_SendData(USART1, (uint8_t) ch);
-
- /* μè′y·¢Ëííê±Ï */
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
-
- return (ch);
- }
- ///Öض¨Ïòc¿aoˉêyscanfμ½USART1
- int fgetc(FILE *f)
- {
- /* μè′y′®¿ú1êäèëêy¾Y */
- while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(USART1);
- }
- /*********************************************END OF FILE**********************/
- /**
- ******************************************************************************
- * @file bsp_xxx.c
- * @author fire
- * @version V1.0
- * @date 2013-xx-xx
- * @brief adc1 ó|óÃbsp / DMA Ä£ê½
- ******************************************************************************
- * @attention
- *
- * êμÑéƽì¨:ò°»e iSO STM32 ¿a·¢°å
- * ÂÛì3 :http://www.firebbs.cn
- * ìÔ±| :http://firestm32.taobao.com
- *
- ******************************************************************************
- */
-
- #include "bsp_adc.h"
- #define ADC1_DR_Address ((u32)0x40012400+0x4c)
- __IO uint16_t ADC_ConvertedValue[3];
- //__IO u16 ADC_ConvertedValueLocal;
- /**
- * @brief ê1ÄüADC1oíDMA1μÄê±Öó£¬3õê¼»ˉPC.01
- * @param ÎT
- * @retval ÎT
- */
- static void ADC1_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable DMA clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
-
- /* Enable ADC1 and GPIOC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
-
- /* Configure PC.01 as analog input */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOC, &GPIO_InitStructure); // PC1,êäèëê±2»óÃéèÖÃËùÂê
- }
- /**
- * @brief ÅäÖÃADC1μÄ1¤×÷Ä£ê½ÎaMDAÄ£ê½
- * @param ÎT
- * @retval ÎT
- */
- static void ADC1_Mode_Config(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
-
- /* DMA channel1 configuration */
- DMA_DeInit(DMA1_Channel1);
-
- DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //ADCμØÖ·
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //Äú′æμØÖ·
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = 3;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //íaéèμØÖ·1춨
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //Äú′æμØÖ·1춨
- 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 DMA channel1 */
- DMA_Cmd(DMA1_Channel1, ENABLE);
-
- /* ADC1 configuration */
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //¶àá¢ADCÄ£ê½
- ADC_InitStructure.ADC_ScanConvMode = ENABLE ; //½ûÖ1é¨Ãèģ꽣¬é¨ÃèÄ£ê½óÃóú¶àí¨μà2é¼ˉ
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //¿aÆôá¬Dø×a»»Ä£ê½£¬¼′2»í£μؽøDDADC×a»»
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //2»ê1óÃía2¿′¥·¢×a»»
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //2é¼ˉêy¾Yóò¶ÔÆë
- ADC_InitStructure.ADC_NbrOfChannel = 3; //òa×a»»μÄí¨μàêyÄ¿1
- ADC_Init(ADC1, &ADC_InitStructure);
-
- /*ÅäÖÃADCê±Öó£¬ÎaPCLK2μÄ8·ÖÆ죬¼′9MHz*/
- RCC_ADCCLKConfig(RCC_PCLK2_DiARM);
- /*ÅäÖÃADC1μÄí¨μà11Îa55. 5¸ö2éÑùÖüÆú£¬DòáDÎa1 */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_55Cycles5);
- /* Enable ADC1 DMA */
- ADC_DMACmd(ADC1, ENABLE);
-
- /* Enable ADC1 */
- ADC_Cmd(ADC1, ENABLE);
-
- /*¸′λD£×¼¼Ä′æÆ÷ */
- ADC_ResetCalibration(ADC1);
- /*μè′yD£×¼¼Ä′æÆ÷¸′λíê3é */
- while(ADC_GetResetCalibrationStatus(ADC1));
-
- /* ADCD£×¼ */
- ADC_StartCalibration(ADC1);
- /* μè′yD£×¼íê3é*/
- while(ADC_GetCalibrationStatus(ADC1));
-
- /* óéóúûóD2éóÃía2¿′¥·¢£¬ËùòÔê1óÃèí¼t′¥·¢ADC×a»» */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- /**
- * @brief ADC13õê¼»ˉ
- * @param ÎT
- * @retval ÎT
- */
- void ADC1_Init(void)
- {
- ADC1_GPIO_Config();
- ADC1_Mode_Config();
- }
- /*********************************************END OF FILE**********************/
复制代码
|
|