端午节学习NEC单片机ad模块成功!
P2.7连接一电位器,,P1口连接8个LED。调节电位器输入,利用8位led上的亮灭来表示转换后的数字输出。欢迎各位朋友交流!一起学习NEC单片机,其乐无穷!
#pragma sfr
#pragma ei
#pragma di
#define uchar unsigned char
#define uint unsigned int
void SysInit();
uchar A2D(uchar chn);
void main()
{
IMS = 0xc8;
IXS = 0x0c;
DI();
SysInit();
EI();
PM1 = 0x00;
P1 = 0xff;
ADPC = 0x04;
P2 = 0xff;
for(;;)
{
P1 = A2D(0x07);
}
}
//--------------------------------System Initialization--------------------
void SysInit()
{
OSCCTL = 0x40;
PCC = 0x00; //Fcpu=Fxp
RCM = 0x83; //lsrstop,rstop
MOC = 0x00; //X1
MCM = 0x07;
}
//--------------------------------AD conversion-----------------------------
uchar A2D(uchar chn)
{
uchar result;
ADCE = 1; //Start AD Comparator
ADPC = 0x07; //Set Register Channel
PM2.7 = 0;
ADS = chn; // Select AD Channel
ADCS = 1; // AD conversion enable
while(!ADIF);
result = ADCRH; // 8bit
ADCS = 0;
ADIF = 0;
return result;
}