作品实现

循迹开始。。。
循迹开始

直角转弯。。。
直角转弯

三叉线转弯。。。
三叉线转弯

直线加速。。。
直线加速

代码实现

#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define fr 50   //频率50HZ
sbit pwmout1 = P1 ^ 0;
sbit pwmout2 = P1 ^ 1;

sbit wela1 = P0 ^ 4;
sbit wela2 = P0 ^ 5;
sbit wela3 = P0 ^ 6;
sbit wela4 = P0 ^ 7;
sbit duan = P1;

sbit you1 = P2 ^ 0;//óò1
sbit you2 = P2 ^ 1;//óò2
sbit zuo1 = P2 ^ 2;//×ó1
sbit zuo2 = P2 ^ 3;//×ó2
sbit shi2 = P2 ^ 4;//ê1?üB
sbit shi1 = P2 ^ 5;//ê1?üA
sbit hong1 = P0 ^ 0;//oìía
sbit hong2 = P0 ^ 1;//
sbit hong3 = P0 ^ 2;//
sbit hong4 = P0 ^ 3;//

void delayms(uint);
uchar code table[] = {
        0x50,/*0*/
        0x5F,/*1*/
        0x38,/*2*/
        0x19,/*3*/
        0x17,/*4*/
        0x91,/*5*/
        0x90,/*6*/
        0x5D,/*7*/
        0x10,/*8*/
        0x11,/*9*/ };
uchar num, num1, a, b, c, d;

uint HighRH, HighRL, LowRH, LowRL;
void began()
{
    TMOD = 0x01;
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;
    EA = 1;
    ET0 = 1;
    TR0 = 1;

}
void pwm(unsigned int zhankongbi)
{
    if (zhankongbi > 100)
        zhankongbi = 100;
    else if (zhankongbi < 0)
        zhankongbi = 0;

    HighRH = (65536 - (zhankongbi / 100) * ((11059200 / 12) / fr) + 12) / 256;
    HighRL = (65536 - (zhankongbi / 100) * ((11059200 / 12) / fr) + 12) % 256;
    LowRH = (65536 - ((100 - zhankongbi) / 100) * ((11059200 / 12) / fr) + 12) / 256;
    LowRL = (65536 - ((100 - zhankongbi) / 100) * ((11059200 / 12) / fr) + 12) % 256;
    TH1 = HighRH;
    TL1 = HighRL;
    TR1 = 1;
    pwmout1 = 1;
    pwmout2 = 1;
}
void Closepwm()
{
    TR1 = 0;
    ET1 = 0;
    pwmout1 = 0;
    pwmout2 = 0;
}
void T0_time() interrupt 1
{
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;
    num++;
    if (num == 20)
    {
        num = 0;
        num1++;
        if (num1 == 9999)
            num1 = 0;
        a = num1 % 10;
        b = num1 / 10 % 10;
        c = num1 / 100 % 10;
        d = num1 / 1000 % 10;
    }
}

void qianjing()
{
    you1 = 0;
    you2 = 1;
    zuo1 = 1;
    zuo2 = 0;
}
void turn_left()
{
    you1 = 1;
    you2 = 0;
    zuo1 = 1;
    zuo2 = 0;
}
void turn_right()
{
    you1 = 0;
    you2 = 1;
    zuo1 = 0;
    zuo2 = 1;
}
void stop()
{
    you1 = 1;
    you2 = 1;
    zuo1 = 1;
    zuo2 = 1;
}
void main()
{
    shi1 = 1;
    shi2 = 1;
    began();
    EA = 0;
    while (1)
    {
        if (hong1 == 1 && hong2 == 1 && hong3 == 1 && hong4 == 1)
        {
            began();
            EA = 1;
            while (1)
            {
                wela1 = 0;
                P1 = table[a];
                wela1 = 1;
                P1 = 0xff;

                wela2 = 0;
                P1 = table[b];
                wela2 = 1;
                P1 = 0xff;

                wela3 = 0;
                P1 = table[c];
                wela3 = 1;
                P1 = 0xff;

                wela4 = 0;
                P1 = table[d];
                wela4 = 1;
                P1 = 0xff;

                if (a == 5)
                {
                    break;
                }
            }
            break;
        }
    }
    qianjing();
    delayms(500);
    while (1)
    {
        wela1 = 0;
        P1 = table[a];
        wela1 = 1;
        P1 = 0xff;

        wela2 = 0;
        P1 = table[b];
        wela2 = 1;
        P1 = 0xff;

        wela3 = 0;
        P1 = table[c];
        wela3 = 1;
        P1 = 0xff;

        wela4 = 0;
        P1 = table[d];
        wela4 = 1;
        P1 = 0xff;

        Closepwm();//关闭pwm,如果用不了,就删了
        if (hong1 == 0 && hong2 == 0 && hong3 == 0 && hong4 == 0)
        {
            pwm(60);
            qianjing();

        }
        else if (hong1 == 0 && hong2 == 1 && hong3 == 0 && hong4 == 0)
        {
            pwm(30);
            turn_right();

        }
        else if (hong1 == 1 && hong2 == 0 && hong3 == 0 && hong4 == 0)
        {
            pwm(60);
            turn_right();
        }
        else if (hong1 == 1 && hong2 == 1 && hong3 == 0 && hong4 == 0)
        {
            pwm(100);
            turn_right();
        }


        else if (hong1 == 0 && hong2 == 0 && hong3 == 1 && hong4 == 1)
        {
            pwm(100);
            turn_left();
        }
        else if (hong1 == 0 && hong2 == 0 && hong3 == 0 && hong4 == 1)
        {
            pwm(60);
            turn_left();
        }
        else if (hong1 == 0 && hong2 == 0 && hong3 == 1 && hong4 == 0)
        {
            pwm(30);
            turn_left();
        }


        else if (hong1 == 1 && hong2 == 1 && hong3 == 1 && hong4 == 1)
        {
            stop();
            EA = 0;
            while (1)
            {
                wela1 = 0;
                P1 = table[a];
                wela1 = 1;
                P1 = 0xff;

                wela2 = 0;
                P1 = table[b];
                wela2 = 1;
                P1 = 0xff;

                wela3 = 0;
                P1 = table[c];
                wela3 = 1;
                P1 = 0xff;

                wela4 = 0;
                P1 = table[d];
                wela4 = 1;
                P1 = 0xff;
            }
        }
    }
}
void delayms(uint xms)
{
    uint i, j;
    for (i = xms; i > 0; i--)
        for (j = 110; j > 0; j--);
}
void InterruptTimer1() interrupt 3
{
    if (pwm == 1)
    {
        TH1 = LowRH;
        TL1 = LowRL;
        pwmout1 = 0;
        pwmout2 = 0;
    }
    else
    {
        TH1 = HighRH;
        TL1 = HighRL;
        pwmout1 = 1;
        pwmout2 = 1;
    }
}
                                   感谢陪我走过大一风风雨雨兄弟们
                                      一起笑,一起闹,一起熬夜
                                         为比赛而努力的我们
                                               加油
                                            远方路很长
                                            且行且珍惜
                                                             ————无极创
文章目录