新聞中心

        0832ADC的51測(cè)試程序

        作者: 時(shí)間:2016-11-13 來(lái)源:網(wǎng)絡(luò) 收藏
        #include"reg52.h"

        #include
        #include"intrins.h"

        本文引用地址:http://www.104case.com/article/201611/316339.htm

        //==========================================================
        sbit CS=P3^0; //使能片選
        sbit CLK=P3^1; //時(shí)鐘
        sbit Do=P3^2; //數(shù)據(jù)輸出
        sbit Di=P3^2; //數(shù)據(jù)輸入
        sbit Do2=P3^3; //數(shù)據(jù)輸出
        sbit Di2=P3^3; //數(shù)據(jù)輸入
        sbit Do3=P3^4; //數(shù)據(jù)輸出
        sbit Di3=P3^4; //數(shù)據(jù)輸入
        sbit Do4=P3^5; //數(shù)據(jù)輸出
        sbit Di4=P3^5; //數(shù)據(jù)輸入
        sbit Do5=P3^6; //數(shù)據(jù)輸出
        sbit Di5=P3^6; //數(shù)據(jù)輸入

        unsigned char data_f=0,data_c=0,data_f2=0,data_c2=0,data_f3=0,data_c3=0,data_f4=0,data_c4=0,data_f5=0,data_c5=0; //data_f正確順序 data_c逆序

        unsigned char last[5]; //記錄上一時(shí)刻各AD轉(zhuǎn)換得到的值
        unsigned char current[5]; //記錄此次AD轉(zhuǎn)換得到的值

        int left = 0; //記錄0.1秒內(nèi)電機(jī)向左轉(zhuǎn)動(dòng)的步數(shù)
        int right = 0;//記錄0.1秒內(nèi)電機(jī)向右轉(zhuǎn)動(dòng)的步數(shù)

        sbit RS = P2^4;//Pin4
        sbit RW = P2^5; //Pin5
        sbit E = P2^6;//Pin6

        #define Data P0 //數(shù)據(jù)端口

        char data TimeNum[]=" ";
        char data Test1[]=" ";


        /******************************************************************/
        /* 函數(shù)聲明 */
        /******************************************************************//***************************************************************/
        void DelayUs(unsigned char us)//delay us
        {
        unsigned char uscnt;
        uscnt=us>>1;/* Crystal frequency in 12MHz*/
        while(--uscnt);
        }
        /******************************************************************/
        void DelayMs(unsigned int ms)//delay Ms
        {
        while(--ms)
        {
        DelayUs(250);
        DelayUs(250);
        DelayUs(250);
        DelayUs(250);
        }
        }
        void WriteCommand(unsigned char c)
        {
        DelayMs(5);//short delay before operation
        E=0;
        RS=0;
        RW=0;
        _nop_();
        E=1;
        Data=c;
        E=0;
        }
        /****************************************************************/
        void WriteData(unsigned char c)
        {
        DelayMs(5); //short delay before operation
        E=0;
        RS=1;
        RW=0;
        _nop_();
        E=1;
        Data=c;
        E=0;
        RS=0;
        }
        /*********************************************************************/
        void ShowChar(unsigned char pos,unsigned char c)
        {
        unsigned char p;
        if (pos>=0x10)
        p=pos+0xb0; //是第二行則命令代碼高4位為0xc
        else
        p=pos+0x80; //是第二行則命令代碼高4位為0x8
        WriteCommand (p);//write command
        WriteData (c); //write data
        }
        /*************************************************************************/
        void ShowString (unsigned char line,char *ptr)
        {
        unsigned char l,i;
        l=line<<4;
        for (i=0;i<16;i++)
        ShowChar (l++,*(ptr+i));//循環(huán)顯示16個(gè)字符
        }
        /*********************************************************************/
        void InitLcd()
        {
        DelayMs(15);
        WriteCommand(0x38); //display mode
        WriteCommand(0x38); //display mode
        WriteCommand(0x38); //display mode
        WriteCommand(0x06); //顯示光標(biāo)移動(dòng)位置
        WriteCommand(0x0c); //顯示開及光標(biāo)設(shè)置
        WriteCommand(0x01); //顯示清屏

        }


        //////////////////////////////////////////////////////////////////////
        //ADC轉(zhuǎn)換函數(shù)========================================================
        void ADconv()
        {
        unsigned char i;


        //時(shí)能前初始化====================================================
        Di=1;
        Di2=1;
        Di3=1;
        Di4=1;
        Di5=1;


        CS=1;
        _nop_();
        CS=0;
        Di=1;
        Di2=1;
        Di3=1;
        Di4=1;
        Di5=1;
        //=================================================================

        //第一個(gè)下降沿===================================================
        CLK=1;
        _nop_();
        _nop_();
        CLK=0;
        //=================================================================

        //確定通道方式,第二個(gè)下降沿===================================
        _nop_();
        _nop_();
        CLK=1;
        Di=1;
        Di2=1;
        Di3=1;
        Di4=1;
        Di5=1;
        _nop_();
        _nop_();
        CLK=0;
        //=================================================================

        //第三個(gè)下降沿===================================================
        _nop_();
        _nop_();
        CLK=1;
        Di=0;
        Di2=1;
        Di3=1;
        Di4=1;
        Di5=1;
        _nop_();
        _nop_();
        CLK=0; //AD初始化完成
        //=================================================================

        //讀取數(shù)據(jù)========================================================
        Di=1; //IO變?yōu)檩斎?br /> Di2=1;
        Di3=1;
        Di4=1;
        Di5=1;
        CLK=1;
        _nop_();
        _nop_();
        CLK=0;

        CLK=1;
        CLK=1;
        _nop_();
        _nop_();
        CLK=0;

        CLK=1;
        //得到一個(gè)正常排序的8位數(shù)據(jù)
        for(i=8;i>0;i--)
        {
        data_f<<=1;
        data_f2<<=1;
        data_f3<<=1;
        data_f4<<=1;
        data_f5<<=1;

        data_f|=Do;
        data_f2|=Do2;
        data_f3|=Do3;
        data_f4|=Do4;
        data_f5|=Do5;

        CLK=1;
        _nop_();
        _nop_();
        CLK=0;
        _nop_();
        }
        //==============================
        //得到一個(gè)反序排列的8位數(shù)據(jù)
        for(i=8;i>0;i--)
        {
        data_c<<=1;
        data_c2<<=1;
        data_c3<<=1;
        data_c4<<=1;
        data_c5<<=1;

        data_c|=Do;
        data_c2|=Do2;
        data_c3|=Do3;
        data_c4|=Do4;
        data_c5|=Do5;

        _nop_();
        CLK=1;
        _nop_();
        _nop_();
        CLK=0;
        _nop_();
        }
        //==============================
        CLK=0;
        _nop_();
        _nop_();
        CLK=1;
        _nop_();
        _nop_();
        CLK=0;
        _nop_();
        _nop_();
        CLK=1;
        _nop_();
        CS=1;
        _nop_();
        _nop_();


        }
        //===================================================================

        //顯示轉(zhuǎn)換===========================================================
        char huanhuan(unsigned char benben)
        {
        switch(benben)
        {
        case 0:
        return 0;
        break;

        case 1:
        return 1;
        break;

        case 2:
        return 2;
        break;

        case 3:
        return 3;
        break;

        case 4:
        return 4;
        break;

        case 5:
        return 5;
        break;

        case 6:
        return 6;
        break;

        case 7:
        return 7;
        break;

        case 8:
        return 8;
        break;

        case 9:
        return 9;
        break;

        default:
        break;
        }
        }
        //===================================================================

        void main()
        {
        unsigned xianshi=0;
        InitLcd();
        DelayMs(15);
        sprintf(Test1," wo shi tiancai ");//the first line
        ShowString(0,Test1);
        sprintf(TimeNum,"AD value is ");//the first line
        ShowString(1,TimeNum);
        while(1)
        {
        DelayMs(800);
        xianshi=data_f;
        ADconv();
        TimeNum[12]=huanhuan(xianshi/100);
        TimeNum[13]=huanhuan(xianshi/10%10);
        TimeNum[14]=huanhuan(xianshi%10);
        ShowString(1,TimeNum);
        }
        }



        關(guān)鍵詞: 0832ADC51測(cè)試程

        評(píng)論


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

        關(guān)閉
        主站蜘蛛池模板: 宁明县| 玉环县| 焉耆| 仪征市| 黑河市| 安宁市| 玉屏| 建湖县| 宁阳县| 乃东县| 腾冲县| 海原县| 肃南| 和田县| 新巴尔虎右旗| 阳江市| 桂林市| 灵宝市| 绥中县| 南和县| 武鸣县| 沭阳县| 正蓝旗| 宜州市| 信阳市| 资讯 | 淳化县| 颍上县| 南京市| 大埔区| 荔浦县| 容城县| 永川市| 油尖旺区| 韶山市| 轮台县| 宁晋县| 永和县| 灵丘县| 嘉定区| 苏尼特左旗|