|
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#pragma data:code
const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0XFF};
void delay(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
void show(uchar j,uchar k)
{
PORTA|=BIT(3);
PORTB=table[j];
PORTA&=~BIT(3);
PORTB=0XFF;
PORTB&=~BIT(k);
PORTA|=BIT(4);
PORTA&=~BIT(4);
delay(1);
}
uchar key_press()
{
uchar i;
DDRD=0XFF;
PORTD=0XF0;
DDRD=0X0F;
i=PIND;
if(i==0XF0)
{
DDRD=0XFF;
return 0;
}
else
{
DDRD=0XFF;
return 1;
}
}
uchar key_scan()
{
uchar key,i=0X7F,j;
delay(10);
if(key_press())
{
do
{
i=(i<<1|i>>7);
PORTD=i;
DDRD=0X0F;
key=PIND;
j=key&0XF0;
}while(j==0XF0);
while(key_press());
switch(key)
{
case 0xEE:
key=0x0;
break;
case 0xDE:
key=0x1;
break;
case 0xBE:
key=0x2;
break;
case 0x7E:
key=0x3;
break;
case 0xED:
key=0x4;
break;
case 0xDD:
key=0x5;
break;
case 0xBD:
key=0x6;
break;
case 0x7D:
key=0x7;
break;
case 0xEB:
key=0x8;
break;
case 0xDB:
key=0x9;
break;
case 0xBB:
key=0xA;
break;
case 0x7B:
key=0xB;
break;
case 0xE7:
key=0xC;
break;
case 0xD7:
key=0xD;
break;
case 0xB7:
key=0xE;
break;
case 0x77:
key=0xF;
break;
default:
key=16;
}
}
else
{
key=16;
}
return key;
}
void main()
{
uchar l;
DDRA|=0X18;// 0001 1000
DDRB=0XFF;
while(1)
{
if(key_press())
{
l=key_scan();
show(l,0);
}
}
}
while(key_press());在程序中加这个是干什么的啊 ???有没有人跟我讲一下哈,关于矩阵键盘程序还有没有更加简便一点的写法啊?非常感谢 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|