|
以前写过键盘扫描的程序,下载到at89s52上面很正常,但是如果移植到mega16上按键k4-1就不能正确显示,led灯在不停闪烁,其他的按键都是正常的。敬请各位大侠指教:
winavr中程序如下:
//PORTD0~3分别接X1~4;PORTA0~2分别接Y1~3;
#include "avr/io.h"
#include "util/delay.h"
int main(void)
{
PORTA=0XFF,DDRA=0XF8; //PORT0~3为输入状态,内部上拉
PORTB=0XFF,DDRB=0XFF;
PORTC=0XFF,DDRC=0XFF;
PORTD=0XFF,DDRD=0XFF;
while(1)
{
scan:
//first line
{
PORTD&=~(1<<PD0);PORTD|=(1<<PD1);PORTD|=(1<<PD2);PORTD|=(1<<PD3);
if(!(PINA&(1<<PA0)))
{PORTB=0XF1;goto delay;}
else if(!(PINA&(1<<PA1)))
{PORTB=0XF2;goto delay;}
else if(!(PINA&(1<<PA2)))
{PORTB=0XF3;goto delay;}
else
//second line
{
PORTD|=(1<<PD0);PORTD&=~(1<<PD1);PORTD|=(1<<PD2);PORTD|=(1<<PD3);
if(!(PINA&(1<<PA0)))
{PORTB=0XF4;goto delay;}
else if(!(PINA&(1<<PA1)))
{PORTB=0XF5;goto delay;}
else if(!(PINA&(1<<PA2)))
{PORTB=0XF6;goto delay;}
else
//third line
{
PORTD|=(1<<PD0);PORTD|=(1<<PD1);PORTD&=~(1<<PD2);PORTD|=(1<<PD3);
if(!(PINA&(1<<PA0)))
{PORTB=0XF7;goto delay;}
else if(!(PINA&(1<<PA1)))
{PORTB=0XF8;goto delay;}
else if(!(PINA&(1<<PA2)))
{PORTB=0XF9;goto delay;}
else
//forth line
{
PORTD|=(1<<PD0);PORTD|=(1<<PD1);PORTD|=(1<<PD2);PORTD&=~(1<<PD3);
if(!(PINA&(1<<PA0)))
{PORTB=0XFA;goto delay;}
else if(!(PINA&(1<<PA1)))
{PORTB=0XFB;goto delay;}
else if(!(PINA&(1<<PA2)))
{PORTB=0XFC;goto delay;}
else
PORTB=0xff;
}//end forth line
}//end third line
}//end second line
}//end first line
delay: _delay_ms(1000);
}//end while
}//end main |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|