简体   繁体   English

用>> 1移出寄存器

[英]Bit Shifting out of register with >>1

I am using an Atmega 16 on a STK500 Programmer Board. 我正在STK500编程器板上使用Atmega 16。 im Trying to Turn on the leds on DDRA starting on Led0 with 0b00000001 to Led7 with 0b1000000. im正在尝试打开从0b00000001的Led0到0b1000000的Led7的DDRA指示灯。 It seems like i push the set bit out of the register with a >> shift. 好像我用>>移位将设置位从寄存器中推出。 Shouldnt it just move 1 to the right? 它不应该只向右移动1吗? I have this snippet 我有这个片段

# define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/io.h>

int main(void)
{
    DDRA=0xFF;
    char leds=0x01;
    while(1)
    {
        if (leds==0x01)
        {
            for (int i=0;i<8;i++)
            {
                PORTA=~leds;
                leds=leds<<1;
                _delay_ms(300);
            }
        }
        else
        for (int x=0;x<8;x++)
        {
            leds=leds>>1;
            PORTA=~leds;
            _delay_ms(300);
        }

    }
}

it seems like this part 好像这部分

for (int x=0;x<8;x++)
            {
                leds=leds>>1;
                PORTA=~leds;
                _delay_ms(300);
            }

pushes the bit out of the register, but it should not. 将该位从寄存器中压出,但不应这样做。 am i making a mistake? 我在弄错吗?

You are shifting the 1 out in the first loop. 您将在第一个循环中移出1。 And you are not seeing because you update the display before the shift. 而且您没有看到,因为您在班次之前更新了显示。

At the end of the iteration with i = 0, leds will be equal to 0x02. 在i = 0的迭代结束时,led将等于0x02。 So the following this logic you get: 因此,您将获得以下逻辑:

i, leds (end of the loop)
0, 0x02
1, 0x04
2, 0x08
3, 0x10
4, 0x20
5, 0x40
6, 0x80
7, 0x00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM