关键点:

1.按键的释放判断为if(PINC!=flag)这个flag存有先前的按键值

2.i=(i-/+1)&0x07巧妙的利用了无符号溢出的作用

3.在我们设置了pc口的输入时,在按键按下后再释放的之后我们会发现我们的pinc的值为0xff,是因为我们设置了上拉电阻,和pc口的数据为0xff;

源代码:

#include <avr/io.h>
#include <util/delay.h>
#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<<i);
    PORTF=~(1<<j);
}

截图:

avrstudio 5 按键控制led移位

相关文章:

  • 2021-08-21
  • 2021-12-15
猜你喜欢
  • 2021-12-29
  • 2021-12-08
  • 2021-11-01
  • 2022-01-17
  • 2021-07-31
相关资源
相似解决方案