矩陣鍵盤(pán)-AVR程序代碼
#include
#include
#asm
.equ__lcd_port=0x18;PORTB
#endasm
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharge=0,shi=0;
ucharkey_value=0;
voidlay_ms(uintms)
{
uintx,y;
for(x=0;x
{
for(y=0;y<1141;y++);
}
}
ucharkeyscan()
{
uchari,j,temp,Buffer[4]={0xef,0xdf,0xbf,0x7f};//1110110110110111
for(j=0;j<4;j++)
{
PORTA=Buffer[j];
lay_ms(2);
temp=0x01;
for(i=0;i<4;i++)
{
if(!(PINA&temp))
{
return((i+j*4)+1);
}
temp=temp<<1;
}
}
}
voiddisplay(ucharm)
{
if(m)
{
lcd_clear();
lay_ms(15);
lcd_putsf("thekeynumber:");
ge=(m-56)+48;
shi=((m-56)/10)+48;
lcd_gotoxy(6,1);
lcd_putchar(shi);
lcd_putchar(ge);
lay_ms(100);
m=0;
}
}
voidkey_16()
{
if((PINC&0X0F)!=0X0F)
{
lay_ms(20);
if((PINA&0X0F)!=0X0F)
{
key_value=keyscan();
//key_value=55;
while((PINA&0X0F)!=0X0F);
lay_ms(5);
while((PINA&0X0F)!=0X0F);
}
}
}
voidmain(void)
{
PORTA=0x0F;
DDRA=0xF0;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
MCUCSR=0x00;
//Timer(s)/Counter(s)Interrupt(s)initialization
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
//LCDmoduleinitialization
lcd_init(16);
lcd_clear();
lcd_putsf("thekeynumber:");
while(1)
{
key_16();
printf("%dn",key_value-48);
display(key_value);
PORTA=0X0f;
};
}