Go Down

Topic: PORT Manipulation bitshifting error (Read 68 times) previous topic - next topic

vertamps

Using my arduino as an ISP i have programmed an Attiny45 to cycle up and down flipping the outputs (PB0-PB4) high and low lighting up LEDs in sequence.

My question: How does 
Code: [Select]
  PORTB = (1 << x);  enable a PortB pin high if 'x is port zero, at logic low and there is no "1/high" bit to bitshift onto the byte/bit? Isnt it logically shifting in a zero out of thin air? I even tried setting the PortB register all to zero in the beginning of my code but it lights the LED's regardless.
Code: [Select]

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    int x = 0;
    DDRB = 0b00111111;
    PORTB = 0b00000000;
    while(1){

        while (x < 4){
            PORTB = (1 << x);
            _delay_ms(100);
            x ++;
        }
        while (x > 0){
            PORTB = (1 << x);
            _delay_ms(100);
            x--;
        }


        }
}


GoForSmoke

#1
Oct 13, 2014, 12:41 pm Last Edit: Oct 13, 2014, 12:48 pm by GoForSmoke Reason: 1
1 << 0 equals 1

That is 1, left-shifted 0 times.

More bits about bits:
http://playground.arduino.cc/Code/BitMath

Quote
I even tried setting the PortB register all to zero in the beginning of my code but it lights the LED's regardless.


Without any delay it can be hard to see that all-zeros start.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up