TLC1543 的源程序
扫描二维码
随时随地手机看文章
******************************************************************/
#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;//时钟口线
sbit DataIn = P3 ^ 2;//数据输入口线
sbit DataOut = P3 ^ 5;//数据输出口线
sbit ChipSelect = P3 ^ 3;//片选口线
sbit EOC = P2 ^ 7;//转换完成口线
/************接口总线定义结束**********/
/*********函数名称:ADCSelChannel********
*
*功能:转换指定通道的模拟量为数字量
*
*说明:
*
*位置:1
*
****************************************/
unsigned int ADCSelChannel(unsigned char Channel)
{
unsigned int ConvertValue;
unsigned char i, Chan;
unsigned char ConvertValueL, ConvertValueH;
unsigned char delay;
ConvertValueL = ConvertValueH = 0;//初始化转换结果
delay = 0;
if (EOC)
{
Clock = 0;
ChipSelect = 1;
Wait2us;
ChipSelect = 0;
Wait2us;
Channel = Channel << 4;
for (i = 0; i < 4; i ++) //输入需要转换的通道的编码
{
Chan = Channel;
Chan = Chan >> 7;
DataIn = (bit)Chan;
Wait2us;
Clock = 1;
Clock = 0;
Channel = Channel << 1;
}
for (i = 0; i < 6;i ++) //输入转换时钟
{
Clock = 1;
Clock = 0;
}
ChipSelect = 1;
//开始检测转换结束标志,或者转换超时出错
while ((!EOC) && (delay < 10))
{
Wait10us;
delay ++;
}
if (delay == 10)
{
return (0xFFFF);//转换超时,返回错误代码
}
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);//返回转换结果
}
}
}
/************ADCSelChannel函数结束**********/