單片機A/D模數(shù)轉(zhuǎn)換
先上AD574功能表:
硬件電路圖:
程序流程圖:
c程序:
/************************************************************************************************A/D實驗程序選擇的晶振是3MHz,選用地址Q0,Q1,用定時器1在1s后重新啟動AD轉(zhuǎn)換(important)程序是copyseataes的,感謝ing,我只是把注釋補全,自己能更快的理解罷了************************************************************************************************/#include#include/**************************************“A0,A1選擇”七者選其一*************************************/#defineAD_STARTPBYTE[0xF9]//Q1Q211111001啟動AD轉(zhuǎn)換的地址#defineAD_HIGHPBYTE[0xFD]//11111101讀AD轉(zhuǎn)換高8位數(shù)據(jù)的地址#defineAD_LOWPBYTE[0xFF]//讀AD轉(zhuǎn)換低4位數(shù)據(jù)的地址/*#defineAD_STARTPBYTE[0xFC]//Q0Q111111100#defineAD_HIGHPBYTE[0xFE]//11111110#defineAD_LOWPBYTE[0xFF]#defineAD_STARTPBYTE[0xF3]//Q2Q311110011#defineAD_HIGHPBYTE[0xFB]//11111011#defineAD_LOWPBYTE[0xFF]#defineAD_STARTPBYTE[0xE7]//Q3Q411100111#defineAD_HIGHPBYTE[0xF7]//11110111#defineAD_LOWPBYTE[0xFF]#defineAD_STARTPBYTE[0xCF]//Q4Q511001111#defineAD_HIGHPBYTE[0xEF]//11101111#defineAD_LOWPBYTE[0xFF]#defineAD_STARTPBYTE[0x9F]//Q5Q610011111#defineAD_HIGHPBYTE[0xDF]//11011111#defineAD_LOWPBYTE[0xFF]#defineAD_STARTPBYTE[0x3F]//Q6Q700111111#defineAD_HIGHPBYTE[0xBF]//10111111#defineAD_LOWPBYTE[0xFF]*//************************************************************************************************/unsignedcharcodetab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,};//共陽數(shù)碼管碼表unsignedcharcodetab1[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,};//共陰數(shù)碼管碼表unsignedchardatabyhigh,bylow;sbitP2_0=P2^0;//數(shù)碼管的位選信號sbitP2_1=P2^1;//數(shù)碼管的位選信號sbitP2_2=P2^2;//數(shù)碼管的位選信號voiddelay(void);voiddisplay(void);//主程序voidmain(){/*************************************關(guān)閉數(shù)碼顯示二者選其一*************************************///P1=0xFF;//P2=0x00;//關(guān)閉共陽數(shù)碼管P1=0x00;//關(guān)閉共陰數(shù)碼管段選P2=0xFF;//關(guān)閉共陰數(shù)碼管位選/************************************************************************************************//***************************************外部中斷二者選其一***************************************///IT0=1;//EX0=1;//外部中斷0IT1=1;//外部中斷采用下降沿觸發(fā)EX1=1;//允許外部中斷中斷/************************************************************************************************/TH0=(65536-50000)/256;//賦初值50*4=200msTL0=(65536-50000)%256;TMOD=0x01;//選擇定時器方式1ET0=1;//允許定時器0中斷TR0=1;//啟動定時器0EA=1;//中斷總允許AD_START=0;//啟動AD轉(zhuǎn)換for(;;){display();}}/***************************************外部中斷二者選其一***************************************//*voidInt0(void)interrupt0//外部中斷0{byhigh=AD_HIGH;bylow=AD_LOW;}*///外部中斷服務(wù)子程序,用于讀取AD的高8位和低四位voidInt1(void)interrupt2{byhigh=AD_HIGH;//讀AD轉(zhuǎn)換的高8位bylow=AD_LOW;//讀AD轉(zhuǎn)換的低4位}/************************************************************************************************///定時器中斷1服務(wù)子程序上voidTimer0(void)interrupt1{staticunsignedchardatabycounter=0;//將byCounter設(shè)置為靜態(tài)變量TL0=(65536-50000)%256;TH0=(65536-50000)/256;bycounter++;if(bycounter==5){bycounter=0;AD_START=0;//啟動AD轉(zhuǎn)換}}//數(shù)碼管顯示程序voiddisplay(void){/************************************************************************************************/P2_0=1;P1=tab[byhigh>>4];//顯示最高4位delay();P2_0=0;P2_1=1;P1=tab[byhigh&0x0F];//顯示中間4位delay();P2_1=0;P2_2=1;P1=tab[bylow>>4];//顯示最低4位,怎么要左移4為呢?(高)xxxx-xxxx-xxxx-****(低)星號表示沒用到的delay();P2_2=0;//共陽數(shù)碼管顯示/************************************************************************************************/P2_0=0;P1=tab1[byhigh>>4];delay();P2_0=1;P2_1=0;P1=tab1[byhigh&0x0F];delay();P2_2=0;P2_1=1;P1=tab1[bylow>>4];delay();P2_2=1;//共陰數(shù)碼管顯示/************************************************************************************************/}//延時子程序1msvoiddelay(void){unsignedchara,b,c;for(c=1;c>0;c--)for(b=142;b>0;b--)for(a=2;a>0;a--);}