现在的位置: 首页 > 综合 > 正文

avrstudio 5 按键控制led移位

2013年07月08日 ⁄ 综合 ⁄ 共 727字 ⁄ 字号 评论关闭

关键点:

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);
}

截图:

2011-3-26-15-49

抱歉!评论已关闭.