專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計實(shí)例 >> 瀏覽文章

ADC0832模數(shù)轉(zhuǎn)換51單片機(jī)程序與電路圖

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時間:2013年11月06日   【字體:
#include<AT89x51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
//******************adc0832****************************//
sbit CS=P2^3; //使能。
sbit CLK=P2^2;//時鐘
sbit DO=P2^1; // 數(shù)據(jù)輸出
sbit DI=P2^0;//數(shù)據(jù)輸入
char CC[]="11001001";
uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar temp;
uint vvv,i;
//通道的選擇:0x02就是單通道0;0x03就是單通道1;
//0x00就是雙通道ch0="+";   ch0="-"
//0x01就是雙通道ch0="-";   ch0="+"
//*****************************************************//
void delay(int tt)
{
while(tt--)
  {for(i=0;i<120;i++);}
}
void startADC()
{
CS=1;
_nop_();
_nop_();
CLK=0;
_nop_();
_nop_();
CS=0;
_nop_();
_nop_();
DI=1;
_nop_();
_nop_();
CLK=1;
_nop_();
_nop_();
DI=0;
_nop_();
_nop_();
CLK=0;
_nop_();
_nop_();
}
void choiceADC(uint CH)//CH為0選擇通道ch0,為1,選擇ch1進(jìn)行AD轉(zhuǎn)換
{
startADC();
if(CH==0)
{
DI=1;
_nop_();
CLK=1;//上升沿DI=1
_nop_();
CLK=0;//1個下降沿DI=1
_nop_();
DI=0;
_nop_();
CLK=1;
_nop_();
CLK=0;//第3個上升沿DI=0
_nop_();
}
else
{
CLK=0;
_nop_();
DI=1;
_nop_();
CLK=1;//上升沿DI=1
_nop_();
CLK=0;//1個下降沿DI=1
_nop_();
DI=1;
_nop_();
CLK=1;//第3個上升沿DI=0
_nop_();
}
/********通道選擇結(jié)束開始讀取轉(zhuǎn)換后的二進(jìn)制數(shù)****/
CLK=1;
_nop_();
CLK=0;//下降沿讀數(shù),一下進(jìn)行判斷和處理,共8次
for(i=0;i<8;i++)
      {
   DI=1;
   if(DO)
      {
   temp |=0x01;
   }
    else
      {
   temp &=0xfe;
   }
      CLK=0;
   _nop_();
   CLK=1;
   temp=temp<<1;
   }
   vvv=temp;
}
void display(uint dat)
{
P3=0x04;
P0=tab[dat/100];
delay(1);
P0=0xff;
P3=0x02;
P0=tab[dat%100/10];
delay(1);
P0=0xff;
P3=0x01;
P0=tab[dat%10];
delay(1);
P0=0xff;
}
void main()
{
   P3=0xFF;
   P0=0xff;
   while(1)
   {
      choiceADC(0);
   delay(1);
   display(vvv);
   }
}

 




 


                ADC0832封裝圖         


ADC0832與單片機(jī)連接圖



ADC0832時序圖
關(guān)閉窗口

相關(guān)文章