找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2169|回復: 4
收起左側

如何合并一下這兩個單片機程序?我合了多次都不行,要么就是不會動

[復制鏈接]
ID:922391 發(fā)表于 2021-5-21 22:03 | 顯示全部樓層 |閱讀模式
避障程序

#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);                //當RX為零時等待
                TR0=1;                            //開啟計數
                while(RX1);                        //當RX為1計數并等待
                TR0=0;                                //關閉計算
                Conut1();                        //計算
               
                qidong2();
                while(!RX2);                //當RX為零時等待
                TR0=1;                            //開啟計數
                while(RX2);                        //當RX為1計數并等待
                TR0=0;                                //關閉計算
                Conut2();                        //計算

                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();                                                         
        }
}


回復

使用道具 舉報

ID:824490 發(fā)表于 2021-5-22 10:22 | 顯示全部樓層
強烈要求畫個流程圖
回復

使用道具 舉報

ID:155507 發(fā)表于 2021-5-22 12:11 | 顯示全部樓層
為何你弄不出來呢,是你編程習慣不好。
建議:
1、先將各個程序畫成流程框圖。
2、將兩個流程框圖流程串聯起來。
3、重新整合框圖,走通各個分支。
4、在按框圖寫程序。

評分

參與人數 1黑幣 +10 收起 理由
yyds0606 + 10 贊一個!

查看全部評分

回復

使用道具 舉報

ID:922391 發(fā)表于 2021-5-22 12:18 | 顯示全部樓層
我知道了,謝謝
回復

使用道具 舉報

ID:390416 發(fā)表于 2021-5-22 18:09 | 顯示全部樓層
人人學會單片機 這套視頻的框架式編程思路,你按套路寫 多少個模塊都可以拼起來用一個程序
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表