新聞中心

        EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)實(shí)現(xiàn)光源的跟蹤

        51單片機(jī)實(shí)現(xiàn)光源的跟蹤

        作者: 時(shí)間:2016-11-27 來(lái)源:網(wǎng)絡(luò) 收藏
        AD采集兩路電壓,并比較,控制電機(jī)轉(zhuǎn)動(dòng)方向,并將數(shù)據(jù)傳至上位機(jī)
        西安理工大學(xué)--自動(dòng)化與信息工程學(xué)院
        鄒藝良 編寫(2014.08.25)
        郵箱:262276047@qq.com
        //*******************************
        //西安理工大學(xué)--自動(dòng)化與信息工程學(xué)院--鄒藝良
        //*******************************
        #include
        #include
        #include
        void ADC_init();
        int get_ADC_result(char ch);
        unsigned int show_ADC_result_ch0();
        unsigned int show_ADC_result_ch1();
        void Send(unsigned char dat);
        void uart_init();
        void delay(unsigned int a);
        void delay_10ms();
        sbit P2_0=P2^0;
        sbit P2_1=P2^1;
        sbit P1_3=P1^3;
        void positive_turn(unsigned int count); //順時(shí)針
        void negative_turn(unsigned int count); //逆時(shí)針
        void stop_turn();
        void main()
        {
        unsigned int ch0,ch1;
        int value,chazhi,z1,z2,z3,z4,gap;
        gap=30;
        while(1)
        {
        uart_init();
        ADC_init();
        //顯示左右兩傳感器電壓
        ch0=show_ADC_result_ch0();//通道ch0電壓
        Send(0x20); //四個(gè)空格符
        Send(0x20);
        Send(0x20);
        Send(0x20);
        ch1=show_ADC_result_ch1();//通道ch1電壓
        Send(0x20); //四個(gè)空格符
        Send(0x20);
        Send(0x20);
        Send(0x20);
        //顯示差值電壓
        value=ch0-ch1;
        if(ch0>ch1)
        {chazhi=ch0-ch1;}
        else
        {chazhi=ch1-ch0;}
        z1=chazhi/1000;
        z2=chazhi00/100;
        z3=chazhi000/10;
        z4=chazhi000;
        Send(0x43);//C
        Send(0x3D);//=
        if(ch0
        {Send(0x2D);}
        else
        {Send(0x2B);}
        Send(z1+48);
        Send(0x2E);//.
        Send(z2+48);
        Send(z3+48);
        Send(z4+48);
        if((ch0+gap)
        {Send(0x52);}
        if(ch0>(ch1+gap))
        {Send(0x4C);}
        if(-gap<(ch0-ch1) || (ch0-ch1)
        {Send(0x20);}
        //Send(0xD); //回車
        //Send(0xA); //換行
        //光源跟蹤
        if(-gap
        stop_turn();
        else if(value<-gap)
        negative_turn(10);
        else if(value>gap)
        positive_turn(10);
        //for(p=0;p<50;p++) //延時(shí)1s刷新一次
        //{delay_10ms();}
        }
        }
        void ADC_init()
        {
        P1ASF=0x03;//設(shè)置P1.0,P1.1為AD轉(zhuǎn)換口
        ADC_CONTR=0x80;//打開(kāi)AD電源、選擇轉(zhuǎn)換速度、
        ADC_RES=0;
        ADC_RESL=0;
        AUXR1&=0xfb; //設(shè)置AD轉(zhuǎn)換結(jié)果存放方式,此設(shè)為10位
        delay(5);
        }
        int get_ADC_result(char ch)
        {
        unsigned int result;
        ADC_CONTR=0x88|ch;//開(kāi)啟AD,選擇通道
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        while(!(ADC_CONTR & 0x10));//等待轉(zhuǎn)換完成,F(xiàn)LAG標(biāo)志位置位
        ADC_CONTR&=0x67; //關(guān)AD,清FLAG01100111
        result=ADC_RES*4+ADC_RESL; //獲取10位結(jié)果
        return(result);
        }

        上一頁(yè) 1 2 下一頁(yè)

        關(guān)鍵詞: 51單片機(jī)光源跟

        評(píng)論


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

        關(guān)閉
        主站蜘蛛池模板: 邢台县| 江北区| 伊宁县| 金溪县| 古蔺县| 漳平市| 乐至县| 阿巴嘎旗| 封丘县| 大化| 库尔勒市| 林甸县| 确山县| 禄丰县| 永福县| 万州区| 上杭县| 塔河县| 麻阳| 林周县| 嘉禾县| 库伦旗| 金塔县| 安化县| 五大连池市| 苍梧县| 包头市| 宜昌市| 宝清县| 蓬安县| 阿尔山市| 铜梁县| 综艺| 松原市| 宜章县| 托克逊县| 江阴市| 洞口县| 武冈市| 渑池县| 兴宁市|