在使用STM8S105K6的ADC1的通道一进行AD转换时,转换出来的值不变且恒为2049,而10位AD最多只能到1024 ,程序反复检查也没有发现bug,我是开启了转换完成中断,在中断里面进行读取,ADC1的配置程序如下:
- void ADC_Config()
- {
- /* Init GPIO for ADC1 */
- GPIO_Init(GPIOB, GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);
- GPIO_Init(GPIOB, GPIO_PIN_2, GPIO_MODE_IN_FL_NO_IT);
-
- /* De-Init ADC peripheral*/
- ADC1_DeInit();
- /* Init ADC1 peripheral */
- ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_1, ADC1_PRESSEL_FCPU_D18,
- ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL1,
- DISABLE);
- ADC1_ConversionConfig(ADC1_CONVERSIONMODE_SINGLE,
- ADC1_CHANNEL_1,
- ADC1_ALIGN_RIGHT);
- /* Enable EOC interrupt */
- ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE);
- /* Enable general interrupts */
- enableInterrupts();
-
- /*Start Conversion */
- ADC1_Cmd(ENABLE);
- ADC1_StartConversion();
- }
复制代码
转换值读出之后通过串口打印出来转换值的读取程序如下:
/* start up converting every 500ms */
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
/* Debug code */
if(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == 0x80)
{
printf("ADC CH1 VALUE IS:%d
",ADC1_GetConversionValue());
// printf("%d
",ADC1_AIN1_Value);
ADC1_ClearITPendingBit(ADC1_IT_EOC);
}
}
此帖出自
小平头技术问答
一周热门 更多>