在ADC模块中,怎样进行初始化!
我使用的是库函数设置的但是不能正确转化。
程序如下:
#include "stm8s208rb.h"
#include "stm8s_gpio.h"
#include "stm8s_adc2.h"
void GPIO_Config(void);
void ADC_Config(void);
void delay(u32 ch)
{
while(ch--);
}
main()
{
u8 port_value=0;
u16 u16_ADC2_value=0;
GPIO_Config();
ADC_Config();
while(1)
{
port_value = GPIO_ReadInputData(GPIOF); //扫描ADC管脚,准备进行转化
port_value=port_value&0x20;
if(port_value==0x20) //如果ADC管脚为高电平,则进行转化,并进行点灯动作
{
delay(5);
port_value = GPIO_ReadInputData(GPIOF);
port_value=port_value&0x20;
if(port_value==0x20)
{
ADC2_StartConversion();
u16_ADC2_value = ADC2_GetConversionValue();
//ADC2_ClearFlag();
//ADC2_Cmd(DISABLE);
while( ADC2_GetFlagStatus()==RESET);
if(u16_ADC2_value > 906 && u16_ADC2_value < 1024)
{
GPIO_WriteHigh(GPIOB, GPIO_PIN_3);
}
if(u16_ADC2_value > 0 && u16_ADC2_value < 905 )
{
GPIO_WriteHigh(GPIOB, GPIO_PIN_2);
}
}
}
delay(500);
GPIO_Write(GPIOB, 0x00);
}
}
void GPIO_Config(void)
{
GPIO_Init(GPIOB, GPIO_PIN_ALL, GPIO_MODE_OUT_PP_HIGH_FAST); //LED灯的管脚
GPIO_Write(GPIOB, 0x00);
}
void ADC_Config(void)
{
GPIO_Init(GPIOF, GPIO_PIN_5, GPIO_MODE_IN_FL_NO_IT); //ADC模拟信号输入管脚
ADC2_DeInit();
ADC2_Init(ADC2_CONVERSIONMODE_CONTINUOUS, ADC2_CHANNEL_0, ADC2_PRESSEL_FCPU_D2,
ADC2_EXTTRIG_TIM, DISABLE, ADC2_ALIGN_RIGHT, ADC2_SCHMITTTRIG_CHANNEL0, DISABLE);
ADC2_Cmd(ENABLE);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif