模块的蓝桥杯

都要比赛了,我还在浪

总管这届的题目很多都可以将各个模块独立开来,从而适应每个题目

下面该程序就抽象出一个基本通杀所有题目的函数主体;

key_scan 使用了4x4的矩阵键盘,即便是只使用一列也可以用,display的以两位作为demo,需要的时候小改就可。整个程序的显示部分在定时器中不断扫描,用更新显存的方式来处理,同时用页的概念来处理不同的显示界面的切换。同时本程序使用大量的switch-case 语句增加代码的可读性。需要说明的是,真正使用的时候把tempkey清零,这里注释掉是为了检验模块的是否能正常工作

#include


#include 
#include 
#define uchar unsigned char
#define uint unsigned int
uchar code tab[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xbf, 0xff};
uchar tempkey;
uchar page ;

void Delay1ms() //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    _nop_();
    _nop_();
    i = 11;
    j = 190;
    do
    {
        while (--j)
            ;
    } while (--i);
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
    AUXR |= 0x80; //定时器时钟1T模式
    TMOD &= 0xF0; //设置定时器模式
    TL0 = 0xCD;   //设置定时初值
    TH0 = 0xD4;   //设置定时初值
    TF0 = 0;      //清除TF0标志
    TR0 = 1;      //定时器0开始计时
    EA = 1;
    ET0 = 1;
}
uchar key_scan()
{
    uchar temp;
    // tempkey = 0; //清楚按键缓存
    P30 = P31 = P32 = P33 = 0;
    P34 = P35 = P42 = P44 = 1;

    Delay1ms();
    Delay1ms();
    Delay1ms();

    if (P34 == 0)
    {
        P30 = P31 = P32 = P33 = 1;
        P34 = P35 = P42 = P44 = 0;
        temp = P3;

        temp = temp & 0x0f; //方便处理
        switch (temp)
        {
        case 0x0e:
            tempkey = 19;
            break;

        case 0x0d:
            tempkey = 18;
            break;
        case 0x0b:
            tempkey = 17;
            break;
        case 0x07:
            tempkey = 16;
        }
    }

    else if (P35 == 0)
    {
        P30 = P31 = P32 = P33 = 1;
        P34 = P35 = P42 = P44 = 0;
        temp = P3;

        temp = temp & 0x0f; //方便处理
        switch (temp)
        {
        case 0x0e:
            tempkey = 15;
            break;
        case 0x0d:
            tempkey = 14;
            break;
        case 0x0b:
            tempkey = 13;
            break;
        case 0x07:
            tempkey = 12;
        }
    }

    else if (P42 == 0)
    {
        P30 = P31 = P32 = P33 = 1;
        P34 = P35 = P42 = P44 = 0;
        temp = P3;

        temp = temp & 0x0f; //方便处理
        switch (temp)
        {
        case 0x0e:
            tempkey = 11;
            break;
        case 0x0d:
            tempkey = 10;
            break;
        case 0x0b:
            tempkey = 9;
            break;
        case 0x07:
            tempkey = 8;
        }
    }

    else if (P44 == 0)
    {
        P30 = P31 = P32 = P33 = 1;
        P34 = P35 = P42 = P44 = 0;
        temp = P3;

        temp = temp & 0x0f; //方便处理
        switch (temp)
        {
        case 0x0e:
            tempkey = 7;
            break;
        case 0x0d:
            tempkey = 6;
            break;
        case 0x0b:
            tempkey = 5;
            break;
        case 0x07:
            tempkey = 4;
        }
    }

    return tempkey;
}

void display(uchar display_code)
{
    uchar m, n;
    m = display_code / 10;
    n = display_code % 10;

    P25 = 0;
    P26 = 1;
    P27 = 1;
    P0 = 0x01;

    P25 = 1;
    P26 = 1;
    P27 = 1;
    P0 = tab[m];

    Delay1ms();

    P25 = 0;
    P26 = 1;
    P27 = 1;
    P0 = 0x02;

    P25 = 1;
    P26 = 1;
    P27 = 1;
    P0 = tab[n];

    Delay1ms();

    P0 = 0xff;
}
int main()
{

    Timer0Init();
    while (key_scan())
    {
        case 19:

        break;
        case 18:

        break;

        case 17:


        break ;
        case 16 :

        break;
        default :

        break;



        
        ;
    }
}

void time() interrupt 1
{
    uchar i;
    i++;
    if (i == 5)
    {
        i = 0;
        if(page==1)
        {
            display(tempkey);
        }
        
    }
}



你可能感兴趣的:(物联网)