STM32 ADC注入通道的使用
扫描二维码
随时随地手机看文章
通过ADC注入通道的使用,可以实时获取4个GPIO输入的AD采样值,下面看具体的代码。
芯片:STM32F205VCT6
STEP1:初始化GPIO
voidGPIO_Configuration(void){//GPIO-PC0PC1PC2PC3-ADC3GPIO_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_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOC,&GPIO_InitStructure);}123456789
STEP2:初始化ADC
voidADC_Configuration(void){ADC_InitTypeDefADC_InitStructure;ADC_CommonInitTypeDefADC_CommonInitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3,ENABLE);//ADCCommonInitADC_CommonInitStructure.ADC_Mode=ADC_Mode_Independent;ADC_CommonInitStructure.ADC_Prescaler=ADC_Prescaler_Div4;ADC_CommonInitStructure.ADC_DMAAccessMode=ADC_DMAAccessMode_Disabled;ADC_CommonInitStructure.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_20Cycles;ADC_CommonInit(&ADC_CommonInitStructure);//ADC3InitADC_InitStructure.ADC_Resolution=ADC_Resolution_12b;ADC_InitStructure.ADC_ScanConvMode=ENABLE;ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;ADC_InitStructure.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_T1_CC1;ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;ADC_InitStructure.ADC_NbrOfConversion=1;ADC_Init(ADC3,&ADC_InitStructure);ADC_TempSensorVrefintCmd(ENABLE);ADC_RegularChannelConfig(ADC3,ADC_Channel_Vrefint,1,ADC_SampleTime_144Cycles);ADC_AutoInjectedConvCmd(ADC3,ENABLE);ADC_ExternalTrigInjectedConvEdgeConfig(ADC3,ADC_ExternalTrigInjecConvEdge_None);ADC_InjectedSequencerLengthConfig(ADC3,4);ADC_InjectedChannelConfig(ADC3,ADC_Channel_10,3,ADC_SampleTime_144Cycles);ADC_InjectedChannelConfig(ADC3,ADC_Channel_11,4,ADC_SampleTime_144Cycles);ADC_InjectedChannelConfig(ADC3,ADC_Channel_12,2,ADC_SampleTime_144Cycles);ADC_InjectedChannelConfig(ADC3,ADC_Channel_13,1,ADC_SampleTime_144Cycles);ADC_SetInjectedOffset(ADC3,ADC_InjectedChannel_1,2000);//采样后的值减去阈值,可以省去后续的处理ADC_SetInjectedOffset(ADC3,ADC_InjectedChannel_2,2000);//EnableADC3ADC_Cmd(ADC3,ENABLE);ADC_SoftwareStartConv(ADC3);}123456789101112131415161718192021222324252627282930313233343536373839404142
STEP3:使用
value1=ADC_GetInjectedConversionValue(ADC3,ADC_InjectedChannel_1);value2=ADC_GetInjectedConversionValue(ADC3,ADC_InjectedChannel_2);value3=ADC_GetInjectedConversionValue(ADC3,ADC_InjectedChannel_3);value4=ADC_GetInjectedConversionValue(ADC3,ADC_InjectedChannel_4);