新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 數(shù)碼管顯示的ADC0831電壓表程序

        數(shù)碼管顯示的ADC0831電壓表程序

        作者: 時(shí)間:2012-11-10 來源:網(wǎng)絡(luò) 收藏
        /*
        程序效果:0.00-5.00U電壓,調(diào)節(jié)電位器,得到
             的2腳電壓值。
        注:測(cè)量時(shí)先把電位器調(diào)節(jié)到中間,也就是2.5U,但切記
          所測(cè)的引腳的電壓值不能超過5U,否則會(huì)燒壞
           芯片和單片機(jī),小心哦。
        程序版權(quán)所有:http://www.51hei.com,如無法編譯,請(qǐng)去掉所有前導(dǎo)空白。
        */
        #includereg52.h>
        #includeintrins.h>
        #define uchar unsigned char
        #define uint unsigned int
        uchar code table[]={
        0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,//共陰的數(shù)碼管的顯示值為0-9
        0x8f,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,//顯示值為:帶有小數(shù)點(diǎn)的0-9
        0x40,0x3e,0x00}; //分別顯示“-”,U(伏的單位),空表
        sbit scl1=P1^3;
        sbit sda1=P1^4;
        sbit px1=P1^5;
        sbit cs1=P1^6;
        uchar tmpdata[]={0,0,0,0};
        uchar readad0831();
        void display(uchar *lp,uchar lc);
        void delay();
        void main()
        {
          uint i=0,tmp; //這要定位為整型,防止數(shù)據(jù)溢出
          px1=0;
          while(1)
          {
            i++;
            if(i==255)
            {
               i=0;
              tmp=readad0831()*100; //這里乘以100,是保留兩位小數(shù)的意思
              tmp=tmp/51;
              tmpdata[0]=tmp/100; //得到百位數(shù),其實(shí)是原來的個(gè)位數(shù)
              tmpdata[0]+=10; //這里加上10,是因?yàn)閹?shù)點(diǎn)的數(shù)字在后面十位
              tmp=tmp%100;
               tmpdata[1]=tmp/10; //得到十位,即小數(shù)點(diǎn)的后一位
              tmpdata[2]=tmp%10; //得到各位,即小數(shù)點(diǎn)的第二位
              tmpdata[3]=21; //加上單位U,即伏
            }
            display(tmpdata,4); //調(diào)用顯示子函數(shù)
          }
        }
        void display(uchar *lp,uchar lc)
        {
          uchar i;
          P2=0;
          P1=P10xf8; //防止改變后五位數(shù)
          for(i=0;ilc;i++)
          {
            P2=table[lp[i]];
            delay();
            if(i==7)
              break;
            P2=0;
            P1++;
          }
        }
        uchar readad0831()//根據(jù)協(xié)議編寫的語句
        {
          uchar i=0,tmp=0;
          sda1=1;
          cs1=0;
          _nop_();
          _nop_();
          scl1=0;
          _nop_();
          _nop_();
          scl1=1;
          _nop_();
          _nop_();
          scl1=0;
          _nop_();
          _nop_();
          scl1=1;
          _nop_();
          _nop_();
          scl1=0;
          _nop_();
          _nop_();
          for(i=0;i8;i++)
          {
            tmp=_crol_(tmp,1);
            if(sda1)
              tmp++;
            scl1=1;
            _nop_();
            _nop_();
            scl1=0;
            _nop_();
             _nop_();
          }
          cs1=1;
          return tmp;
        }
        void delay() //延時(shí)子函數(shù)
        {
           _nop_();_nop_();_nop_();_nop_();_nop_();
        }


        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 房山区| 巴楚县| 金塔县| 罗田县| 长兴县| 马关县| 姜堰市| 威远县| 天祝| 广饶县| 田阳县| 施秉县| 吴堡县| 琼中| 教育| 兴隆县| 富川| 云安县| 湖南省| 高密市| 衡东县| 盘锦市| 延安市| 中山市| 昌黎县| 绿春县| 邳州市| 阿克陶县| 巴塘县| 太仆寺旗| 康定县| 松原市| 汉寿县| 库尔勒市| 泗洪县| 东山县| 泰安市| 甘泉县| 舟曲县| 青浦区| 满洲里市|