A/D TLC1543 應(yīng)用
源程序:
uint Read_AD(uchar AD_Channel) //從TLC1543讀取采樣值,形參AD_Channel是采樣的通道號
{
uint i;
uint AD_data; //定義AD數(shù)據(jù)儲存地址
uchar AD_data_l=0,AD_data_h=0; //定義AD數(shù)據(jù)儲存地址高低位
// Page13 of TLC1543_datasheet
AD_CLOCK=0;
AD_CS=0;
AD_Channel<<=4;
for (i=0;i<4;i++) //把通道號打入1543
{
AD_D_IN=(bit)(AD_Channel&0x80);
AD_CLOCK=1;
AD_CLOCK=0;
AD_Channel<<=1;
}
for (i=0;i<6;i++) //填充6個(gè)AD_CLOCK
{
AD_CLOCK=1;
AD_CLOCK=0;
}
//AD 轉(zhuǎn)換有6種模式,具體時(shí)序圖見數(shù)據(jù)手冊,這里舉例2種轉(zhuǎn)換模式
//1)Fast Modes=>Mode1: Hight between conversion cycles ; 10 I/O CLOCKS; MSB AT DATA OUT CS falling edge
/*AD_CS=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();
AD_CS=0; // AD轉(zhuǎn)換結(jié)束
*/
// 2)Fast Modes=>Mode2: Low conversion cycles ; 10 I/O CLOCKS; MSB AT DATA EOC rising edge
while(!AD_EOC); //等待AD轉(zhuǎn)換
_nop_();_nop_();_nop_(); //延時(shí)
for (i=0;i<2;i++) //讀取高2位:D9,D8
{
AD_D_OUT=1;//讀取數(shù)據(jù)以前,數(shù)據(jù)口先置高
AD_CLOCK=1;
AD_data_h<<=1;
//數(shù)據(jù)高低判斷
if (AD_D_OUT==1) //(1)容易理解的寫法
{AD_data_h=AD_data_h+0x01;}
else
{AD_data_h=AD_data_h+0x00;}
//if (AD_D_OUT) AD_data_l +=0x01; //(1)精簡寫法
AD_CLOCK=0;
}
for (i=0;i<8;i++) //讀取低8位:D7--D0
{
AD_D_OUT=1; //讀取數(shù)據(jù)以前,數(shù)據(jù)口先置高
AD_CLOCK=1;
AD_data_l <<= 1;
//數(shù)據(jù)高低判斷
if (AD_D_OUT==1) //(1)容易理解的寫法
{AD_data_l=AD_data_l+0x01;}
else
{AD_data_l=AD_data_l+0x00;}
//if (AD_D_OUT) AD_data_l +=0x01; //(2)精簡寫法
AD_CLOCK=0;
}
AD_CS=1; //不使能AD
//AD轉(zhuǎn)換結(jié)果計(jì)算公式:AD_data= AD_data_h*256+AD_data_l
AD_data = (uint)AD_data_h;
AD_data <<= 8;
AD_data=AD_data+(uint)AD_data_l; //得到AD值
return(AD_data);
}
評論