3个普通IO识别22个按键试验
吸取各位前辈的经验,将之前二极管用量多的问题优化一下,目前不用二极管能接6键,2只二极管能接12键,6只二极管能接18键,9只二极管能接21键,第22键要单独占用3只二极管最不化算。
实验用89S51作试验,电路接线就是P1.2,P1.3,P1.4接键盘,P1.0接显示器。
(原文件名:GIF.gif)
(原文件名SC00015.JPG)
/*==================================================================*
* 3个IO接识别22键测试程序 *
* ------------------------------------------------ *
* MCU: AT89C2051 *
* OSC: 12M cysytel *
* 程序设计:Cowboy *
* 程序版本:V1.0 *
*==================================================================*/
#include
//================== IO口线连接 ==================
sbit Bus = P1^0;
sbit IO_a = P1^4;
sbit IO_b = P1^3;
sbit IO_c = P1^2;
//================== 变量声明 ====================
unsigned char Disp_buf[3];
unsigned char Dig;
unsigned char Key_count;
unsigned char bdata Key_state;
sbit KB0 = Key_state^0;
sbit KB1 = Key_state^1;
sbit KB2 = Key_state^2;
sbit KB3 = Key_state^3;
sbit KB4 = Key_state^4;
sbit KB5 = Key_state^5;
//================== 表格数据 ====================
code unsigned char LED_FONT[24]=
{
0x84,0x9f,0xa2,0x8a,0x99,0xc8,0xc0,0x9e,0x80, //012345678
0x88,0x90,0xc1,0xe4,0x83,0xe0,0xf0,0xff,0xfb, //9abcdef -
};
code unsigned char Key_tab[64]= //键码映射表
{// 0 1 2 3 4 5 6 7 8 9
22, 0, 2, 0, 0, 0, 0, 0, 4, 0, //0
0, 0, 0, 0, 0,18, 0, 0, 0, 0, //1X
0, 0, 0, 0, 0, 0, 3,14, 0, 0, //2X
20,10, 6, 0, 0, 0, 0, 0, 1,19, //3X
0, 5, 0, 0, 0,15, 0,11, 0, 0, //4X
0,17, 0, 0,13, 8, 0,21, 0, 9, //5X
16,12, 7, 0 //6X
};
//=============== 检测按键 =================
void Key_scan()
{
unsigned char i;
Key_count --; //扫描次序
Key_count &= 3;
switch (Key_count) //按次序处理
{
case 2: //第一轮扫描
KB0 = IO_b;
KB1 = IO_c;
IO_a = 1;
IO_b = 0;
break;
case 1: //每二轮扫描
KB2 = IO_c;
KB3 = IO_a;
IO_b = 1;
IO_c = 0;
break;
case 0: //每三轮扫描
KB4 = IO_a;
KB5 = IO_b;
IO_c = 1;
break;
default: //每四轮扫描
if (!IO_a) KB0 = 0;
if (!IO_b) KB2 = 0;
if (!IO_c) KB4 = 0;
IO_a = 0;
//======更新显示缓冲区=======
i = Key_tab[Key_state];
if (i == 0)
{
Disp_buf[2] = 0x11; //显示三横
Disp_buf[1] = 0x11;
Disp_buf[0] = 0x11;
}
else
{
Disp_buf[2] = 0x0c; //字符"C"
Disp_buf[1] = i / 10; //键码十位
Disp_buf[0] = B;于 //键码个位
}
Key_state = 0;
}
}
/*===================================================================
ONE WIRE 显示总线驱动程序
===================================================================*/
//=============== 发送一位 =================
void Send_bit(bit Dat)
{
unsigned char i = 3;
if (!Dat) Bus = 0;
else
{
Bus = 0;
- Windows CE下驱动程序开发基础(二)(11-09)
- 用NiosII搭建的固态盘设备系统(03-23)
- 采用Linux与DSP/BIOS RTOS实施双OS信号处理技术(02-13)
- 迎合三重播放业务时代需要的基础局端 DSP(01-28)
- 展望未来 多核DSP技术不仅仅是小把戏(04-03)
- 基于Nios II的多媒体广告系统原理设计(02-20)