#include
#include
#define uchar unsigned char
#define uint unsigned int
void Delayms(uint MS);
uchar key_scan(void);
void init_io(void);
uchar led_discode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
uchar key_val=0;
void main (viod)
{
uchar key_io;
init_io();
while(1)
{
PORTD=0XF0;
Delayms(5);
key_io=PIND & 0xf0;
if(key_io !=0xf0)
{
PORTD=0XF0;
Delayms(1);
key_io=PIND & 0xf0;
if(key_io !=0xf0)
{
key_val=key_scan();
key_val |= key_io ;
}
switch(key_val)
{
case 0xee: key_val=0x00; break; //按键K1
case 0xde: key_val=0x01; break; //按键K2
case 0xbe: key_val=0x02; break; //按键K3
case 0x7e: key_val=0x03; break; //按键K4
case 0xed: key_val=0x04; break; //按键K5
case 0xdd: key_val=0x05; break; //按键K6
case 0xbd: key_val=0x06; break; //按键K7
case 0x7d: key_val=0x07; break; //按键K8
case 0xeb: key_val=0x08; break; //按键K9
case 0xdb: key_val=0x09; break; //按键K10
case 0xbb: key_val=0x0a; break; //按键K11
case 0x7b: key_val=0x0b; break; //按键K12
case 0xe7: key_val=0x0c; break; //按键K13
case 0xd7: key_val=0x0d; break; //按键K14
case 0xb7: key_val=0x0e; break; //按键K15
case 0x77: key_val=0x0f; break; //按键K16
}
}
PORTD=0XF0;
key_io=PIND & 0xf0;
while(key_io !=0xf0)
{
key_io=PIND & 0xf0;
}
PORTC=led_discode[key_val];
}
}
void init_io(void)
{
DDRA=0XFF;
PORTA=0XFF;
DDRB=0XFF;
PORTB=0XFF;
DDRC=0XFF;
PORTC=0XFF;
DDRD=0X0F;
PORTD=0XF0;
DDRF=0X0E;
PORTF=0X0E;
PORTF&=0XF7;
DDRB |=0X10;
PORTB|=0X10;
}
uchar key_scan(void)
{
uchar m,temp=0xf7;
for(m=0;m<4;m++)
{
PORTD=temp | 0xf0;
Delayms(1);
if((PIND & 0XF0)!=0XF0)
return(temp & 0x0f);
temp>>=1;
}
}
void Delayms(uint MS)
{
uint i,j;
for( i=0;i
for(j=0;j<1141;j++);
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/262316.html