user85812: I am using a custom Arduino Nano compatible board with an Atmel 328P.  I am externally changing the values applied to several pins on PORTB.  I expect to be able to read the values of these pins (low or high) but I always read zeroes.  However, the pin change interrupt is called.  What do I need to do to read values from PORTB?
Supply voltage is 5 volts.  Pin applied high voltage is 3.3 volts.
With the below code, when I change the voltage applied to a pin, LED1 flashes but LED2 does not.
#include <avr/interrupt.h>

#define LED1_PORT                           (PORTD)
#define LED1_PIN_bm                         (_BV(4))         //D1, white

#define LED2_PORT                           (PORTD)
#define LED2_PIN_bm                         (_BV(3))         //D2, red

volatile bool g_bValueChanged = 0;

void setup()
{
  DDRB = 0;                     //set all as inputs
  PCMSK0 = 0xfb;                //exclude 1 pin because of noise
  PCICR |= (1 << PCIE0);        //enable pin change interrupt 0 (for pins PB0..7)

  sei();
}

uint8_t g_valuePrevious = 0;

void loop()
{
  if (g_bValueChanged)
  {
    g_bValueChanged = false;

    LED1_PORT |= LED1_PIN_bm;
    delay(40);
    LED1_PORT &= ~LED1_PIN_bm;
    delay(40);
  }

  uint8_t value = PORTB;
  if (value != g_valuePrevious)
  {
    g_valuePrevious = value;
    
    LED2_PORT |= LED2_PIN_bm;
    delay(40);
    LED2_PORT &= ~LED2_PIN_bm;
    delay(40);
  }
}

ISR(PCINT0_vect)
{
    g_bValueChanged = true;
}


user7508: The PORTB register is initialized by the hardware to zero. You never
write to this register, so it always stays zero.
Maybe you are mixing up PORTB and PINB:

PORTB is the port output register, controlled by you
PINB is the port input register, for which the bits
corresponding to input pins are controlled by whatever drives those
pins from the outside.


85812: <p>I am using a custom Arduino Nano compatible board with an Atmel 328P.  I am externally changing the values applied to several pins on PORTB.  I expect to be able to read the values of these pins (low or high) but I always read zeroes.  However, the pin change interrupt is called.  What do I need to do to read values from PORTB?</p>
<p>Supply voltage is 5 volts.  Pin applied high voltage is 3.3 volts.</p>
<p>With the below code, when I change the voltage applied to a pin, LED1 flashes but LED2 does not.</p>
<pre><code>#include &lt;avr/interrupt.h&gt;

#define LED1_PORT                           (PORTD)
#define LED1_PIN_bm                         (_BV(4))         //D1, white

#define LED2_PORT                           (PORTD)
#define LED2_PIN_bm                         (_BV(3))         //D2, red

volatile bool g_bValueChanged = 0;

void setup()
{
  DDRB = 0;                     //set all as inputs
  PCMSK0 = 0xfb;                //exclude 1 pin because of noise
  PCICR |= (1 &lt;&lt; PCIE0);        //enable pin change interrupt 0 (for pins PB0..7)

  sei();
}

uint8_t g_valuePrevious = 0;

void loop()
{
  if (g_bValueChanged)
  {
    g_bValueChanged = false;

    LED1_PORT |= LED1_PIN_bm;
    delay(40);
    LED1_PORT &amp;= ~LED1_PIN_bm;
    delay(40);
  }

  uint8_t value = PORTB;
  if (value != g_valuePrevious)
  {
    g_valuePrevious = value;
    
    LED2_PORT |= LED2_PIN_bm;
    delay(40);
    LED2_PORT &amp;= ~LED2_PIN_bm;
    delay(40);
  }
}

ISR(PCINT0_vect)
{
    g_bValueChanged = true;
}
</code></pre>
 7508: <p>The <code>PORTB</code> register is initialized by the hardware to zero. You never
write to this register, so it always stays zero.</p>
<p>Maybe you are mixing up <code>PORTB</code> and <code>PINB</code>:</p>
<ul>
<li><code>PORTB</code> is the port <strong>output</strong> register, controlled by you</li>
<li><code>PINB</code> is the port <strong>input</strong> register, for which the bits
corresponding to input pins are controlled by whatever drives those
pins from the outside.</li>
</ul>
