nclude <iom16v.h>
#include <macros.h>
#include<AVR_HJ-2G.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(100);
}
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 a,KeyValue;
delay(10);
if(key_press())
{
PORTD=0xf0;
DDRD=0X0f;
a=PIND;
switch(a)
{
case 0x70:
KeyValue=4;
break;
case 0xb0:
KeyValue=3;
break;
case 0xd0:
KeyValue=2;
case 0xe0:
KeyValue=1;
break;
}
DDRD=0xff;
PORTD=0x0f;
DDRD=0xf0;
a=PIND;
switch(a)
{
case 0x07:
KeyValue=KeyValue+12;
break;
case 0x0b:
KeyValue=KeyValue+8;
break;
case 0x0d:
KeyValue=KeyValue+4;
case 0x0e:
KeyValue=KeyValue;
break;
}
return KeyValue;
}
else
{
return 0;
}
}
void main()
{
uchar l;
//LEDOFF(); //关LED总开关
SEGOFF();//关数码显示管函数 用于开机时数码管不显示
DDRA|=0X18;// 0001 1000
DDRB=0XFF;
LEDON();
while(1)
{
if(key_scan()==1)
{
//l=key_scan();
//show(key_scan(),0);
PORTB=0x00;
}
}
}
扫描函数那里那里错了,为什么得不到我要的结果,上面程序的结果是,如果key_scan()=1,我按矩阵键盘第一行和第二行都可以把灯点亮,但是第三第四行没反应,把key_scan()=2,所有按键怎么按都没反应 |