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[0].type = 1;
SensorChanel[1].type = 0;
SensorChanel[2].type = 0;
SensorChanel[3].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[2];
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 << (u8)8));
l = temph&0xFF;
h = (temph>>8)&0xFF;
dt[0] = h;dt[1]=l;
SendBuf(dt,2);//这里打出的ad值为03 FF即全为1?????不知道哪里出了问题
}
return ((u16)temph);
}
例外:编译器好像对C库函数的调用有点问题?自己写了个printf函数如下:
<#include <stdarg.h>>
int print(const char *format, ...)
{
char buff[50];
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;
} |