吃完个饭 又得开始整理了 现在就整理下关于AVR的键盘扫描 继续进一步理解AVR端口与寄存器的使用
下面的这个程序 针对的不是矩阵矩阵扫描 而是针对独立键盘扫描
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
//延时1MS子程序
void DelayMs ( uint i )
{
uchar j;
while (i--)
{
j=167; //1MS延时
while (j--);
}
}
//键盘扫描程序
void KeyScan ( void)
{
uchar KayNum=0;
DDRA=0xff;
PORTA=0xff;
DelayMs(1);
DDRA=0x00;
if ( PINA!=0xff)
{
DelayMs(1); //防抖动
}
if ( PINA!=0xff)
{
switch (PINA & 0xff)
{
case 0b11111110 : KeyNum=1;break;
case 0b11111101 : KeyNum=2;break;
case 0b11111011 : KeyNum=3;break;
case 0b11110111 : KeyNum=4;break;
case 0b11101111 : KeyNum=5;break;
case 0b11011111 : KeyNum=6;break;
case 0b10111111 : KeyNum=7;break;
case 0b01111111 : KeyNum=8;break;
default : break;
}
while (PINA!=0xff)
{
DDRB=0xff;
PORTB&=~( 1<<(KeyNum-1));
DDRA=0xff;
PORTA=0xff; //在显示的时候按下其它按键无效
DelayMs(1);
DDRA=0x00;
}
DDTB=0xff;
PORTB=0xff;
}
}
//主函数
void main ( void)
{
DDRB=0xff;
PORTB=0xff;
while ( 1)
{
DelayMs (200) ;
KeyScan () ;
}
}
好咯 这个程序经过自己做的板子调试 完全可行 至于矩阵键盘的驱动程序 由于当时做板子时为了方便没有做那个模块 所以现在也省了 但其原理也都差不多 唯一要注意的是 矩阵键盘的程序扫描:用两次反扫描以便读出按下的某个键
转载于:https://blog.51cto.com/lengyiyun/196775