1

I'm trying to write PORTB and PORTD in ATmega328P using byte system like this,

    //  PD7  PD6  PD5  PD4  PD3  PD2  PD1  PD0
    //  N     N    N    N    N    U    U    U
    //  7     6    5    4    3    2    1    0
    //======================================
    //  PB7  PB6  PB5  PB4  PB3  PB2  PB1  PB0
    //  X     X    1    1    1    1    1    1
    //  C     C    13   12   11   10   9    8
    //======================================
    //C-Crystal Pins/U-Un used/N-Used/X-unknown value

    byte pins[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

    //uint16_t style1=1365; //10101010101
    //uint16_t style2=682;  //01010101010

    void setup() {
      for (int i = 0; i < 11; i++) {
        pinMode(pins[i],OUTPUT);
        PORTD=B00000000;
        PORTB=B00000000;
      }

    }

    void loop() {
      PORTD=B10101000;
      PORTB=B00101010;
      delay(1000);
      PORTD=B01010000;
      PORTB=B00010101;

    }

According to this , Port value should be swapped. But, It's still remaining given value of the code before delay()

What is the problem in this code?

5

What is the problem in this code?

No problem, it does change the bits of port D and B from 0B10101000 and 0B00101010 to 0B01010000 and 0B00010101 respectively.

However, microseconds later loop() runs again and changes them back to 0B10101000 and 0B00101010 for a full second.

To better see what's happening, add a second delay(1000); statement, placing it just before the closing brace.

  • Ops,,,I really know about that.But,I have forgot that.,Im very mad. Thank you. – user_fs10 Apr 27 '17 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.