- //ADC采集通道
- #define ADC_MAX_CHAN 10 //通道总数
- #define ADC_PORT_MP0 GPIOA
- #define ADC_PINS_MP0 GPIO_Pin_4
- #define ADC_PORT_MP1 GPIOA
- #define ADC_PINS_MP1 GPIO_Pin_7
- #define ADC_PORT_MA0 GPIOA
- #define ADC_PINS_MA0 GPIO_Pin_5
- #define ADC_PORT_MA1 GPIOA
- #define ADC_PINS_MA1 GPIO_Pin_6
- #define ADC_CHAN_MA0 ADC_Channel_1
- #define ADC_CHAN_MA1 ADC_Channel_0
- #define ADC_PORT_RP0 GPIOD
- #define ADC_PINS_RP0 GPIO_Pin_2
- #define ADC_PORT_RP1 GPIOB
- #define ADC_PINS_RP1 GPIO_Pin_1
- #define ADC_PORT_RA0 GPIOD
- #define ADC_PINS_RA0 GPIO_Pin_3
- #define ADC_PORT_RA1 GPIOB
- #define ADC_PINS_RA1 GPIO_Pin_0
- #define ADC_CHAN_RA0 ADC_Channel_19
- #define ADC_CHAN_RA1 ADC_Channel_18
- #define ADC_PORT_FP0 GPIOB
- #define ADC_PINS_FP0 GPIO_Pin_2
- #define ADC_PORT_FP1 GPIOB
- #define ADC_PINS_FP1 GPIO_Pin_5
- #define ADC_PORT_FA0 GPIOB
- #define ADC_PINS_FA0 GPIO_Pin_3
- #define ADC_PORT_FA1 GPIOB
- #define ADC_PINS_FA1 GPIO_Pin_4
- #define ADC_CHAN_FA0 ADC_Channel_15
- #define ADC_CHAN_FA1 ADC_Channel_14
- #define ADC_PORT_LP0 GPIOB
- #define ADC_PINS_LP0 GPIO_Pin_6
- #define ADC_PORT_LP1 GPIOF
- #define ADC_PINS_LP1 GPIO_Pin_1
- #define ADC_PORT_LA0 GPIOB
- #define ADC_PINS_LA0 GPIO_Pin_7
- #define ADC_PORT_LA1 GPIOF
- #define ADC_PINS_LA1 GPIO_Pin_0
- #define ADC_CHAN_LA0 ADC_Channel_11
- #define ADC_CHAN_LA1 ADC_Channel_24
- #define ADC_PORT_BP0 GPIOD
- #define ADC_PINS_BP0 GPIO_Pin_4
- #define ADC_PORT_BP1 GPIOD
- #define ADC_PINS_BP1 GPIO_Pin_7
- #define ADC_PORT_BA0 GPIOD
- #define ADC_PINS_BA0 GPIO_Pin_5
- #define ADC_PORT_BA1 GPIOD
- #define ADC_PINS_BA1 GPIO_Pin_6
- #define ADC_CHAN_BA0 ADC_Channel_9
- #define ADC_CHAN_BA1 ADC_Channel_8
- //按顺序排列,方便模块化
- GPIO_TypeDef* ADCPORTSel[ADC_MAX_CHAN] = {ADC_PORT_MP0,ADC_PORT_MP1,ADC_PORT_RP0,ADC_PORT_RP1,ADC_PORT_FP0,ADC_PORT_FP1,ADC_PORT_LP0,ADC_PORT_LP1,ADC_PORT_BP0,ADC_PORT_BP1,};
- GPIO_Pin_TypeDef ADCPINSSel[ADC_MAX_CHAN] = {ADC_PINS_MP0,ADC_PINS_MP1,ADC_PINS_RP0,ADC_PINS_RP1,ADC_PINS_FP0,ADC_PINS_FP1,ADC_PINS_LP0,ADC_PINS_LP1,ADC_PINS_BP0,ADC_PINS_BP1,};
- ADC_Channel_TypeDef ADCCHANSel[ADC_MAX_CHAN] = {ADC_CHAN_MA0,ADC_CHAN_MA1,ADC_CHAN_RA0,ADC_CHAN_RA1,ADC_CHAN_FA0,ADC_CHAN_FA1,ADC_CHAN_LA0,ADC_CHAN_LA1,ADC_CHAN_BA0,ADC_CHAN_BA1,};
- GPIO_TypeDef* ADCPORTCH[ADC_MAX_CHAN] = {ADC_PORT_MA0,ADC_PORT_MA1,ADC_PORT_RA0,ADC_PORT_RA1,ADC_PORT_FA0,ADC_PORT_FA1,ADC_PORT_LA0,ADC_PORT_LA1,ADC_PORT_BA0,ADC_PORT_BA1,};
- GPIO_Pin_TypeDef ADCPINSCH[ADC_MAX_CHAN] = {ADC_PINS_MA0,ADC_PINS_MA1,ADC_PINS_RA0,ADC_PINS_RA1,ADC_PINS_FA0,ADC_PINS_FA1,ADC_PINS_LA0,ADC_PINS_LA1,ADC_PINS_BA0,ADC_PINS_BA1,};
- //读到的当前数据
- uint16_t recCurVal[ADC_MAX_CHAN];
- void GetADCValue(unsigned char ch)
- {
- uint16_t temp=0;
-
- if(ch<ADC_MAX_CHAN) //防止越界跑飞
- {
- GPIO_Init(ADCPORTCH[ch], ADCPINSCH[ch], GPIO_Mode_In_FL_No_IT); //设置ADC脚为输入
- ADC1_Init();
- GPIO_SetBits(ADCPORTSel[ch], ADCPINSSel[ch]); //打开电源
- ADC_ChannelCmd (ADC1,ADCCHANSel[ch],ENABLE); //使能
- Delay(50); //延时,电容充电要时间,约5ms
- ADC_SoftwareStartConv (ADC1); //开启软件转换
- while(!ADC_GetFlagStatus (ADC1,ADC_FLAG_EOC)); //等待转换结束
- ADC_ClearFlag (ADC1,ADC_FLAG_EOC); //清除相关标识
- temp = ADC_GetConversionValue(ADC1); //获取转换值
- GPIO_ResetBits(ADCPORTSel[ch], ADCPINSSel[ch]); //关闭电源
- ADC1_Closed();
- GPIO_Init(ADCPORTCH[ch] , ADCPINSCH[ch] , GPIO_Mode_Out_PP_Low_Slow); //设置ADC脚为输出低
-
- recCurVal[ch] = temp; //保存数据
-
- #ifdef DEBUG
- temp = temp*3000UL/4095UL; //转换成电压值
- USART1_SendInt(ch,temp);
- #endif
- }
- }
- void main(void)
- {
- static unsigned char aaa=0;
- PWR_FastWakeUpCmd(ENABLE); //快速唤醒使能
- RTC_Config();
- PowerOn_Init();
- aaa = 0;
- while (1)
- {
- GetADCValue(aaa);
- if(++aaa>=9) aaa = 0;
- GPIO_ResetBits(LED_PORT,LED_PINS);
- halt();
- GPIO_SetBits(LED_PORT,LED_PINS);
- }
- }
复制代码
程序如上,问题在于:
1:如果人为的用 GetADCValue(0); 或者GetADCValue(1); 或者GetADCValue(3); 等,串口打印ADC读到的数据完全没有问题
2:如果像上面那样用一个变量控制,则第一个 GetADCValue(0); 和 GetADCValue(7);数据是正确的,其他数据为0或者很小(小于5)
求解
一周热门 更多>