******************************************************************/
#define Wait1us _nop_();
#define Wait2us {_nop_();_nop_();}
#define Wait4us {Wait2us;Wait2us;}
#define Wait8us {Wait4us;Wait4us;}
#define Wait10us{Wait8us;Wait2us;}
#i nclude "INTRINS.H"
/************定義接口總線**************/
sbit Clock = P3 ^ 4;//時(shí)鐘口線
sbit DataIn = P3 ^ 2;//數(shù)據(jù)輸入口線
sbit DataOut = P3 ^ 5;//數(shù)據(jù)輸出口線
sbit ChipSelect = P3 ^ 3;//片選口線
sbit EOC = P2 ^ 7;//轉(zhuǎn)換完成口線
/************接口總線定義結(jié)束**********/
/*********函數(shù)名稱:ADCSelChannel********
*
*功能:轉(zhuǎn)換指定通道的模擬量為數(shù)字量
*
*說明:
*
*位置:1
*
****************************************/
unsigned int ADCSelChannel(unsigned char Channel)
{
unsigned int ConvertValue;
unsigned char i, Chan;
unsigned char ConvertValueL, ConvertValueH;
unsigned char delay;
ConvertValueL = ConvertValueH = 0;//初始化轉(zhuǎn)換結(jié)果
delay = 0;
if (EOC)
{
Clock = 0;
ChipSelect = 1;
Wait2us;
ChipSelect = 0;
Wait2us;
Channel = Channel << 4;
for (i = 0; i < 4; i ++) //輸入需要轉(zhuǎn)換的通道的編碼
{
Chan = Channel;
Chan = Chan >> 7;
DataIn = (bit)Chan;
Wait2us;
Clock = 1;
Clock = 0;
Channel = Channel << 1;
}
for (i = 0; i < 6;i ++) //輸入轉(zhuǎn)換時(shí)鐘
{
Clock = 1;
Clock = 0;
}
ChipSelect = 1;
//開始檢測轉(zhuǎn)換結(jié)束標(biāo)志,或者轉(zhuǎn)換超時(shí)出錯(cuò)
while ((!EOC) && (delay < 10))
{
Wait10us;
delay ++;
}
if (delay == 10)
{
return (0xFFFF);//轉(zhuǎn)換超時(shí),返回錯(cuò)誤代碼
}
else
{
Wait10us;
Clock = 0;
ChipSelect = 1;
Wait1us;
ChipSelect = 0;
Wait1us;
for (i = 0; i < 2; i ++) //讀取高二位bit值
{
Clock = 1;
DataOut = 1;
ConvertValueH <<= 1;
if (DataOut)
ConvertValueH |= 0x1;
Clock = 0;
Wait1us;
}
for (i = 0; i < 8; i ++) //讀取低八位bit值
{
Clock = 1;
DataOut = 1;
ConvertValueL <<= 1;
if (DataOut)
ConvertValueL |= 0x1;
Clock = 0;
Wait1us;
}
ChipSelect=1;
ConvertValue = ConvertValueH;
ConvertValue <<= 8;
ConvertValue |= ConvertValueL;
return (ConvertValue);//返回轉(zhuǎn)換結(jié)果
}
}
}
/************ADCSelChannel函數(shù)結(jié)束**********/