void adc_injected(int adc_trigger){
	uint32_t injected_trigger = 0;
	if(adc_trigger == ADC_TRIGGER_NONE){
		adc_init_injected(DONT_USE_TRIGGER,injected_trigger);
	}else{
		if(adc_trigger == ADC_TRIGGER_TIMER2){
			injected_trigger = ADC_ExternalTrigInjecConvEvent_2;
			TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);//OTRIG : Update
		}else if(adc_trigger == ADC_TRIGGER_TIMER6){
			injected_trigger = ADC_ExternalTrigInjecConvEvent_14;
			TIM_SelectOutputTrigger(TIM6,TIM_TRGOSource_Update);//OTRIG : Update
		}else if(adc_trigger == ADC_TRIGGER_TIMER15){
			injected_trigger = ADC_ExternalTrigInjecConvEvent_15;
			TIM_SelectOutputTrigger(TIM15,TIM_TRGOSource_Update);//OTRIG : Update
		}
		adc_init_injected(USE_TRIGGER,injected_trigger);
	}
	ADC_StartInjectedConversion(ADC1);
}
Beispiel #2
0
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);

}
Beispiel #3
0
//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);


}