你好,我使用STC12C5410AD进行AD转换,将模拟电压信号从外界输入转换成数字信号输出,想请教C程序怎么写?

2025-01-02 11:50:08
推荐回答(2个)
回答1:

uchar get_AD_result(uchar channel)
{
uchar AD_finished=0;
ADC_RES = 0;
channel &= 0x07;
ADC_CONTR = AD_SPEED;
_nop_();
ADC_CONTR |= channel;
_nop_();
ADC_CONTR |= 0x80;
delay(1);
ADC_CONTR |= 0x08;
AD_finished = 0;
while (AD_finished ==0 )
{
AD_finished = (ADC_CONTR & 0x10);
}
ADC_CONTR &= 0xE7;
return (ADC_RES);
}

把AD的结果通过IO口输出就可以了

这个程序P1.7做AD脚的,你稍微小改下,有什么迷糊的,直接搬下来用就OK了,反正我一直这么用,他也没脾气。

STC的单片机基本都差不多少,自己修改下就可以了

回答2:

5604ad的
uint GetADCResult(uchar ch)
{ uint temp=0;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
delay_us(4); //Must wait before inquiry

while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
temp=((temp|ADC_DATA)<<8)&0xff00|ADC_LOW2;
return temp; //Return ADC result
}

/*----------------------------
Initial ADC
----------------------------*/
void InitADC()
{
P1 = P1M0 = P1M1 = 0xff; //Set all P1 as Open-Drain mode
ADC_DATA = 0; //Clear previous result
ADC_LOW2 = 0;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
delay_ms(10); //ADC power-on and delay
}