// // Boost_HW_SetAnalog // void Boost_HW_SetAnalog(void) { GPIO_InitTypeDef analogIo, vin; analogIo.GPIO_Mode = GPIO_Mode_AN; analogIo.GPIO_Pin = BOOST_VFB_PIN; vin.GPIO_Mode = GPIO_Mode_AN; vin.GPIO_Pin = BOOST_VIN_PIN; //Aciona clock do port onde esta localizado o A/D RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //Inicializa o pino de IO desejado: GPIO_Init(BOOST_VFB_PORT, &analogIo); GPIO_Init(BOOST_VFB_PORT, &vin); //Inicializa clock do ADC: RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); // Configura o A/D: ADC_InitTypeDef adcInit; ADC_CommonInitTypeDef adcCommon; ADC_StructInit(&adcInit); ADC_CommonStructInit(&adcCommon); // // o adc vai rodar no modo mais simples, single conversion + 1 regular channel: // adcCommon.ADC_Clock = ADC_Clock_SynClkModeDiv1; adcCommon.ADC_Mode = ADC_Mode_Independent; adcInit.ADC_ExternalTrigEventEdge = ADC_ExternalTrigInjecEventEdge_None; adcInit.ADC_AutoInjMode = ADC_AutoInjec_Disable; adcInit.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; adcInit.ADC_DataAlign = ADC_DataAlign_Right; adcInit.ADC_Resolution = ADC_Resolution_12b; adcInit.ADC_NbrOfRegChannel = 1; // Prepara o sequencer: ADC_DeInit(ADC1); ADC_CommonInit(ADC1, &adcCommon); ADC_Init(ADC1, &adcInit); ADC_RegularChannelSequencerLengthConfig(ADC1,1); ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1,ADC_SampleTime_1Cycles5 ); // ADC Pronto para rodar. //ADC_VoltageRegulatorCmd(ADC1, ENABLE); uint32_t i = 0; //Aguarda o vreg estabilizar. for( i = 0 ; i < 0x7FFF; i++); ADC_Cmd(ADC1, ENABLE); }
void initial_adc(void) { GPIO_InitTypeDef structGPIO; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div10 /*RCC_ADC12PLLCLK_Div128*/); RCC_ADCCLKConfig(RCC_ADC34PLLCLK_Div10 /*RCC_ADC34PLLCLK_Div128*/); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12 | RCC_AHBPeriph_ADC34 | RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD, ENABLE); //configure GPIO structGPIO.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_2 | GPIO_Pin_6; structGPIO.GPIO_Mode = GPIO_Mode_AN; structGPIO.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &structGPIO); structGPIO.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_10 | GPIO_Pin_1 | GPIO_Pin_12; GPIO_Init(GPIOB, &structGPIO); structGPIO.GPIO_Pin = GPIO_Pin_5; GPIO_Init(GPIOC, &structGPIO); structGPIO.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 /*| GPIO_Pin_14*/; GPIO_Init(GPIOD, &structGPIO); //configure ADC /*power-on voltage reference*/ ADC_VoltageRegulatorCmd(ADC1, ENABLE); Delay(100); ADC_VoltageRegulatorCmd(ADC2, ENABLE); Delay(100); ADC_VoltageRegulatorCmd(ADC3, ENABLE); Delay(100); ADC_VoltageRegulatorCmd(ADC4, ENABLE); Delay(100); /*calibration*/ ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1) != RESET ); ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC2); while(ADC_GetCalibrationStatus(ADC2) != RESET ); ADC_SelectCalibrationMode(ADC3, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC3); while(ADC_GetCalibrationStatus(ADC3) != RESET ); ADC_SelectCalibrationMode(ADC4, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC4); while(ADC_GetCalibrationStatus(ADC4) != RESET ); /*init logic*/ ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_7; ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable /*ADC_ContinuousConvMode_Disable*/; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_RisingEdge; ADC_Init(ADC1, &ADC_InitStructure); ADC_Init(ADC2, &ADC_InitStructure); ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_4; ADC_Init(ADC3, &ADC_InitStructure); ADC_Init(ADC4, &ADC_InitStructure); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_RegSimul; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC1, &ADC_CommonInitStructure); ADC_CommonInit(ADC3, &ADC_CommonInitStructure); ADC_CommonInit(ADC2, &ADC_CommonInitStructure); ADC_CommonInit(ADC4, &ADC_CommonInitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_1Cycles5); ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 1, ADC_SampleTime_1Cycles5); //514.3 kHz ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 1, ADC_SampleTime_1Cycles5); ADC_RegularChannelConfig(ADC4, ADC_Channel_3, 1, ADC_SampleTime_1Cycles5); ADC_RegularChannelSequencerLengthConfig(ADC1, 1); ADC_RegularChannelSequencerLengthConfig(ADC2, 1); ADC_RegularChannelSequencerLengthConfig(ADC3, 1); ADC_RegularChannelSequencerLengthConfig(ADC4, 1); initial_opa(); //init NVIC ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); return; }