通过ADC注入通道的使用,可以实时获取4个GPIO输入的AD采样值,下面看具体的代码。
芯片:STM32F205VCT6
STEP1:初始化GPIO
void GPIO_Configuration ( void ) { //GPIO -PC0 PC1 PC2 PC3 -ADC3 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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init ( GPIOC, &GPIO_InitStructure ); }123456789
STEP2:初始化ADC
void ADC_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);//ADC Common Init ADC_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);// ADC3 Init ADC_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);// Enable ADC3 ADC_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);
上一篇:STM32 ADC的规则通道和注入通道的区别
下一篇:嵌入式设计经验:ARM-WinCE平台时钟同步设计
推荐阅读最新更新时间:2024-03-16 15:36
设计资源 培训 开发板 精华推荐
- 泰克专家邀你了解大数据时代全新解决方案,参与翻盖有礼!
- 购买TI store MSP432P401R LaunchPad 晒单就送礼!
- 爱“拼”才会赢 英飞凌栅极驱动IC拼图闯关 活动开始啦!
- 看泰克视频讲座,了解先进医疗电子测试方案,填问卷,赢好礼!
- 下载泰克电源设计测试方案+图文攻略 帮助工程师解决电源效率问题。有好礼
- 有奖直播|MPS 新一代磁角度传感器MA600介绍与应用
- 是德科技:五招教您最小化合格/不合格的误判风险在线直播预报名、看直播、填问卷、好礼相送!
- TI 高精度实验室信号链精品课大作战——你学习我送礼!
- EEWorld邀你来拆解(第7期)——拆拆减肥利器,看看跳绳里都有什么