1.5独立键盘

非编码键盘与编码键盘
编码键盘



非编码键盘


非编码键盘 有分 独立键盘 和 矩阵键盘

独立键盘

1.5独立键盘_第1张图片

当按下去之后 借口电位变低

练习

让后四位数码管以0.01秒(利用定时器)的速度从0显示到9999。按下S2开始跑数。按下S3停止跑数。按下S4数码管的数值加1(需要在停止跑数时执行才有效)。按下S5数码管的数值减1(需要在停止跑数时执行才有效)。

#include
#define uint unsigned int
#define uchar unsigned char
uint counter;
sbit we=P2^7;
sbit du=P2^6;
sbit S2=P3^0;
sbit S3=P3^1;
sbit S4=P3^2;
sbit S5=P3^3;


unsigned code leddata[]={ 

                0x3F,  //"0"
                0x06,  //"1"
                0x5B,  //"2"
                0x4F,  //"3"
                0x66,  //"4"
                0x6D,  //"5"
                0x7D,  //"6"
                0x07,  //"7"
                0x7F,  //"8"
                0x6F,  //"9"
                0x77,  //"A"
                0x7C,  //"B"
                0x39,  //"C"
                0x5E,  //"D"
                0x79,  //"E"
                0x71,  //"F"
                0x76,  //"H"
                0x38,  //"L"
                0x37,  //"n"
                0x3E,  //"u"
                0x73,  //"P"
                0x5C,  //"o"
                0x40,  //"-"
                0x00,  //熄灭
                0x00  //自定义

};

void delay(uint z)
{
    uint x,y;
    for(x = z; x>0; x--)
        for(y = 114;y>0;y--);
}

void timer_init()
{
    TMOD = 0x01;
    TH0 = (65536-9216)/256;
    TL0 = (65536-9216)%256;

}
void keyscan()
{
    if(S2==0)
    {
        delay(10);
        if(S2==0)
        {
            TR0 = 1;
            while(!S2);
        }
            
    }
    if(S3==0)
    {
        delay(10);
        if(S3==0)
        {
            TR0 = 0;
            while(!S3);
        }
            
    }
    if(S4==0)
    {
        delay(10);
        if(S4==0&&TR0==0)
        {
            counter +=1;
            while(!S4);
        }
            
    }
    if(S5==0&&TR0==0)
    {
        delay(10);
        if(S5==0)
        {
            counter -=1;
            while(!S5);
        }
            
    }
}

void display(uint x)
{
     uint qian, bai, shi, ge;
     qian = x /1000;           // 9876  9
     bai = (x/100)%10;         // 9876  8 
     shi = (x/10)%10;          //9876 7
     ge = x%10;                 // 9876 6

     we = 1;
     P0 = 0xef;
     we = 0;                //    1110 1111
     P0 = 0xff;

     du = 1;
     P0 = leddata[qian];
     du = 0;
     delay(1);

     we = 1;
     P0 = 0xdf;             //    1101 1111
     we = 0;
     P0 = 0xff;
     
     du = 1;
     P0 = leddata[bai];
     du = 0;
     delay(1);

     we = 1;
     P0 = 0xBf;             // 1011 1111
     we = 0;
     P0 = 0xff;
     du = 1;
     P0 = leddata[shi];
     du = 0;
     delay(1);

     we = 1;
     P0 = 0x7f;
     we = 0;
     P0 = 0xff;
     du = 1;
     P0 = leddata[ge];
     du = 0;
     delay(1);
}

int main()
{
    timer_init();
    while(1)
    {   
        if(TF0==1)
        {
            TH0 = (65536-9216)/256;
            TL0 = (65536-9216)%256;
            TF0 = 0;
            counter++;
            if(counter>9999)
                counter=0;
        }
        display(counter);
        keyscan();
        
    }
}


你可能感兴趣的:(1.5独立键盘)