#ifndef __key_h
#define __key_h
#include "sys.h"
//行操作巨集定義
#define row1_write paout(0)
#define row2_write paout(2)
#define row3_write paout(3)
#define row1_read pain(0)
#define row2_read pain(2)
#define row3_read pain(3)
//列操作巨集定義
#define col1_write paout(7)
#define col2_write paout(6)
#define col3_write paout(4)
#define col1_read pain(7)
#define col2_read pain(6)
#define col3_read pain(4)
#define pin_out(x) //將io口配置為輸出
#define pin_in(x) //將io口配置為輸入
void key_init(void); //io初始化
u8 key_scan(u8); //按鍵掃瞄函式
#endif
#include "key.h"
#include "delay.h"
#include "usart.h"
void key_init(void)
//按鍵處理函式
u8 key_scan(u8 mode)
,,,};
u8 row=0,col=0;
col1_write=1;
col2_write=1;
col3_write=1;
//檢測行
if(row1_read==1||row2_read==1||row3_read==1)
else
return 0; //無按鍵按下
//改變輸入輸出模式
col1_write=0;
col2_write=0;
col3_write=0;
pin_out(0);
pin_out(2);
pin_out(3);
pin_in(4);
pin_in(6);
pin_in(7);
row1_write=1;
row2_write=1;
row3_write=1;
//檢測列
if(col1_read==1)
col=1;
else if(col2_read==1)
col=2;
else if(col3_read==1)
col=3;
//還原io口
pin_out(4);
pin_out(6);
pin_out(7);
pin_in(0);
pin_in(2);
pin_in(3);
if(row!=0&&col!=0)
else
return 0;
}
#include "sys.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include "led.h"
int main(void)
} led0=0; }
}
4 4矩陣鍵盤C51程式
include include define key port p0 鍵盤介面定義 sbit key port 0 key port 0 sbit key port 1 key port 1 sbit key port 2 key port 2 sbit key port 3 key port 3 ...
矩陣鍵盤掃瞄
矩陣鍵盤掃瞄一般採取行列掃瞄法。比如先拉低所有列線,拉高行線,之後讀取行線狀態,如果行線有一行為低,則假設有按鍵按下,此時再延時20毫秒左右後判 斷行線的某一行是否仍然為低,若為低則確認有鍵按下。這時可以進行按鍵鍵值判斷,即判別在哪一行哪一列有按鍵按下。判斷的方法是通過拉高列線,依次置行線 的某一行...
矩陣鍵盤掃瞄
矩陣鍵盤掃瞄 module matrix keyboard input clk 50m,50m時鐘 input rst n,復位 input 3 0 row,矩陣鍵盤行輸入 output reg 3 0 col,矩陣鍵盤列輸出 output reg key flag,按鍵按下標誌 output re...