void U_ADC::ADCInit(uint8_t ADC_Channel, uint8_t ADC_SampleTime) { ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4); ADC_ChannelConfig(ADC1, ADC_Channel, ADC_SampleTime); ADC_TempSensorCmd(ENABLE); ADC_GetCalibrationFactor(ADC1); ADC_Cmd(ADC1, ENABLE); while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)) ; }
/** * @brief ADC Configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC Channel11 as analog input */ #ifdef USE_STM320518_EVAL GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; #else GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; #endif /* USE_STM320518_EVAL */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADCs DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continuous mode with a resolution equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ #ifdef USE_STM320518_EVAL ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles); #else ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles); #endif /* USE_STM320518_EVAL */ /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable the ADC peripheral */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY flag */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
/** * @brief Initializes the ADC Light sensor ... * @param None * @retval None */ void Light_Init(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* LIGHT GPIO Periph clock enable */ RCC_AHBPeriphClockCmd(LIGHT_GPIO_CLK | LIGHT_PWR_GPIO_CLK, ENABLE); /* LIGHT ADC Periph clock enable */ RCC_APB2PeriphClockCmd(LIGHT_ADC_CLK, ENABLE); /* Configure LIGHT as analog input */ GPIO_InitStructure.GPIO_Pin = LIGHT_PIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(LIGHT_GPIO_PORT, &GPIO_InitStructure); /* Configure LIGHT_PWR_PIN pin: Light sensor shutdown pin */ GPIO_InitStructure.GPIO_Pin = LIGHT_PWR_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(LIGHT_PWR_GPIO_PORT, &GPIO_InitStructure); LIGHT_PWR_GPIO_PORT->BSRR = LIGHT_PWR_PIN; /* ADC DeInit */ ADC_DeInit(LIGHT_ADC); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the LIGHT ADC in a 12 bits resolutuion */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(LIGHT_ADC, &ADC_InitStructure); /* Convert the LIGHT Channel with 239.5 Cycles as sampling time */ ADC_ChannelConfig(LIGHT_ADC, LIGHT_ADC_CHANNEL, ADC_SampleTime_239_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(LIGHT_ADC); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(LIGHT_ADC, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(LIGHT_ADC, ADC_FLAG_ADEN)); }
/************************************************************************************************ ** Function name : ** Description : ** ** Input : ** Output : ** Return : ** Others : ** ************************************************************************************************/ void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //GPIOC Periph clock enable RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //ADC1 Periph clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Configure ADC Channel1 as analog input GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); //ADC1 Configuration //ADCs DeInit ADC_DeInit(ADC1); //Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); //转换ADC1通道,周期采样时间 ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_28_5Cycles); //ADC Calibration-ADC校准 ADC_GetCalibrationFactor(ADC1); //Enable the auto delay feature-启用自动延时功能 ADC_WaitModeCmd(ADC1, ENABLE); //使能ADC1 ADC_AutoPowerOffCmd(ADC1, ENABLE); //Enable ADCperipheral[PerIdx]-启用ADC外设[PerIdx] ADC_Cmd(ADC1, ENABLE); //Wait the ADCEN falg-等待ADC1校准完成 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); //ADC1 regular Software Start Conv ADC_StartOfConversion(ADC1); }
/** * @brief ADC1 channel12 configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* ADC1 DeInit */ ADC_DeInit(ADC1); /* Enable ADC and GPIO clocks ****************************************/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC1 Channel12 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 12 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_12 , ADC_SampleTime_239_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable DMA request after last transfer (OneShot-ADC mode) */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
/** * @brief Initializes the ADC Distance sensor ... * @param None * @retval None */ void Dist_Init(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* DIST GPIO Periph clock enable */ RCC_AHBPeriphClockCmd(DIST_GPIO_CLK, ENABLE); /* DIST ADC Periph clock enable */ RCC_APB2PeriphClockCmd(DIST_ADC_CLK, ENABLE); /* Configure DIST as analog input */ GPIO_InitStructure.GPIO_Pin = DIST_PIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(DIST_GPIO_PORT, &GPIO_InitStructure); /* ADC DeInit */ ADC_DeInit(DIST_ADC); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the TRIM ADC in a 12 bits resolutuion */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(DIST_ADC, &ADC_InitStructure); /* Convert the DIST Channel with 239.5 Cycles as sampling time */ ADC_ChannelConfig(DIST_ADC, DIST_ADC_CHANNEL, ADC_SampleTime_239_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(DIST_ADC); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(DIST_ADC, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(DIST_ADC, ADC_FLAG_ADEN)); }
void init_ADC(){ GPIO_InitTypeDef gpioinitADC; #if defined(CX_10_RED_BOARD) gpioinitADC.GPIO_Pin = GPIO_Pin_2; #endif #if defined(CX_10_BLUE_BOARD) gpioinitADC.GPIO_Pin = GPIO_Pin_7; //-5 #endif gpioinitADC.GPIO_Mode = GPIO_Mode_AN; gpioinitADC.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &gpioinitADC); ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init (ADC1, &ADC_InitStructure); #if defined(CX_10_RED_BOARD) ADC_ChannelConfig (ADC1, ADC_Channel_2, ADC_SampleTime_239_5Cycles); #endif #if defined(CX_10_BLUE_BOARD) ADC_ChannelConfig (ADC1, ADC_Channel_7, ADC_SampleTime_239_5Cycles); #endif ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 4; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); ADC_GetCalibrationFactor(ADC1); ADC_Cmd (ADC1, ENABLE); while (!ADC_GetFlagStatus (ADC1, ADC_FLAG_ADEN)); }
void analogin_init(analogin_t *obj, PinName pin) { ADC_TypeDef *adc; ADC_InitTypeDef ADC_InitStructure; // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); MBED_ASSERT(obj->adc != (ADCName)NC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); // Save pin number for the read function obj->pin = pin; // The ADC initialization is done once if (adc_inited == 0) { adc_inited = 1; // Get ADC registers structure address adc = (ADC_TypeDef *)(obj->adc); // Enable ADC clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // Configure ADC ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(adc, &ADC_InitStructure); // Calibrate ADC ADC_GetCalibrationFactor(adc); // Enable ADC ADC_Cmd(adc, ENABLE); } }
unsigned short ADC_Conf (void) { unsigned short cal = 0; ADC_InitTypeDef ADC_InitStructure; if (!RCC_ADC_CLK) RCC_ADC_CLK_ON; ADC_ClockModeConfig(ADC1, ADC_ClockMode_SynClkDiv4); // preseteo de registros a default /* ADCs DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continuous mode with a resolution equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); //software by setting bit ADCAL=1. //Calibration can only be initiated when the ADC is disabled (when ADEN=0). //ADCAL bit stays at 1 during all the calibration sequence. //It is then cleared by hardware as soon the calibration completes cal = ADC_GetCalibrationFactor(ADC1); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); SetADC1_SampleTime (); return cal; }
void ADC1_Config(void) { ADC_InitTypeDef ADC_InitStructure; /* ADC1 DeInit */ ADC_DeInit(ADC1); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 input with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_1_5Cycles); //ADC_ChannelConfig(ADC1, 1, ADC_SampleTime_55_5Cycles); // PA.1 - IN1 /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
void adc_init() { GPIO_InitTypeDef GPIO_init_structure; ADC_InitTypeDef ADC_init_structure; // Enable the ADC interface clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // ADC pins configuration // 1. Enable the clock for the ADC GPIOs // 2. Configure these ADC pins in analog mode using GPIO_Init(); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_init_structure.GPIO_Pin = GPIO_Pin_0; GPIO_init_structure.GPIO_Mode = GPIO_Mode_AN; GPIO_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_init_structure); // Configure the ADC conversion resolution, data alignment, external // trigger and edge, scan direction and Enable/Disable the continuous mode // using the ADC_Init() function. ADC_init_structure.ADC_Resolution = ADC_Resolution_12b; ADC_init_structure.ADC_ContinuousConvMode = ENABLE; ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right; ADC_init_structure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_init_structure); // Calibrate ADC before enabling ADC_GetCalibrationFactor(ADC1); // Activate the ADC peripheral using ADC_Cmd() function. ADC_Cmd(ADC1, ENABLE); // Wait until ADC enabled while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN) == RESET); }
/** * @brief ADC configuration * @note * @retval */ void ADC_Cfg(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_Cmd(ADC1, DISABLE); ADC_VrefintCmd(ENABLE); ADC_GetCalibrationFactor(ADC1);//校准 ADC1->CHSELR |= ADC_CHSELR_CHSEL0 | ADC_CHSELR_CHSEL17 ; ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; ADC_Cmd(ADC1, ENABLE); /* GPIOA Clocks enable */ RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE); /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); }
int main(void) { /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); /* Add your application code here */ //SysTick_Type SysTick1; //SysTick1.LOAD = 48000; //SysTick1.VAL = 0; USART1Init(); USART1print((int32_t)RCC_Clocks.HCLK_Frequency); SysTick_Config(RCC_Clocks.HCLK_Frequency/10000); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC Channel11 as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADCs DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_InitTypeDef ADCInit; ADC_StructInit(&ADCInit); ADCInit.ADC_ContinuousConvMode = ENABLE; ADCInit.ADC_DataAlign = ADC_DataAlign_Right; ADCInit.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None; ADCInit.ADC_Resolution = ADC_Resolution_12b; ADCInit.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADCInit); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_1_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); USART1println("INIT!"); /* Infinite loop */ while (1) { ++x; //while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //USART1println(ADC_GetConversionValue(ADC1)); } }
void temperaInit(void){ //clock /* Enable GPIOA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); NVIC_InitTypeDef NVIC_InitStructure; /* NVIC configuration */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //io /* Configure PA.02 as analog input */ GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStruct); /* DMA1 Channel1 Config */ DMA_InitTypeDef DMA_InitStruct; DMA_DeInit(DMA1_Channel1); DMA_InitStruct.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&adcbuf; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStruct.DMA_BufferSize = lenthof(adcbuf); DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High; DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStruct); DMA_ITConfig(DMA1_Channel1, DMA_IT_TC,ENABLE); DMA_Cmd(DMA1_Channel1, ENABLE); //ADC ADC_DeInit(ADC1); ADC_InitTypeDef ADC_InitStruct; ADC_StructInit(&ADC_InitStruct); ADC_InitStruct.ADC_Resolution = ADC_Resolution_10b; ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStruct); ADC_ChannelConfig(ADC1, ADC_Channel_2 , ADC_SampleTime_239_5Cycles); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); ADC_GetCalibrationFactor(ADC1); ADC_ClockModeConfig(ADC1, ADC_ClockMode_SynClkDiv4); //start adc ADC_Cmd(ADC1, ENABLE); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); }
void ADC_DMA_Init(void) { ADC_InitTypeDef ADC_InitStruct; DMA_InitTypeDef DMA_InitStruct; /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); /* ADC1 DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStruct); /* Configure the ADC1 in continous mode with a resolutuion equal to 12 bits */ ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStruct); /* ADC CLOCK IS 12MHz*/ //0.05MHz, the minimum sampling rate ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_2, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_8, ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_9, ADC_SampleTime_239_5Cycles); // ADC_VrefintCmd(ENABLE); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)ADC_DR_Address; DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&ADC_Data; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStruct.DMA_BufferSize = 6; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High; DMA_InitStruct.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStruct); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); }
/** * @brief ADC1 channel with DMA configuration * @param None * @retval None */ void ADC1_DMA_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* ADC1 DeInit */ ADC_DeInit(ADC1); /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); /* Configure ADC Channel11 as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 4; 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); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 1 with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_55_5Cycles); /* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles); ADC_TempSensorCmd(ENABLE); /* Convert the ADC1 Vref with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles); ADC_VrefintCmd(ENABLE); /* Convert the ADC1 Vbat with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_Vbat , ADC_SampleTime_55_5Cycles); ADC_VbatCmd(ENABLE); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
void adc_init(void) { ADC_InitTypeDef ADC_InitStructure; { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = BATTERYPIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(BATTERYPORT, &GPIO_InitStructure); } RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); { DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x40012440; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcarray; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 2; 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); } ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); ADC_DMACmd(ADC1, ENABLE); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, BATTERY_ADC_CHANNEL , ADC_SampleTime_239_5Cycles); ADC_VrefintCmd(ENABLE); ADC_GetCalibrationFactor(ADC1); ADC_Cmd(ADC1, ENABLE); ADC_StartOfConversion(ADC1); DMA_Cmd(DMA1_Channel1, ENABLE); // reference is measured a 3.3v, we are powered by 2.8, so a 1.17 multiplier // different vccs will translate to a different adc scale factor, // so actual vcc is not important as long as the voltage is correct in the end vref_cal = 1.17857f * (float) ( adcref_read ((adcrefcal *) 0x1FFFF7BA) ); }
void adc_init(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; uint8_t i; /* clear buffers */ for(i=0;i<4;i++) { adc_buffer[i] = adc_prev_buffer[i] = 0; } /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t *)(&ADC1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adc_buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 4; 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); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* GPIOA Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC Channel1,2,3 as analog inputs */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); /* ADCs DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continuous mode with a resolution equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 1,2,3 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_0 , ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_2 , ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, ADC_Channel_3 , ADC_SampleTime_239_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable the ADC peripheral */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY flag */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
//============================================================================== // ADC 初始化 //============================================================================== void F_InitialADC(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // 聲明定時器初始化結構體 TIM_OCInitTypeDef TIM_OCInitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(P_VR2_GPIO_CLK, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* TIM3 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Configure ADC Channel0 as analog input */ GPIO_InitStructure.GPIO_Pin = P_VR2_PIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(P_VR2_GPIO_PORT, &GPIO_InitStructure); /* TIM3 Configuration *******************************************************/ TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM3 TRGO selection */ TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); /* ADC2 Configuration *******************************************************/ /* ADCs DeInit */ ADC_DeInit(ADC1); /* Configure the ADC1 in continous mode withe a resolutuion equal to 8 bits*/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 0 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, P_VR2_Channel , ADC_SampleTime_28_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable the auto delay feature */ ADC_WaitModeCmd(ADC1, ENABLE); /* Enable the Auto power off mode */ ADC_AutoPowerOffCmd(ADC1, ENABLE); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* TIM2 enable counter */ TIM_Cmd(TIM3, ENABLE); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
void ConfigKernel() { //настраиваем ADC порт Current GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); //Voltage GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOB, &GPIO_InitStructure); DMA_InitTypeDef DMA_InitStructure; /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 2; 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); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* ADC1 configuration ---------------*/ ADC_InitTypeDef ADC_InitStructure; ADC_DeInit(ADC1); ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;// DISABLE; // вкл/выкл непрерывное преобразование ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;// выравнивание ADC_InitStructure.ADC_Resolution=ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_T15_TRGO;//чтобы ошибка не срабатывала ADC_InitStructure.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ScanDirection=ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel configuration */ ADC_ChannelConfig(ADC1, ADC_Channel_6, ADC_SampleTime_55_5Cycles);//current ADC_ChannelConfig(ADC1, ADC_Channel_9, ADC_SampleTime_55_5Cycles);//voltage /* Check the end of ADC1 reset calibration register */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); //настраиваем TIM2_CH2 /* GPIOA Configuration: Channel 2 PA1*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ; GPIO_Init(GPIOA, &GPIO_InitStructure); //прикрепляем АФ к таймеру 2 GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Period = TimerPeriod; //TIM_TimeBaseStructure.TIM_Prescaler =3; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); /* Channel 2 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_Pulse = Channel2CCR; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //Not use N port #ifdef INVERSEN TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //Not use N port TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //Not use N port #else TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //Not use N port TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set; //Not use N port #endif TIM_OC2Init(TIM2, &TIM_OCInitStructure); // Как я понял - автоматическая перезарядка таймера. TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM2, ENABLE); TIM_CCxCmd(TIM2, TIM_Channel_2, TIM_CCx_Enable); /* TIM2 counter enable */ TIM_Cmd(TIM2, ENABLE); ADC_StartOfConversion(ADC1); State=StateStart; }
/** * @brief analogRead Read GPIO Arduino pin value * @param pin number * @retval 0 to max STM32 ADC value */ uint16_t analogRead(uint16_t pin){ uint16_t L_oldType = ArduinoPort[pin].PinMode; //Does the pin already configured? uint32_t L_adcChannel; uint16_t L_ADCConvertedValue=0; /* Check the parameters */ assert_param(IS_ADC_PIN(pin)); switch(pin){ //pin to configure or to get value from case A0: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; L_adcChannel = ADC_Channel_10; break; case A1: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; L_adcChannel = ADC_Channel_11; break; case A2: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; L_adcChannel = ADC_Channel_12; break; case A3: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; L_adcChannel = ADC_Channel_13; break; case A4: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; L_adcChannel = ADC_Channel_14; break; case A5: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; L_adcChannel = ADC_Channel_15; break; default: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; L_adcChannel = ADC_Channel_10; break; } if(L_oldType != INPUT_AN) { //Pin was not correctly configured! Do it first! //ADC Config RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; /* GPIOC config for ADC */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //Config pin as AN GPIO_Init(GPIOC, &GPIO_InitStructure); //Config ADC ADC_Init(ADC1, &ADC_InitStructure); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); //Pin is configured as ADC INPUT now. ArduinoPort[pin].PinMode = INPUT_AN; } //Select channel to scan ADC_ChannelConfig(ADC1, L_adcChannel , ADC_SampleTime_239_5Cycles); // ADC1 regular Software Start Conv ADC_StartOfConversion(ADC1); // Wait for end of conversion while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //Get value L_ADCConvertedValue = ADC_GetConversionValue(ADC1); //Stop conversion and return ADC value. ADC_StopOfConversion(ADC1); return(L_ADCConvertedValue); }
/** * @brief ADC1 channel configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC Channel11 as analog input */ #ifdef USE_STM320518_EVAL GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; #elif defined (USE_STM32072B_EVAL) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; #endif /* USE_STM320518_EVAL */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADC1 DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continuous mode withe a resolution equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel11 and channel10 with 239.5 Cycles as sampling time */ #ifdef USE_STM320518_EVAL ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles); #elif defined (USE_STM32072B_EVAL) ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles); #endif /* USE_STM320518_EVAL */ /* Analog watchdog config ******************************************/ /* Configure the ADC Thresholds between 1.5V and 2.5V (1861, 3102) */ ADC_AnalogWatchdogThresholdsConfig(ADC1, 3102, 1861); /* Enable the ADC1 single channel */ ADC_AnalogWatchdogSingleChannelCmd(ADC1, ENABLE); ADC_OverrunModeCmd(ADC1, ENABLE); /* Enable the ADC1 analog watchdog */ ADC_AnalogWatchdogCmd(ADC1,ENABLE); /* Select a single ADC1 channel 11 */ #ifdef USE_STM320518_EVAL ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_AnalogWatchdog_Channel_11); #elif defined (USE_STM32072B_EVAL) ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_AnalogWatchdog_Channel_10); #endif /* USE_STM320518_EVAL */ /* Enable AWD interrupt */ ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE); /* Configure and enable ADC1 interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the ADC1 Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable the ADC peripheral */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY flag */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
/** * @brief ADC1 channel11 configuration * @param None * @retval None */ void ADC1_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Configure ADC Channel11 as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* TIM3 Configuration *******************************************************/ TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM3 TRGO selection */ TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); /* ADC1 Configuration *******************************************************/ /* ADCs DeInit */ ADC_DeInit(ADC1); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits*/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_28_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); #if defined (ADC_LOWPOWER) /* Enable the wait conversion mode */ ADC_WaitModeCmd(ADC1, ENABLE); /* Enable the Auto power off mode */ ADC_AutoPowerOffCmd(ADC1, ENABLE); #endif /* Enable OVR interupts */ ADC_ITConfig(ADC1, ADC_IT_OVR, ENABLE); /* Configure and enable ADC1 interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* TIM2 enable counter */ TIM_Cmd(TIM3, ENABLE); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
/** * @brief ADC1 configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOC Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC Channel11(for STM320518 EVAL) and Chanenl10(for STM32072B EVAL) as analog input */ #ifdef USE_STM320518_EVAL GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; #else GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; #endif /* USE_STM320518_EVAL */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADCs DeInit */ ADC_DeInit(ADC1); /* Configure the ADC1 in continous mode withe a resolution equal to 12 bits*/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 11 and channel10 with 28.5 Cycles as sampling time */ #ifdef USE_STM320518_EVAL ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_28_5Cycles); #else ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_28_5Cycles); #endif /* USE_STM320518_EVAL */ /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable OVR interupt */ ADC_ITConfig(ADC1, ADC_IT_OVR, ENABLE); /* Configure and enable ADC1 interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the ADC peripheral */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY flag */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); #if defined (ADC_LOWPOWER) /* Enable the wait conversion mode */ ADC_WaitModeCmd(ADC1, ENABLE); /* Enable the Auto power off mode */ ADC_AutoPowerOffCmd(ADC1, ENABLE); #endif /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
void init_adc( void ) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; // GPIOA Periph clock enable RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_StructInit(&GPIO_InitStructure); //Configure ADC Channel1/2/3/4 PA1/2/3/4 as analog input GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 ); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); // ADC1 DeInit ADC_DeInit(ADC1); // ADC1 Periph clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // DMA1 clock enable RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); // DMA1 Channel1 Config DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = NO_SAMPLES * NO_CHANNELS; 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); // DMA1 Channel1 enable DMA_Cmd(DMA1_Channel1, ENABLE); // ADC DMA request in circular mode ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); // Enable ADC_DMA ADC_DMACmd(ADC1, ENABLE); // Initialize ADC structure ADC_StructInit(&ADC_InitStructure); // Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); ADC_JitterCmd(ADC1, ADC_JitterOff_PCLKDiv4, ENABLE); //ADC Frequency set as 12MHz //With 5 ADC readings at 239.5 + 12.5 ADC Cycles //this gives a sampling rate of // Convert the ADC_SOL_V with 239.5 + 12.5 = ADC Cycles as sampling time ADC_ChannelConfig(ADC1, ADC_SOL_V , ADC_SampleTime_239_5Cycles); // Convert the ADC_SOL_I with 239.5 + 12.5 = ADC Cycles as sampling time ADC_ChannelConfig(ADC1, ADC_SOL_I , ADC_SampleTime_239_5Cycles); // Convert the ADC_BATT_V with 239.5 + 12.5 = ADC Cycles as sampling time ADC_ChannelConfig(ADC1, ADC_BATT_V , ADC_SampleTime_239_5Cycles); // Convert the ADC_BATT_I with 239.5 + 12.5 = ADC Cycles as sampling time ADC_ChannelConfig(ADC1, ADC_BATT_I , ADC_SampleTime_239_5Cycles); //Enable Temperature Sensor //>2.2us Sampling time required ADC_TempSensorCmd(ENABLE); ADC_ChannelConfig(ADC1, ADC_TEMP, ADC_SampleTime_239_5Cycles); //Get Temp Calibration Values ts_cal1 = *( (uint16_t*) 0x1FFFF7B8 ); ts_cal2 = *( (uint16_t*) 0x1FFFF7C2 ); // ADC Calibration ADC_GetCalibrationFactor(ADC1); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); // Wait the ADCEN falg while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); //adc_init_analog_watchdog(); // ADC1 regular Software Start Conv ADC_StartOfConversion(ADC1); }