//==========AD采样模块============//
//TLC1543 A/D用转换用
#define CLOCK P1_3
#define D_IN P1_4
#define D_OUT P1_5
#define _CS P1_6
//========================新版本=======================
uint read1543(uchar port) //从TLC1543读取采样值,形参port是采样的通道号
{
uint data ad;uint data i;
uchar data al=0,ah=0;
CLOCK=0;
_CS=0;
port<<=4;
for (i=0;i<4;i++) //把通道号打入1543
{
D_IN=(bit)(port&0x80);CLOCK=1;CLOCK=0;
port<<=1;
}
for (i=0;i<6;i++) //填充6个CLOCK
{
CLOCK=1;CLOCK=0;
}
_CS=1;_nop_();_nop_();_CS=0; //等待AD转换
for (i=0;i<2;i++) //取D9,D8
{
D_OUT=1;
CLOCK=1;
ah<<=1;
if (D_OUT) ah|=0x01;
CLOCK=0;
}
for (i=0;i<8;i++) //取D7--D0
{
D_OUT=1;
CLOCK=1;
al<<=1;
if (D_OUT) al|=0x01;
CLOCK=0;
}
_CS=1;
ad=(uint)ah;ad<<=8;ad|=al; //得到AD值
return (ad);
}
(备注:以上代码皆从网上搜集而来,如存在版权问题请马上通知我们,我们将马上删除。)