找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2035|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

如何合并一下這兩個(gè)單片機(jī)程序?我合了多次都不行,要么就是不會(huì)動(dòng)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:922391 發(fā)表于 2021-5-21 22:03 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
避障程序

#include"reg51.h"
#include <intrins.h>  
#define uchar unsigned char
#define uint  unsigned int
#define ulong  unsigned long

uint time=0,timer=0;
ulong S=0,T=0;

sbit RX1=P2^6;
sbit TX1=P2^7;
sbit RX2=P3^6;
sbit TX2=P3^7;

uint lf0=0,lf1=0;
uint lb0=0,lb1=0;
uint rf0=0,rf1=0;
uint rb0=0,rb1=0;

sbit inputL=P2^0;sbit inputR=P2^1;

sbit a1=P0^7;sbit a2=P0^6;
sbit b1=P0^5;sbit b2=P0^4;
sbit c1=P1^7;sbit c2=P1^6;                                                                                                                                         
sbit d1=P1^5;sbit d2=P1^4;
void Delay(uint xms)
{
        uchar i, j;
        while(xms--)
        {
                i = 2;
                j = 239;
                do
                {
                        while (--j);
                } while (--i);
        }
}

void forward(int pwm,int time)
{
        int a=0;
        while(a<time)
                {
                        a1=0,a2=1;
                        b1=0,b2=1;
                        c1=0,c2=1;
                        d1=0,d2=1;
                        Delay(pwm);
                        
                        a1=0,a2=0;
                        b1=0,b2=0;
                        c1=0,c2=0;
                        d1=0,d2=0;
                        Delay(10-pwm);

                        a++;               
                }
}               



void turnleft(int pwm,int time)
{        
        int a=0;
        while(a<time)
                {
                        a1=1,a2=0;
                        b1=1,b2=0;
                        c1=0,c2=1;
                        d1=0,d2=1;
                        Delay(pwm);
                        
                        a1=0,a2=0;
                        b1=0,b2=0;
                        c1=0,c2=0;
                        d1=0,d2=0;
                        Delay(10-pwm);

                        a++;               
                }

}

void turnright(int pwm,int time)
{
        int a=0;
        while(a<time)
                {
                        a1=0,a2=1;
                        b1=0,b2=1;
                        c1=1,c2=0;
                        d1=1,d2=0;
                        Delay(pwm);
                        
                        a1=0,a2=0;
                        b1=0,b2=0;
                        c1=0,c2=0;
                        d1=0,d2=0;
                        Delay(10-pwm);

                        a++;               
                }
}


void back(int pwm,int time)
{
        int a=0;
        while(a<time)
                {
                        a1=1,a2=0;
                        b1=1,b2=0;
                        c1=1,c2=0;
                        d1=1,d2=0;
                        Delay(pwm);
                        
                        a1=0,a2=0;
                        b1=0,b2=0;
                        c1=0,c2=0;
                        d1=0,d2=0;
                        Delay(10-pwm);

                        a++;               
                }
}

void maichong()
{                                       
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();           
        _nop_();
}

void qidong1()
{
        TX1=1;                                       
        maichong();
        TX1=0;
}
void qidong2()
{
        TX2=1;                                       
        maichong();
        TX2=0;
}

void Conut1()
{                                                   
        time=TH0*256+TL0;
        TH0=0;
        TL0=0;        
        S=(long)(time*0.17);
}

void Conut2()
{                                                   
        timer=TH0*256+TL0;
        TH0=0;
        TL0=0;        
        T=(long)(timer*0.17);
}

void kaishi()
{
        TMOD=0x01;                  
        TH0=0;
        TL0=0;
        ET0=1;         
        EA=1;                                 
}

void scan()
{
        if(S>200&&T>200)
        {
                forward(5,5);
        }
        if(S>200&&T<200)
        {
                turnleft(5,5);
        }
        if(S<200&&T>200)
        {
                turnright(5,5);
        }
        if(S<200&&T<200)
        {
                back(5,5);
        }
}
void  main()
{         
        kaishi();                          
        while(1)
        {               
                qidong1();
                while(!RX1);                //當(dāng)RX為零時(shí)等待
                TR0=1;                            //開啟計(jì)數(shù)
                while(RX1);                        //當(dāng)RX為1計(jì)數(shù)并等待
                TR0=0;                                //關(guān)閉計(jì)算
                Conut1();                        //計(jì)算
               
                qidong2();
                while(!RX2);                //當(dāng)RX為零時(shí)等待
                TR0=1;                            //開啟計(jì)數(shù)
                while(RX2);                        //當(dāng)RX為1計(jì)數(shù)并等待
                TR0=0;                                //關(guān)閉計(jì)算
                Conut2();                        //計(jì)算

                scan();
        }
}

