4乘4矩阵键盘扫描物理键值

物理键值扫描

unsigned char keyscan()

{

    static unsigned char keyvalue;

    KEYPORT = 0xf0;

    if(KEYPORT != 0xf0)

    {

        delay_ms(10);

        if(KEYPORT != 0xf0)

        {

            KEYPORT = 0xfe;

            if(KEYPORT != 0xfe)

            {

                keyvalue = KEYPORT;

                keyvalue &= 0xf0;

                keyvalue += 0x0e;

                while(KEYPORT != 0xfe);

                delay_ms(10);

                while(KEYPORT != 0xfe);

                return keyvalue;

            }

            KEYPORT = 0xfd;

            if(KEYPORT != 0xfd)

            {

                keyvalue = KEYPORT;

                keyvalue &= 0xf0;

                keyvalue += 0x0d;

                while(KEYPORT != 0xfd);

                delay_ms(10);

                while(KEYPORT != 0xfd);

                return keyvalue;

            }

            KEYPORT = 0xfb;

            if(KEYPORT != 0xfb)

           {

               keyvalue = KEYPORT;

               keyvalue &= 0xf0;

               keyvalue += 0x0b;

               while(KEYPORT != 0xfb);

               delay_ms(10);

               while(KEYPORT != 0xfb);

               return keyvalue;

           }

           KEYPORT = 0xf7;

           if(KEYPORT != 0xf7)

           {

               keyvalue = KEYPORT;

               keyvalue &= 0xf0;

               keyvalue += 0x07;

               while(KEYPORT != 0xf7);

               delay_ms(10);

               while(KEYPORT != 0xf7);

               return keyvalue;

           }

       }

    }

    return 0xff;

}

 






你可能感兴趣的:(单片机学习笔记)