关键点:
1.按键的释放判断为if(PINC!=flag)这个flag存有先前的按键值
2.i=(i-/+1)&0x07巧妙的利用了无符号溢出的作用
3.在我们设置了pc口的输入时,在按键按下后再释放的之后我们会发现我们的pinc的值为0xff,是因为我们设置了上拉电阻,和pc口的数据为0xff;
源代码:
#include
#include
#define F_CPU 8000000UL
char i,j;
void move_step();
int main(void)
{
char flag=0x00;
DDRE = 0xff;DDRF = 0xff;
DDRC = 0x00;PORTC = 0xff;
while(1)
{
if(PINC!=flag)
{
flag=PINC;
move_step();
_delay_ms(200);
}
}
}
void move_step()
{
if ((PINC&0x01)==0x00)
{
i=(i-1)&0x7;
}
else if((PINC&0x02)==0x00)
{
i=(i+1)&0x07;
}
else if((PINC&0x04)==0x00)
{
j=(j-1)&0x7;
}
else if((PINC&0x08)==0x00)
{
j=(j+1)&0x7;
}
PORTE=~(1<
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』