STM8S AD采样,读取全为FF???
void InitADC_Sample(void) //初始化AD
{
u8 i=0;
ADC_DeInit();
ADC_CR2 |= ADC_ALIGN;
ADC_CR1 = 0x00;// SPSEL = 12
ADC_CSR |= 0x01;
//ADC_TDRL = 0xFF;
//ADC_TDRH = 0xFF;
ADC_CR1 |= ADC_ADON; /* First set ADON to power on the ADC module. */
//i = 6; /* Wait >7us to ensure the ADC power on finished.*/
//while(i--);
delay(10);
ADC_CSR &= (~ADC_EOC);
memset(SensorChanel, 0, sizeof(SensorChanel));
SensorChanel.type = 1;
SensorChanel.type = 0;
SensorChanel.type = 0;
SensorChanel.type = 0;
}
/*
********************************************************************************
**函数名称:ADC_GetConversionValue
**函数功能:AD采样
**入口参数:AINx:通道号
**出口参数:采样值
**函数说明:none
**
********************************************************************************
*/
u16 ADC_GetConversionValue(u8 AINx)
{
u8 templ = 0;
u8 i=0;
u8 h=0,l=0;
u8 dt;
u16 temph = 0;
ADC_CSR &= (~ADC_EOC);
ADC_CR1 |= ADC_ADON; /* Set ADON again to start AD convert. */
delay(1);
while(!(ADC_CSR & ADC_EOC));/* Waiting for AD convert finished (EOP=1). */
ADC_CSR &= (~ADC_EOC);
/* Right alignment */
if (ADC_CR2 & ADC_ALIGN) {
ON_ALARM_LED();
/* Read LSB first */
templ = ADC_DRL;
/* Then read MSB */
temph = ADC_DRH & (0x03);
temph = (u16)(templ | (u16)(temph >8)&0xFF;
dt = h;dt=l;
SendBuf(dt,2);//这里打出的ad值为03 FF即全为1?????不知道哪里出了问题
}
return ((u16)temph);
}
例外:编译器好像对C库函数的调用有点问题?自己写了个printf函数如下:
int print(const char *format, ...)
{
char buff;
int chars;
va_list ap;
va_start(ap, format);
chars = vsprintf(buff, format, ap);//编译报错,说vsprintf变量未定义,这是库函数啊,这不是扯淡吗???
va_end(ap);
if (chars > 0)
{
SendBuf((u8 *)buff,chars);
return 1;
}
return 0;
}
RE:STM8S AD采样,读取全为FF???
据手册上说在ADC_ADON第一次使能延时后再使能一次ADC_ADON,看你的代码中好象没做这步,不知有没有影响。
页:
[1]