十年专注单片机方案开发的方案公司金沙js5线路,分享PIC16F87X单片机4X4矩阵键盘。金沙js5线路现提供服务产品涉及主控芯片:8位单片机、16位单片机、32位单片机及各类运算放大器等。
/************************************************* * 键盘扫描子程序 * *************************************************/ unsigned char keyscan() { unsigned char temp,key_value; static bit b_keyoncol; //某一列上有按键时置1
PORTB=0xFF; b_keyoncol=0; key_value=0; //扫描RB0 RB0=0; #asm nop nop nop nop #endasm temp=key_col[PORTB>>4]; DELAY(); if(temp==key_col[PORTB>>4]){ if(temp==0xFF) return 0xFF; else if(temp!=0){ b_keyoncol=1; key_value=temp; } } //扫描RB1 RB0=1; RB1=0; temp=key_col[PORTB>>4]; DELAY(); if(temp==key_col[PORTB>>4]){ if(temp==0xFF) return 0xFF; else if (temp!=0){ if(b_keyoncol==1) return 0xFF; else
{key_value=temp+4; b_keyoncol=1;} } } //扫描RB2 RB0=1; RB1=1; RB2=0; temp=key_col[PORTB>>4]; DELAY(); if(temp==key_col[PORTB>>4]){ if (temp==0xFF) return 0xff; else if (temp!=0){ if(b_keyoncol==1) return 0xFF; else {key_value=temp+8; b_keyoncol=1;} } } //扫描RB3 RB0=1; RB1=1; RB2=1; RB3=0; temp=key_col[PORTB>>4]; DELAY(); if(temp==key_col[PORTB>>4]){ if (temp==0xFF) return 0xff; else if (temp!=0){ if(b_keyoncol==1) return 0xFF; else {key_value=temp+12; b_keyoncol=1;} } }
//扫描结束 RB3=1;
return key_value; }
(文源网络,侵删)