AD0832是8位逐次逼近模數(shù)轉(zhuǎn)換器,可支持兩個(gè)單端輸入通道和一個(gè)差分輸入通道。是8位逐次逼近模數(shù)轉(zhuǎn)換器,可支持兩個(gè)單端輸入通道和一個(gè)差分輸入通道。
當(dāng)ADC0832未工作時(shí)其CS輸入端應(yīng)為高電平,此時(shí)芯片禁用,當(dāng)要進(jìn)行A/D轉(zhuǎn)換時(shí),
須先將CS使能端置于低電平并且保持低電平直到轉(zhuǎn)換完全結(jié)束。此時(shí)芯片開始轉(zhuǎn)換工作,同時(shí)由處理器向芯片時(shí)鐘輸入端CLK輸入時(shí)鐘脈沖,DO/DI端則使用DI端輸入通道功能選擇的數(shù)據(jù)信號。在第1個(gè)時(shí)鐘脈沖的下沉之前DI端必須是高電平,表示啟始信號。在第2、3個(gè)脈沖下沉之前DI端應(yīng)輸入2位數(shù)據(jù)用于選擇通道功能,
當(dāng)此2位數(shù)據(jù)為“1”、“0”時(shí),只對CH0進(jìn)行單通道轉(zhuǎn)換。
當(dāng)2位數(shù)據(jù)為“1”、“1”時(shí),只對CH1進(jìn)行單通道轉(zhuǎn)換。
當(dāng)2位數(shù)據(jù)為“0”、“0”時(shí),將CH0作為正輸入端IN+,CH1作為負(fù)輸入端IN-進(jìn)行輸入。
當(dāng)2位數(shù)據(jù)為“0”、“1”時(shí),將CH0作為負(fù)輸入端IN-,CH1作為正輸入端IN+進(jìn)行輸入。
在完成輸入啟動(dòng)位、通道選擇之后,就可以開始讀出數(shù)據(jù),轉(zhuǎn)換得到的數(shù)據(jù)會(huì)被送出二次,一次高位在前傳送,一次低位在前傳送,連續(xù)送出。在程序讀取二個(gè)數(shù)據(jù)后,我們可以加上檢驗(yàn)來看看數(shù)據(jù)是否被正確讀取。
由于ADC0832是8位分辨率,返回的數(shù)值在0~255之間,對應(yīng)模擬數(shù)值為0~5V,因此每一檔對應(yīng)的電壓值約為0.0196V。大家可以在通道輸入端引入模擬信號(0~5V)進(jìn)行測試,比如可以在通道腳和地之間接入電池來測試電池電壓值。為使兩位數(shù)碼管顯示電壓值大小,在寫程序是可將對應(yīng)比值改位0.196,同理,如果想要顯示精度更高,可用三位或四位顯示,那么響應(yīng)的改一下比值為1.96或19.6,當(dāng)然,需要注意你所得數(shù)據(jù)的大小是否超出數(shù)據(jù)類型的大小。
時(shí)序圖
基于ADC0834的AD轉(zhuǎn)換
/*----------------------------------------------------------
功能介紹:
-----------------------------------------------------------*/
#include 《reg52.h》 #include 《stdio.h》
#include 《math.h》
#define uchar unsigned char
#define uint unsigned int
#define ucode unsigned code
/*--------------------------------------------
包含函數(shù)
---------------------------------------------*/
void DISP(void); //數(shù)碼管顯示函數(shù)
void key(void);//矩陣鍵盤函數(shù)
void _nop_(void);
void ad_buf(); //模擬量轉(zhuǎn)化為數(shù)字函數(shù)
uchar ADC0834(uchar tds); //AD轉(zhuǎn)換函數(shù)
/*-------------------------------------------
參數(shù)定義
-------------------------------------------*/
uchar bdata ekey1; //鍵前沿提取
uchar bdata ekey2;
uchar data key2;
uchar data key1;
uchar bdata lastkey1;
uchar bdata lastkey2;
uchar disp_a;//數(shù)碼管顯示指針
sbit cs0=P2^0;
sbit cs1=P2^1;
sbit cs2=P2^2;
sbit cs3=P2^3;
sbit cs4=P2^4;
sbit esw0=ekey2^6; //獨(dú)立按鍵鍵沿標(biāo)志
sbit esw1=ekey2^7; //
sbit ek0=ekey1^0; //矩陣按鍵鍵沿標(biāo)志
sbit ek1=ekey1^1;
sbit ek2=ekey1^2;
sbit ek3=ekey1^3;
sbit ek4=ekey1^4;
sbit ek5=ekey1^5;
sbit ek6=ekey1^6;
sbit ek7=ekey1^7;
sbit ek8=ekey2^0;
sbit ek9=ekey2^1;
sbit row0=P2^5;
sbit row1=P2^6;
sbit row2=P2^7;
sbit sw0=P1^6;
sbit sw1=P1^7;
sbit do_0834=P1^0;//0834 DI
sbit cs_0834=P1^1; //0834 片選
sbit di_0834=P1^2; //0834 DI
sbit clk_0834=P1^3; // 0834 時(shí)鐘
sbit dian=P0^7; // 小數(shù)點(diǎn)
uchar buf0; // 顯示緩沖單元個(gè)位
uchar buf1; // 十
uchar buf2; // 百
uchar buf3; //千
uchar buf4;
uchar code led[]={0x0C0,0x0F9,0x0A4,0x0B0,0x99,0x92,0x82,0x0F8,0x80,0x90};
uchar msta=0;
uchar tds;//通道選擇
uint adbuf;
bit t1s;//一秒標(biāo)志位
bit light; //小數(shù)點(diǎn)亮標(biāo)志位
uchar ktime;
/*-------------------------------------------
主函數(shù)
-------------------------------------------*/
void main()
{ uchar f1s=0;
uchar i;
TMOD=0x01;
TH0=0xec;
TL0=0x78;
TR0=1;
while(1)
{
while(!TF0); //5ms?
TF0=0;
TH0=0x0ec;
TL0=0x78;
++f1s;
if(f1s==200)
{t1s=1;
f1s=0;}
key();
DISP();
switch(msta)
{ //待機(jī)模塊 顯示“0000”
case 0:if(esw0){msta=1;tds=0xd0;}//SW1按下跳到模塊1 選擇單端模式 AD一次
if(esw1){msta=1;tds=0x80;}//SW2按下跳到模塊1 選擇差分模式 AD一次
if(ek0){msta=2;tds=0xd0;}// ek0按下跳到模塊2 選擇單端模式 十次AD取平均
if(ek1){msta=2;tds=0x80;}//ek1按下跳到 模塊2 選擇差分模式 十次AD取平均
if(ek2){msta=3;tds=0xd0;}// ek2按下跳到模塊3 選擇單端模式 一秒AD一次
if(ek3){msta=3;tds=0x80;}// ek3按下跳到模塊3 選擇差分模式 一秒AD一次
buf4=0,buf3=0,buf2=0,buf1=0;
buf0=0;
break;
case 1:if(esw0){msta=1;tds=0xd0;} //只AD一次
if(esw1){msta=1;tds=0x80;}
light=1;//小數(shù)點(diǎn)亮
adbuf=ADC0834(tds);
ad_buf(); break;
case 2:if(ek0){msta=2;tds=0xd0;} //AD十次取平均
if(ek1){msta=2;tds=0x80;}
light=1;
for(i=0;i《10;i++)
{adbuf=adbuf+ADC0834(tds);}
adbuf=adbuf/11;
ad_buf();
break;
case 3:if(ek2){msta=3;tds=0xd0;} //一秒AD一次
if(ek3){msta=3;tds=0x80;}
light=1;
if(t1s)
{t1s=0;
adbuf=ADC0834(tds);
ad_buf();}
break;
default:break;
}
}
}
/*-----------------------------------------------
數(shù)碼管顯示
------------------------------------------------*/
void DISP()
{ P0=0xff;
P2=0xff;
switch(disp_a)
{ case 0: cs0=0; // 點(diǎn)亮第0位數(shù)碼管
P0=led[buf0];
disp_a=1;
break;
case 1: cs1=0; // 點(diǎn)亮第1位數(shù)碼管
P0=led[buf1];
disp_a=2;
break;
case 2: cs2=0; // 點(diǎn)亮第2位數(shù)碼管
P0=led[buf2];
disp_a=3;
break;
case 3: cs3=0; // 點(diǎn)亮第3位數(shù)碼管
P0=led[buf3];
if(light)
dian=0; //小數(shù)點(diǎn)亮
disp_a=4;
break;
case 4: cs4=0; // 點(diǎn)亮流水燈
P0=buf4;
disp_a=0;
break;
default:break;
}
}
/*-------------------------------------------------------
矩陣鍵盤及獨(dú)立按鍵子程序
------------------------------------------------------------*/
void key()
{uchar kbuf;
P2=0x0ff;//關(guān)數(shù)碼管
row0=0; //掃描K0~3
_nop_(); //延時(shí)
_nop_();
kbuf=P2;
kbuf=(kbuf&0x0f)^0x0f;
//K0~3鍵位保留 求反轉(zhuǎn)正邏輯
lastkey1=kbuf; // 新的鍵狀態(tài)暫存R6
P2=0x0ff;
row1=0; //掃描K4~7
_nop_();
_nop_();
kbuf=P2;
kbuf=(kbuf&0x0f)^0x0f;
lastkey1=(kbuf《《4)+lastkey1;
P2=0x0ff;
row2=0;//掃描K8~K11
_nop_();
_nop_();
kbuf=P2;
kbuf=(kbuf&0x0f)^0x0f;
lastkey2=kbuf;
P2=0x0ff;
P1=P1|0x0c0;
kbuf=P1;
kbuf=(kbuf&0x0c0)^0x0c0;
lastkey2=kbuf+lastkey2;
if((lastkey2!=key2)||(lastkey1!=key1))//鍵狀態(tài)變化則轉(zhuǎn)移
{if(ktime--!=0)
{lastkey1=key1;
lastkey2=key2;}
}
else
ktime=0x04;
ekey1=(key1^lastkey1)&lastkey1;
key1=lastkey1;
ekey2=(key2^lastkey2)&lastkey2;
key2=lastkey2; }
/************************************************************
AD轉(zhuǎn)換
************************************************************/
uchar ADC0834(uchar tds)
{
uchar i,adbuf=0; cs_0834=0; for(i=0;i《5;i++)
{
clk_0834=0;
di_0834=0;
if(tds&0x80)
di_0834=1;
clk_0834=1;
tds《《=1;
}
do_0834=1;
for(i=0;i《8;i++)
{
clk_0834=0;
clk_0834=1;
adbuf=(adbuf《《1)|do_0834;
}
cs_0834=1;
return(adbuf);
}
/*****************************************************************
模擬量轉(zhuǎn)化為數(shù)字并送顯
******************************************************************/
void ad_buf()
{
buf4=adbuf%51*10%51*10%51*10/51;//
流水燈
buf3=adbuf/51; //個(gè)位
buf2=adbuf%51*10/51;//十分位
buf1=adbuf%51*10%51*10/51;//百分位
buf0=adbuf%51*10%51*10%51*10/51;// 千分位
}
評論