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