標(biāo)題: 基于51單片機電壓表的電路圖與程序 [打印本頁]

作者: waiee    時間: 2021-9-22 13:13
標(biāo)題: 基于51單片機電壓表的電路圖與程序


#include <AT89X52.H>  
unsigned char code dispbitcode[]={0xfe,0xfd,0xfb,0xf7, 0xef,0xdf,0xbf,0x7f};  //放數(shù)碼管位選的值
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f,0x00};//放數(shù)碼管顯示的數(shù)字
unsigned char dispbuf[8]={10,10,10,10,10,0,0,0};  //存放ad采集過來的值(只有最后3位有用)
unsigned char dispcount;
unsigned char getdata;
unsigned int temp;
long int  i;
unsigned int R1;

sbit ST=P3^0; //是否選中該芯片
sbit OE=P3^1; //所存轉(zhuǎn)換的值
sbit EOC=P3^2; //當(dāng)EAC數(shù)據(jù)有效時,將轉(zhuǎn)換數(shù)據(jù)送出
sbit CLK=P3^3; //給adc0808提供時鐘

void main(void)
{
  ST=0;
  OE=0;
  ET0=1; //啟用定時器0
  ET1=1;   // 啟用定時器1
  EA=1;    //開總中斷
  TMOD=0x12;//設(shè)置定時器0和1的工作方式
  TH0=216;
  TL0=216; //設(shè)置定時器0的初值為216(為adc0808提供時鐘)
  TH1=(65536-5000)/256;
  TL1=(65536-5000)%256;//設(shè)置定時器1的初值為5ms
  TR1=1;  //開定時器1
  TR0=1;  //開定時器0
  ST=1;
  ST=0;
  while(1)
    {
      if(EOC==1) //判斷裝換是否結(jié)束
        {
          OE=1;  
          getdata=P1;  //如果結(jié)束,給OE一個跳變,讀出數(shù)據(jù)存入getdata
          OE=0;
                    i=getdata*196;         //256/getdata=5V/xV  >>>x=(getdata*5/256)V
                        dispbuf[5]=i/10000; //將數(shù)據(jù)存入數(shù)組中以待顯示。 (個位)
                        i=i%10000;
                        dispbuf[6]=i/1000;        //小數(shù)點第一位
                        i=i%1000;
                        dispbuf[7]=i/100;   //小數(shù)點第二位      
          ST=1;
          ST=0;
        }
    }
}

void t0(void) interrupt 1 using 0           //定時器0  中斷服務(wù)
{
  CLK=~CLK;  //給adc0808提供時鐘
}

void t1(void) interrupt 3 using 0          //定時器1  中斷服務(wù)
{
  TH1=(65536-5050)/256;
  TL1=(65536-5050)%256;
  P2=0xff;
  P0=dispcode[dispbuf[dispcount]];          //段選
  P2=dispbitcode[dispcount];                 //位選
  if(dispcount==5)         // 顯示小數(shù)點
    {
      P0=P0 | 0x80;
    }
  dispcount++;          //讓位選循環(huán)起來
  if(dispcount==8)
    {
      dispcount=0;
    }  
}







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1