I’m using a breadboarded ATmega328PB with platformio. I’m able to address pins in native code no problem, but using pin labels such as PD5, PD6 etc in Arduino code has no effect, i.e. the code compiles correctly but the expected outputs do not occur. PD7, strangely, does work. Can anyone help? This is driving me even more mad than I was to start with.
This is platformio.ini (including irrelevant bits for completeness):
[env:atmega328pb]
platform = atmelavr
board = atmega328pb
framework = arduino
board_build.mcu=atmega328pb
board_build.f_cpu=8000000L
upload_protocol=stk500v1
upload_port = com10
upload_speed = 19200
upload_flags = -P$UPLOAD_PORT -b$UPLOAD_SPEED
This is the native code that works:
#include <avr/io.h>
#include <util/delay.h>
#define INTERVAL 100
void lights();
int main(void) {
DDRB = 1 << 1; // make the RED LED pin an output for PORTD5
DDRC = 1 << 1; // make the YELLOW LED pin an output for PORTD6
DDRD = 1 << 7; // make the GREEN LED pin an output for PORTD7
while (1)
{
lights();
_delay_ms(INTERVAL);
lights();
_delay_ms(INTERVAL);
}
}
void lights() {
PORTB ^= 1 << 1;
PORTC ^= 1 << 1;
PORTD ^= 1 << 7;
}
This is the Arduino code that doesn’t work (except PD7):
#include <arduino.h>
#define GREENLED PD7
#define REDLED PD6
#define YELLOWLED PD5
#define INTERVAL 100
void lights();
void setup() {
pinMode(GREENLED, OUTPUT);
pinMode(REDLED, OUTPUT);
pinMode(YELLOWLED, OUTPUT);
}
void loop() {
lights();
delay(INTERVAL);
lights();
delay(INTERVAL);
}
void lights() {
digitalWrite(GREENLED, !digitalRead(GREENLED));
digitalWrite(REDLED, !digitalRead(REDLED));
digitalWrite(YELLOWLED, !digitalRead(YELLOWLED));
}