void Init_ADC(void)
{
GPIO_Init(GPIOB, GPIO_PIN_0, GPIO_MODE_IN_FL_NO_IT);
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_0, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_ALL, ENABLE);
ADC1_Cmd(ENABLE);
}
void main(void)
{
FlagStatus flag_status;
u16 u16_adc1_value;
Init_UART2();
Init_ADC();
/* Infinite loop */
while (1)
{
//Send(0xf0);
ADC1_StartConversion();
do{
flag_status = ADC1_GetFlagStatus(ADC1_FLAG_EOC);
}
while(flag_status == RESET); // SET or RESET
u16_adc1_value = ADC1_GetConversionValue();
ADC1_ClearFlag(ADC1_FLAG_EOC);
flag_status = ADC1_GetFlagStatus(ADC1_FLAG_EOC);
UART2_sendhex((u16_adc1_value>>8));
UART2_sendhex((u16_adc1_value&0xff));
UART2_sendstr("\r\n");
}
}
单片机买的淘宝stm8s105k4t6核心板,debug时u16_adc1_value的值,我用可调电源输入大于2.1v时就是1023,小于2.1v就是0。flag_status的值一直是128,ADC1_ClearFlag(ADC1_FLAG_EOC)语句之后还是128不变。ADC1_SCHMITTTRIG_ALL, ENABLE改成ENABLE后情况一样,郁闷 |