/*************************************************
程序名称:4x4矩阵键盘-数码显示键值
编译环境:CVAVR / 基于AVR
单 片 机:ATmega8515
实验对象:XY900_USB型学习开放系统板
月夜听风 http://www.mcusy.cn
*************************************************/
#include <mega8515.h>
#include <delay.h>
#define duan PORTD.3 //定义段选
#define wei PORTD.4 //定义位选
unsigned char table[]=
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
//--------------------------------------------------------
unsigned char Keyscan() //键盘扫描函数
{
DDRB=0x0f; //高四位输入,低四位输出 00001111
PORTB=0xfe; //11111110
delay_ms(2);
switch(PINB)
{
case 0xee:return 'D';
case 0xde:return '#';
case 0xbe:return 0 ;
case 0x7e:return '*';
default:break;
}
PORTB=0xfd; //11111101
delay_ms(2);
switch(PINB)
{
case 0xed:return 'C';
case 0xdd:return 9;
case 0xbd:return 8;
case 0x7d:return 7;
default:break;
}
PORTB=0xfb; //11111011
delay_ms(2);
switch(PINB)
{
case 0xeb:return 'B';
case 0xdb:return 6;
case 0xbb:return 5;
case 0x7b:return 4;
default:break;
}
PORTB=0xf7;//11110111
delay_ms(2);
switch(PINB)
{
case 0xe7:return 'A';
case 0xd7:return 3;
case 0xb7:return 2;
case 0x77:return 1; ;
default:break;
}
return;
}
//--------------------------------------------------------
void display(unsigned char num)
{
DDRD=0x18;
DDRA=0xff;
duan=1;PORTA=table[num];duan=0;//送显示段码
delay_ms(2); //
wei=1;PORTA=0X40;wei=0; //开显示位码
delay_ms(2);
}
//--------------------------------------------------------
void main()
{
unsigned char num;
while(1)
{
num=Keyscan() ; //键值赋予num
if(num>=0&&num<=9) //显示0~9
display(num);
}
}