- #include "bsp_adc.h"
- #include "stm32f10x_dma.h"
- #include "stm32f10x_adc.h"
- #define ADC1_DR_Address ((u32)0x40012400+0x4c)
- __IO uint16_t ADC_ConvertedValue[2];
- float ADC_ConvertedValueLocal[2];
- /**
- * @brief ê1ÄüADC1oíDMA1μÄê±Öó£¬3õê¼»ˉPC.01 PC.02
- * @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 PC.02 as analog input */
- GPIO_InitStructure.GPIO_Pin = 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′«êä·½Ïò
- DMA_InitStructure.DMA_BufferSize = 2; //DMA»o3åÇøμÄ3¤¶è word
- 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 = 2; //òa×a»»μÄí¨μàêyÄ¿2
- ADC_Init(ADC1, &ADC_InitStructure);
-
- /*ÅäÖÃADCê±Öó£¬ÎaPCLK2μÄ8·ÖÆ죬¼′9MHz*/
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- /*ÅäÖÃADC1μÄí¨μà11Îa55. 5¸ö2éÑùÖüÆú£¬DòáDÎa1 */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5);
- /*ÅäÖÃADC1μÄí¨μà12Îa55. 5¸ö2éÑùÖüÆú£¬DòáDÎa2 */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 2, 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();
- }
- void filter (void)
- {
- int i = 0;
- for(i = 0;i < 2;i++)
- {
- ADC_ConvertedValueLocal[i] = ADC_ConvertedValue[i]*3.3/4096 ;
- }
- }
复制代码
此帖出自
小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>