最近在用C8051F350做一个电压表,可是调试ADC的时候出了问题,不清楚为什么ADC0L、ADC0M、ADC0H的内容一直为0.希望学过这款单片机的童鞋能够提供一下帮助。下面是我写的程序
初始化程序
void ADC0_Init (void)
{
REF0CN |= 0x03; // Enable internal Vref
ADC0CN = 0x00; // Gain = 1, Unipolar mode
ADC0CF = 0x00; // Interrupts upon SINC3 filter output
// and uses internal VREF
ADC0CLK = (SYSCLK/MDCLK)-1;
ADC0DEC = ((unsigned long) MDCLK / (unsigned long) OWR / (unsigned long) 128) - 1;
ADC0MD = 0x81; // Start internal calibration
while(AD0CALC != 1); // Wait until calibration is complete
EIE1 &= ~0x08; // Enable ADC0 Interrupts
ADC0MD &= ~0x07;
ADC0BUF = 0X00;
ADC0MD = 0x80; // Enable the ADC0 (IDLE Mode)
}
获取ADC的程序
unsigned long Ain(void)
{
unsigned long buf;
ADC0_Init();
ADC0MUX = 0x08; // select AIN7
AD0INT = 0;
ADC0MD |=0x83;
while(!AD0INT);
AD0INT = 0;
buf = ADC0H;
buf = buf<<16;
buf += (long)ADC0L + ((long)ADC0M<<8);
return buf;
}
没有用过这个型号。现在C8051可以用SimplicityStudio开发,它里面有可视化配置功能,可以产生代码,可以参考一下。以前的Config2应该也可以产生ADC的代码。
楼主用SILICON的单片机型号多吗?
QQ : 327097215 企业QQ:2880368999
北京懿芯雅创电子---元器件供应商
你这里好像没有使能ADC AD0EN = 1; 还有你使用的是查询方式的转换 启动转换是有AD0BUSY = 1; 我用的是C8051F310的单片机
void ADC0_Init (void)
{
ADC0CN = 0x00; // // ADC0 disabled, normal tracking,
// conversion triggered on TMR0 overflow
REF0CN = 0x0A; // Enable on-chip VREF, select Vdd as
// voltage reference.
AMX0N = 0x1F; // ADC0 negative input = GND
// i.e., single ended mode
ADC0CF = ((SYSCLK/3000000)-1)<<3; // set SAR clock to 3MHz
ADC0CF |= 0x00; // right-justify results
EIE1 |= 0x08; // enable ADC0 conversion complete int.
AD0EN = 1; // enable ADC0
}
abc(void)
{
AD0INT = 0;
AMX0P=0x03;
AD0BUSY = 1;
while(~AD0INT);
template=ADC0>>2;
AD0INT = 0;
}
这一段都是310的你参考一下吧!
好久没有过来了,问题早就解决了,主要是参考电压引脚没有设计好!