蓝桥杯之临时抱佛脚(3)

最后一篇超声波和NE555,超声波去年考过,NE555省赛考过所以再次考的概率不高

下面的代码是我从实验室一位大佬那么学习的

超声波模块

#include "config.h"

sbit Sonic_Txd = P1^0;
sbit Sonic_Rxd = P1^1;

u8 LedChar[] = {
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90
};
u8 LedBuff[] = {
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

u32 cnt = 0;
u8 T1RH, T1RL;
bit flag200ms = 1;

void CloseFucker();
void ConfigTimer0();
void ConfigTimer1(u16 ms);
void ShowNumber(u16 num);
void SendWave();

void main()
{
    u16 time, distance=0;

    EA = 1;
    CloseFucker();
    ConfigTimer0();
    ConfigTimer1(1);

    while(1)
    {
        if(flag200ms)
        {
            flag200ms = 0;

            TH0 = 0;
            TL0 = 0;
            TF0 = 0;
            SendWave();
            TR0 = 1;
            while((Sonic_Rxd) && (TF0==0));
            TR0 = 0;

            if(TF0)
            {
                LedBuff[3] = 0xBF;
                LedBuff[2] = 0xBF;
                LedBuff[1] = 0xBF;
                LedBuff[0] = 0xBF;
            }
            else
            {
                time = ((u16)TH0<<8)+TL0;
                distance = 0.17 * time;
                ShowNumber(distance);   
            }   
        }                   
    }

}

void Delay13us()        //@11.0592MHz
{
    unsigned char i;

    _nop_();
    _nop_();
    i = 33;
    while (--i);
}


void SendWave()
{
    u8 i=8;

    while(i--)
    {
        Sonic_Txd = 1;
        Delay13us();
        Sonic_Txd = 0;
        Delay13us();    
    }   
}

void ShowNumber(u16 num)
{
    u8 buf[8];
    char i;

    for(i=0; i<4; i++)
    {
        buf[i] = num%10;
        num /= 10;
    }   
    for(i=3; i>0; i--)
    {
        if(buf[i] == 0)
        {
            LedBuff[i] = 0xFF;
        }
        else
        {
            break;
        }
    }
    for( ; i>=0; i--)
    {
        LedBuff[i] = LedChar[buf[i]];
    }
    LedBuff[1] &= 0x7F;
}

void CloseFucker()
{
    P2 = (P2&0x1F)|0xA0;
    P0 = P0&0xAF;
    P2 = P2&0x1F;
}

void ConfigTimer0()
{   
    TMOD &= 0xF0;
    TMOD |= 0x01;
    TR0 = 0;
    TF0 = 0;    
}

void ConfigTimer1(u16 ms)
{
    u32 tmp;

    tmp = 11059200/12;
    tmp = (tmp*ms)/1000;
    tmp = 65536 - tmp;
    T1RH = (u8)(tmp>>8);
    T1RL = (u8)tmp;
    TMOD &= 0x0F;
    TMOD |= 0x10;
    TH1 = T1RH;
    TL1 = T1RL;
    ET1 = 1;
    TR1 = 1;
}

void LedScan()
{
    static u8 index = 0;

    P2 = (P2&0x1F)|0xE0;
    P0 = 0xFF;
    P2 = P2&0x1F;
    P2 = (P2&0x1F)|0xC0;
    P0 = 0x80>>index;
    P2 = P2&0x1F;
    P2 = (P2&0x1F)|0xE0;
    P0 = LedBuff[index];
    P2 = P2&0x1F;

    index++;
    index &= 0x07;
}


void InterruptTimer1() interrupt 3
{
    static u16 tmr200ms = 0;
    TH1 = T1RH;
    TL1 = T1RL;
    tmr200ms++;

    if(tmr200ms >= 200)
    {
        tmr200ms = 0;
        flag200ms = 1;
    }

    LedScan();
}

NE555

#include 

typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;

u8 LedChar[] = {
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90
};
u8 LedBuff[] = {
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

u32 cnt = 0;
u8 T1RH, T1RL;
bit flag1s = 0;

void CloseFucker();
void ConfigTimer0();
void ConfigTimer1(u16 ms);
void ShowNumber(u32 num);

void main()
{
    EA = 1;
    CloseFucker();
    ConfigTimer0();
    ConfigTimer1(1);

    while(1)
    {
        if(flag1s)
        {
            flag1s = 0;
            ShowNumber(cnt);
            cnt = 0;
        }
    }

}

void CloseFucker()
{
    P2 = (P2&0x1F)|0xA0;
    P0 = P0&0xAF;
    P2 = P2&0x1F;
}

void ConfigTimer0()
{
    TH0 = 0xFF;
    TL0 = 0xFF;
    TMOD &= 0xF0;
    TMOD |= 0x04;
    ET0 = 1;
    TR0 = 1;    
}

void ConfigTimer1(u16 ms)
{
    u32 tmp;

    tmp = 11059200/12;
    tmp = (tmp*ms)/1000;
    tmp = 65536 - tmp;
    T1RH = (u8)(tmp>>8);
    T1RL = (u8)tmp;
    TMOD &= 0x0F;
    TMOD |= 0x10;
    TH1 = T1RH;
    TL1 = T1RL;
    ET1 = 1;
    TR1 = 1;
}

void LedScan()
{
    static u8 index = 0;

    P2 = (P2&0x1F)|0xE0;
    P0 = 0xFF;
    P2 = P2&0x1F;
    P2 = (P2&0x1F)|0xC0;
    P0 = 0x80>>index;
    P2 = P2&0x1F;
    P2 = (P2&0x1F)|0xE0;
    P0 = LedBuff[index];
    P2 = P2&0x1F;

    index++;
    index &= 0x07;
}

void ShowNumber(u32 num)
{
    u8 buf[8];
    char i;

    for(i=0; i<8; i++)
    {
        buf[i] = num%10;
        num /= 10; 
    }
    for(i=7; i>0; i--)
    {
        if(buf[i] == 0)
        {
            LedBuff[i] = 0xFF;
        }
        else
        {
            break;
        }
    }
    for( ; i>=0; i--)
    {
        LedBuff[i] = LedChar[buf[i]];
    }
}

void InterruptTimer0() interrupt 1
{
    cnt++;
}

void InterruptTimer1() interrupt 3
{
    static u16 tmr1s = 0;

    TH1 = T1RH;
    TL1 = T1RL;
    tmr1s++;
    if(tmr1s == 1000)
    {
        tmr1s = 0;
        flag1s = 1;
    }

    LedScan();
}

 

你可能感兴趣的:(蓝桥杯抱佛脚)