新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > ADC0831模數(shù)轉(zhuǎn)換程序

        ADC0831模數(shù)轉(zhuǎn)換程序

        作者: 時間:2012-11-10 來源:網(wǎng)絡(luò) 收藏

        /*
        程序效果:數(shù)碼管顯示AD轉(zhuǎn)換到的一字節(jié)數(shù)據(jù),以顯示
             調(diào)節(jié)相應(yīng)的電位器,顯示數(shù)據(jù)作相應(yīng)的變化
        程序開發(fā):http://www.51hei.com
        原理:0831是串行AD芯片,調(diào)節(jié)點位器就是調(diào)節(jié)了輸入0831的模擬電壓的大小,然后通過數(shù)碼管顯示
        運行環(huán)境:51hei單片機(jī)學(xué)習(xí)板
        */
        #includereg52.h>
        #includeintrins.h>
        #define uchar unsigned char
        #define uint unsigned int
        uchar code table[]={0x3f,0x06,0x5b,//數(shù)碼管顯示的數(shù)值
        0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
        sbit scl1=P1^3; //芯片的通信脈沖引腳
        sbit sda1=P1^4; //數(shù)據(jù)傳輸引腳
        sbit px1=P1^5; //片選引腳,即選擇和DS1302
        sbit cs1=P1^6; //片選引腳
        uchar tmpdata[]={0,0,0};
        uchar readad0831();//定義該函數(shù)為讀取的數(shù)據(jù)
        void display(uchar *lp,uchar lc); //顯示子函數(shù)
        void delay(); //延時子函數(shù)
        void main() // 主函數(shù)
        {
          uchar i=0,tmp;
          px1=0; //選擇該芯片
          while(1)
          {
            i++;
            if(i==255)   //這里為循環(huán)255個周期讀取一次0831,
            {      //因為CPU得運行速度很快,所以沒有必要每一次
              tmp=readad0831(); //讀取數(shù)據(jù),這樣可以充分利用CPU的資源
              i=0;
              tmpdata[0]=tmp/100;//分離百位
               tmp=tmp%100;
              tmpdata[1]=tmp/10; //分離十位
              tmpdata[2]=tmp%10;//分離個位
            }
          display(tmpdata,3); //輸出顯示
          }
        }
        void display(uchar *lp,uchar lc) //顯示子函數(shù)
        {
          uchar i; //定義局部變量
          P2=0; //端口P2為輸出口
          P1=P10xf8; //P1口的前三位用于控制那個數(shù)碼管亮,
                 //如果改為P1=0xf8,可能會出現(xiàn)數(shù)碼管
                //不停的顯示,難以分清顯示的數(shù)值
          for(i=0;ilc;i++)
          {
            P2=table[lp[i]]; //循環(huán)顯示
            delay(); //延時五個空指令
            if(i==7) //檢測是否顯示完8位,完成則直接退出
              //不讓P1口再加一,否則會影響P1^4口
              break;
            P2=0; //清零端口,準(zhǔn)備顯示下一位
            P1++; //點亮下一個數(shù)碼管
          }
        }
        void delay() //延時子函數(shù)
        {
          _nop_();_nop_();_nop_();_nop_();_nop_();
        }
        uchar readad0831() //本函數(shù)是按照ADC0831的協(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=1;
            if(sda1)
              tmp++;
            scl1=1;
            _nop_();
            _nop_();
            scl1=0;
            _nop_();
            _nop_();
          }
          cs1=1;
          return tmp;
        }
        如程序無法編譯,請去掉所有前導(dǎo)空白。



        評論


        相關(guān)推薦

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

        關(guān)閉
        主站蜘蛛池模板: 资阳市| 托克逊县| 阳西县| 正宁县| 凯里市| 理塘县| 乐安县| 河西区| 邻水| 临泉县| 富蕴县| 新化县| 华阴市| 河西区| 岚皋县| 青龙| 元江| 高尔夫| 新竹县| 玉树县| 甘孜| 正宁县| 昭苏县| 白水县| 惠水县| 德兴市| 洮南市| 灵石县| 苗栗市| 祁东县| 南汇区| 天水市| 荥经县| 辉南县| 萨嘎县| 包头市| 宁强县| 九台市| 内江市| 武安市| 闽侯县|