代码编译,仿真运行时均不报错,就是LDR PA0引脚读出的数据一直为0,用探针实际测量有电压
单片机为STM32F103R6
光敏电阻连接在PA0,电路连接如下图所示(尝试过在LDR引脚处加电阻,有电压读出数据还为0)
adc初始化代码
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStruct);
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
- ADC_DeInit(ADC1);
- ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStruct.ADC_ScanConvMode = DISABLE;
- ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
- ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStruct.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1,&ADC_InitStruct);
-
- ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);
- ADC_Cmd(ADC1,ENABLE);
-
- //ADC_SoftwareStartConvCmd(ADC1,ENABLE);
-
- ADC_ResetCalibration(ADC1);
- delay_ms(500);
- //while(ADC_GetResetCalibrationStatus(ADC1)){OLED_ShowString(0,36,"LSENS Reset...",12);OLED_Refresh_Gram();};
- ADC_StartCalibration(ADC1);
- delay_ms(500);
- //while(ADC_GetCalibrationStatus(ADC1)){OLED_ShowString(0,36,"LSENS Strat...",12);OLED_Refresh_Gram();};
复制代码
读取adc值代码
- while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
- return ADC_GetConversionValue(ADC1);
复制代码 |