循跡程序
#include"reg51.h"
#include <intrins.h>
#define uchar unsigned char
#define uint  unsigned int
#define ulong  unsigned long

sbit a1=P0^7;sbit a2=P0^6;
sbit b1=P0^5;sbit b2=P0^4;
sbit c1=P1^7;sbit c2=P1^6;
sbit d1=P1^5;sbit d2=P1^4;

uint time=0,timer=0;

sbit inputL=P2^0;sbit inputR=P2^1;
void Delay(uint xms)
{
        uchar i, j;
        while(xms--)
        {
                i = 2;
                j = 239;
                do
                {
                        while (--j);
                } while (--i);
        }
}

void forward(int pwm,int time)
{
        int a=0;
        while(a<time)
                {
                        a1=0,a2=1;
                        b1=0,b2=1;
                        c1=0,c2=1;
                        d1=0,d2=1;
                        Delay(pwm);
                        
                        a1=0,a2=0;
                        b1=0,b2=0;
                        c1=0,c2=0;
                        d1=0,d2=0;
                        Delay(10-pwm);

                        a++;               
                }
}               



void turnleft(int pwm,int time)
{        
        int a=0;
        while(a<time)
                {
                        a1=0,a2=1;
                        b1=0,b2=1;
                        c1=1,c2=0;
                        d1=1,d2=0;
                        Delay(pwm);
                        
                        a1=0,a2=0;
                        b1=0,b2=0;
                        c1=0,c2=0;
                        d1=0,d2=0;
                        Delay(10-pwm);

                        a++;               
                }

}

void turnright(int pwm,int time)
{
        int a=0;
        while(a<time)
                {
                        a1=1,a2=0;
                        b1=1,b2=0;
                        c1=0,c2=1;
                        d1=0,d2=1;
                        Delay(pwm);
                        
                        a1=0,a2=0;
                        b1=0,b2=0;
                        c1=0,c2=0;
                        d1=0,d2=0;
                        Delay(10-pwm);

                        a++;               
                }
}



void scan()
{
        if(inputL==0&&inputR==0)
        {
                forward(3,5);
        }
        if(inputL==1&&inputR==1)
        {
                forward(3,5);
        }
        if(inputL==1&&inputR==0)
        {
                turnleft(3,5);
        }
        if(inputL==0&&inputR==1)
        {
                turnright(3,5);
        }
}


void kaishi()
{
        TMOD|=0X11;        
        TH1=0xff;
        TL1=0x9c;        
        ET1=1;
        TR1=1;
}

void  main()

{  
        kaishi();                           
        EA=1;
        while(1)
        {
                scan();                                                         
        }
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:824490 發(fā)表于 2021-5-22 10:22 | 只看該作者
強(qiáng)烈要求畫個(gè)流程圖
回復(fù)

使用道具 舉報(bào)

板凳
ID:155507 發(fā)表于 2021-5-22 12:11 | 只看該作者
為何你弄不出來呢,是你編程習(xí)慣不好。
建議:
1、先將各個(gè)程序畫成流程框圖。
2、將兩個(gè)流程框圖流程串聯(lián)起來。
3、重新整合框圖,走通各個(gè)分支。
4、在按框圖寫程序。

評(píng)分

參與人數(shù) 1黑幣 +10 收起 理由
yyds0606 + 10 贊一個(gè)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

地板
ID:922391 發(fā)表于 2021-5-22 12:18 | 只看該作者
我知道了,謝謝
回復(fù)

使用道具 舉報(bào)

5#
ID:390416 發(fā)表于 2021-5-22 18:09 | 只看該作者
人人學(xué)會(huì)單片機(jī) 這套視頻的框架式編程思路,你按套路寫 多少個(gè)模塊都可以拼起來用一個(gè)程序
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表