PIC16F877A矩陣鍵盤的用法
/*******************************************************************************
Platform: PIC1687A
Project : 實驗16:矩陣式按鍵
Clock F : 外部4M
Software: PICC
Author : 竹林清風(fēng)
comments:
學(xué)習(xí)使用矩陣式按鍵的用法
本例功能是按一下相應(yīng)鍵,數(shù)碼管顯示相應(yīng)的值
0-9;不帶連發(fā),不帶組合,希望有興趣的人幫忙完成
并共享
UP鍵按鍵計數(shù)
proteus仿真通過;
*******************************************************************************/
#include
#include
#include"delay.h"
#include"key.h"
unsigned char led_7[]={0x3f,0x06,0x5b,0x4F,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//數(shù)碼管顯示值
unsigned char key_temp;
void main(void)
{
unsigned char ID=0;
TRISB=0X00;//所有B0端口輸出
PORTB=0XFF;
TRISD=0X0F;//初始化鍵盤接口
PORTD=0XFF;//置輸出為高電平
while(1){
delay_nms(5);
key_temp = read_keyboard();// 調(diào)用鍵盤接口函數(shù)讀鍵盤
if (key_temp != No_key)
{// 有按鍵按下
switch(key_temp)
{
case 1:
PORTB=led_7[1];
break;
case 2:
PORTB=led_7[2];
break;
case 3:
PORTB=led_7[3];
break;
case 4:
PORTB=led_7[4];
break;
case 5:
PORTB=led_7[5];
break;
case 6:
PORTB=led_7[6];
break;
case 7:
PORTB=led_7[7];
break;
case 8:
PORTB=led_7[8];
break;
case 9:
PORTB=led_7[9];
break;
case K3_2:
PORTB=led_7[0];
break;
case 13:
if(++ID>=10)
{
ID=0;
}
PORTB=led_7[ID];
break;
}
}
}
}
下面為延時函數(shù):文件delay.c
void delay_1us(void) //1us
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延時函數(shù)
{
unsigned int i=0;
for (i=0;i
}
void delay_1ms(void) //1ms延時函數(shù)
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時函數(shù)
{
unsigned int i=0;
for (i=0;i
頭文件:delay.h
void delay_1us(void);
void delay_nus(unsigned int n); //N us延時函數(shù)
void delay_1ms(void); //1ms延時函數(shù)
void delay_nms(unsigned int n); //N ms延時函數(shù)
鍵盤key.h
#define No_key 255
#define K1_11
#define K1_22
#define K1_33
#define k1_4 4
#define K2_15
#define K2_26
#define K2_37
#define K2_48
#define K3_19
#define K3_20
#define K3_310
#define K3_411
#define K4_112
#define K4_213
#define K4_314
#define K4_415
#define Key_mask0b00001111
unsigned char read_keyboard()
{
static unsigned char key_state = 0, key_value, key_line;
unsigned char key_return = No_key,i,key_time;
switch (key_state)
{
case 0:
key_line = 0b00010000;
for (i=1; i<=4; i++)// 掃描鍵盤
{
PORTD = ~key_line;// 輸出行線電平
PORTD = ~key_line;// 必須送2次!?。?br/> key_value = Key_mask & PORTD;// 讀列電平
if (key_value == Key_mask)
key_line <<= 1;// 沒有按鍵,繼續(xù)掃描
else
{
key_state++;// 有按鍵,停止掃描
break;// 轉(zhuǎn)消抖確認狀態(tài)
}
}
break;
case 1:
if (key_value == (Key_mask & PORTD))// 再次讀列電平,
{
switch (key_line | key_value)// 與狀態(tài)0的相同,確認按鍵
{// 鍵盤編碼,返回編碼值
case 0b00011110:
key_return = K1_1; //"1"
key_state++;
break;
case 0b00011101:
key_return = K1_2; //"2"
key_state++;
break;
case 0b00011011:
key_return = K1_3; //"3"
key_state++;
break;
case 0b00010111:
key_return = k1_4; //"4"
key_state++;
break;
case 0b00101110:
key_return = K2_1; //"5"
key_state++;
break;
case 0b00101101:
key_return = K2_2; //"6"
key_state++;
break;
case 0b00101011:
key_return = K2_3; //"7"
key_state++;
break;
case 0b00100111:
key_return = K2_4; //"8"
key_state++;
break;
case 0b01001110:
key_return = K3_1; //"9"
key_state++;
break;
case 0b01001101:
key_return = K3_2; //"0"
key_state++;
break;
case 0b01001011:
key_return = K3_3; //"A"
key_state++;
break;
case 0b01000111:
key_return = K3_4; //"B"
key_state++;
break;
case 0b10001110:
key_return = K4_1; //"C"
key_state++;
break;
case 0b10001101:
//key_return = K4_2; //"D"
// key_state++;
key_time=0;
key_state=3;
break;
case 0b10001011:
key_return = K4_3; //"E"
key_state++;
break;
case 0b10000111:
key_return = K4_4; //"F"
key_state++;
break;
}
// 轉(zhuǎn)入等待按鍵釋放狀態(tài)
}
else
key_state--;// 兩次列電平不同返回狀態(tài)0,(消抖處理)
break;
case 2:// 等待按鍵釋放狀態(tài)
PORTD = 0b00000111;// 行線全部輸出低電平
PORTD = 0b00000111;// 重復(fù)送一次
if ( (Key_mask & PORTD) == Key_mask)
key_state=0;// 列線全部為高電平返回狀態(tài)0
break;
case 3:
PORTD = 0b00000111;// 行線全部輸出低電平
PORTD = 0b00000111;// 重復(fù)送一次
if ( (Key_mask & PORTD) == Key_mask)
{
key_state=0;// 列線全部為高電平返回狀態(tài)0
key_return = K4_2;
}
//break;
else if( (Key_mask & PORTD)!=Key_mask)
{
if(++key_time>=100)
{
key_state=4;
key_time=0;
key_return=20;
}
}
break;
case 4:
PORTD = 0b00000111;// 行線全部輸出低電平
PORTD = 0b00000111;// 重復(fù)送一次
if ( (Key_mask & PORTD) == Key_mask)
{
key_state=0;// 列線全部為高電平返回狀態(tài)0
//key_return = K4_2;
}
else if(++key_time>=20)
{
key_time=0;
key_return=20;
}
break;
}
return key_return;
}