void adc_init_injected(int use_trigger, int trigger){ //Confiure pins PA0[AN1], PA1[AN2] for analog input operation RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); GPIO_InitTypeDef myGPIO; GPIO_StructInit(&myGPIO); myGPIO.GPIO_Pin=(GPIO_Pin_1|GPIO_Pin_0); myGPIO.GPIO_Mode=GPIO_Mode_AN; GPIO_Init(GPIOA,&myGPIO); //Configure ADC RCC_ADCCLKConfig(RCC_ADC12PLLCLK_OFF); RCC_AHBPeriphClockCmd(RCC_AHBENR_ADC12EN,ENABLE); ADC_CommonInitTypeDef myADC_Comm; ADC_CommonStructInit(&myADC_Comm); myADC_Comm.ADC_Clock=ADC_Clock_SynClkModeDiv1; ADC_CommonInit(ADC1,&myADC_Comm); ADC_VoltageRegulatorCmd(ADC1,ENABLE); /*Initial calibration*/ ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1) != RESET); ADC_GetCalibrationValue(ADC1); ADC_InjectedInitTypeDef myADC; ADC_InjectedStructInit(&myADC); myADC.ADC_ExternalTrigInjecEventEdge= (use_trigger) ? ADC_ExternalTrigInjecEventEdge_RisingEdge : ADC_ExternalTrigInjecEventEdge_None; //Connect timer with adc myADC.ADC_ExternalTrigInjecConvEvent=trigger;//Start convertion on TIM2_OTRIG myADC.ADC_NbrOfInjecChannel=2; myADC.ADC_InjecSequence1=ADC_InjectedChannel_1; myADC.ADC_InjecSequence2=ADC_InjectedChannel_1; myADC.ADC_InjecSequence3=ADC_InjectedChannel_1; myADC.ADC_InjecSequence4=ADC_InjectedChannel_2; ADC_InjectedInit(ADC1,&myADC); ADC_InjectedChannelSampleTimeConfig(ADC1,ADC_InjectedChannel_1,ADC_SampleTime_7Cycles5); ADC_InjectedChannelSampleTimeConfig(ADC1,ADC_InjectedChannel_2,ADC_SampleTime_7Cycles5); // ADC_ITConfig(ADC1, ADC_IT_JEOS, ENABLE); NVIC_EnableIRQ(ADC1_IRQn); /* wait for ADRDY */ ADC_Cmd(ADC1,ENABLE); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); }
void ADC_init(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; GPIO_InitTypeDef GPIO_InitStructure; ADC_InjectedInitTypeDef ADC_InjectedInitStruct; RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 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); ADC_VoltageRegulatorCmd(ADC1, ENABLE); Delay_ms(10); ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1) != RESET ); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; 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_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_InjectedInitStruct.ADC_ExternalTrigInjecConvEvent = ADC_ExternalTrigInjecConvEvent_9; ADC_InjectedInitStruct.ADC_ExternalTrigInjecEventEdge = ADC_ExternalTrigInjecEventEdge_RisingEdge; ADC_InjectedInitStruct.ADC_InjecSequence1 = ADC_InjectedChannel_1; /* to PA0 (Iout) */ ADC_InjectedInitStruct.ADC_InjecSequence2 = ADC_InjectedChannel_2; /* to PA1 (Vout) */ ADC_InjectedInitStruct.ADC_InjecSequence3 = ADC_InjectedChannel_3; /* to PA2 (Vext) */ ADC_InjectedInitStruct.ADC_InjecSequence4 = ADC_InjectedChannel_4; /* to PA3 (Vin) */ ADC_InjectedInitStruct.ADC_NbrOfInjecChannel = 4; ADC_InjectedInit(ADC1, &ADC_InjectedInitStruct); /* ADC1 channel2 configuration */ ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_1, ADC_SampleTime_2Cycles5); /* Iout */ ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_2, ADC_SampleTime_2Cycles5); /* Vout */ ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_3, ADC_SampleTime_2Cycles5); /* Vext */ ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_4, ADC_SampleTime_2Cycles5); /* Vin */ /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* wait for ADRDY */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); /* Start ADC1 Injected Conversions */ ADC_StartInjectedConversion(ADC1); }
void ADC_Configuration(void) { __IO uint16_t calibration_value = 0; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_InjectedInitTypeDef ADC_InjInitStructure; ADC_StructInit(&ADC_InitStructure); /* Calibration procedure */ ADC_VoltageRegulatorCmd(ADC2, ENABLE); /* Insert delay equal to 10 µs */ Delay(10); ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC2); while(ADC_GetCalibrationStatus(ADC2) != RESET ); calibration_value = ADC_GetCalibrationValue(ADC2); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_InjSimul; 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(ADC2, &ADC_CommonInitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Enable; ADC_InitStructure.ADC_NbrOfRegChannel = 3; ADC_Init(ADC2, &ADC_InitStructure); ADC_InjInitStructure.ADC_ExternalTrigInjecConvEvent = ADC_ExternalTrigInjecConvEvent_0; ADC_InjInitStructure.ADC_ExternalTrigInjecEventEdge = ADC_ExternalTrigInjecEventEdge_None; ADC_InjInitStructure.ADC_NbrOfInjecChannel = 3; ADC_InjInitStructure.ADC_InjecSequence1 = ADC_Channel_5; ADC_InjInitStructure.ADC_InjecSequence2 = ADC_Channel_11; ADC_InjInitStructure.ADC_InjecSequence3 = ADC_Channel_12; ADC_InjectedInit(ADC2, &ADC_InjInitStructure); /* ADC1 regular channel7 configuration */ //ADC_RegularChannelConfig(ADC2, ADC_Channel_5, 1, ADC_SampleTime_7Cycles5); ADC_InjectedChannelSampleTimeConfig(ADC2, ADC_Channel_5, ADC_SampleTime_7Cycles5); ADC_InjectedChannelSampleTimeConfig(ADC2, ADC_Channel_11,ADC_SampleTime_7Cycles5); ADC_InjectedChannelSampleTimeConfig(ADC2, ADC_Channel_12,ADC_SampleTime_7Cycles5); /* Enable ADC1 */ ADC_Cmd(ADC2, ENABLE); /* wait for ADRDY */ while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY)); /* Start ADC1 Software Conversion */ ADC_StartConversion(ADC2); }
//CURRENT_ADC: PA0 ADC1_IN1 //OUTPUT_ADC: PA1 ADC1_IN2 //SUPPLY_ADC: PA2 ADC1_IN3 //AUDIO_ADC: PA3 ADC1_IN4 void ADC_config(){ ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_InjectedInitTypeDef ADC_InjectedInitStruct; /* Configure the ADC clock */ RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1); /* Enable ADC1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); //enable power to ADC ADC_VoltageRegulatorCmd(ADC1, ENABLE); msDelay(1); //allow power to stabilize //calibrate the ADC ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1) != RESET ); //wait for finish //structure to init ADC1 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; 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); //init the ADC //more detailed init structure //do not set up in regular sampling mode, want injected sampling mode ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); //set up ADC injection sampling //injected event comes from page 230 of the refrence manual //HRTIM_ADCTRG2 event ADC_InjectedInitStruct.ADC_ExternalTrigInjecConvEvent = ADC_ExternalTrigInjecConvEvent_9; ADC_InjectedInitStruct.ADC_ExternalTrigInjecEventEdge = ADC_ExternalTrigInjecEventEdge_RisingEdge; ADC_InjectedInitStruct.ADC_InjecSequence1 = ADC_InjectedChannel_1; /* corresponds to PA0 (CURRENT_ADC) */ ADC_InjectedInitStruct.ADC_InjecSequence2 = ADC_InjectedChannel_2; /* corresponds to PA1 (OUTPUT_ADC) */ ADC_InjectedInitStruct.ADC_InjecSequence3 = ADC_InjectedChannel_3; /* corresponds to PA2 (SUPPLY_ADC) */ ADC_InjectedInitStruct.ADC_InjecSequence4 = ADC_InjectedChannel_4; /* corresponds to PA3 (AUDIO_ADC) */ ADC_InjectedInitStruct.ADC_NbrOfInjecChannel = 4; ADC_InjectedInit(ADC1, &ADC_InjectedInitStruct); ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_1, ADC_SampleTime_7Cycles5); //~15ns time constant ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_2, ADC_SampleTime_7Cycles5); //clk is 14ns ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_3, ADC_SampleTime_7Cycles5); //~7 time constants ADC_InjectedChannelSampleTimeConfig(ADC1, ADC_Channel_4, ADC_SampleTime_7Cycles5); ADC_Cmd(ADC1, ENABLE); //enable ADC // wait for adc to be ready while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); //Start ADC1 Injected Conversions //this actually just enables it ADC_StartInjectedConversion(ADC1); }