Skip to content

Instantly share code, notes, and snippets.

@EllieTheYeen
Created May 20, 2024 03:03
Show Gist options
  • Save EllieTheYeen/6d0e32f117624567a1b87d3a075ada92 to your computer and use it in GitHub Desktop.
Save EllieTheYeen/6d0e32f117624567a1b87d3a075ada92 to your computer and use it in GitHub Desktop.
Arduino port manipulation using struct
struct PBS {
bool D8 : 1;
bool D9 : 1;
bool D10 : 1;
bool D11 : 1;
bool D12 : 1;
bool D13 : 1;
bool DONOTUSE1 : 1;
bool DONOTUSE2 : 1;
} *PB = (PBS *)&(PORTB);
struct PBDS {
bool D8 : 1;
bool D9 : 1;
bool D10 : 1;
bool D11 : 1;
bool D12 : 1;
bool D13 : 1;
bool DONOTUSE1 : 1;
bool DONOTUSE2 : 1;
} *PBD = (PBDS *)&(DDRB);
struct PCS {
bool A0 : 1;
bool A1 : 1;
bool A2 : 1;
bool A3 : 1;
bool A4 : 1;
bool A5 : 1;
bool DONOTUSE1 : 1;
bool DONOTUSE2 : 1;
} *PC = (PCS *)&(PORTC);
struct PCDS {
bool A0 : 1;
bool A1 : 1;
bool A2 : 1;
bool A3 : 1;
bool A4 : 1;
bool A5 : 1;
bool DONOTUSE1 : 1;
bool DONOTUSE2 : 1;
} *PCD = (PCDS *)&(DDRC);
struct PDS {
bool D0 : 1;
bool D1 : 1;
bool D2 : 1;
bool D3 : 1;
bool D4 : 1;
bool D5 : 1;
bool D6 : 1;
bool D7 : 1;
} *PD = (PDS *)&(PORTD);
struct PDDS {
bool D0 : 1;
bool D1 : 1;
bool D2 : 1;
bool D3 : 1;
bool D4 : 1;
bool D5 : 1;
bool D6 : 1;
bool D7 : 1;
} *PDD = (PDDS *)&(DDRD);
void setup() {
PDD->D2 = 1;
PCD->A0 = 1;
PBD->D13 = 1;
}
void loop() {
PD->D2 = 1;
delay(1000);
PC->A0 = 1;
delay(1000);
PB->D13 = 1;
delay(1000);
PD->D2 = 0;
delay(1000);
PC->A0 = 0;
delay(1000);
PB->D13 = 0;
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment