Simple Blink Sketch with registers

enrique

Member
Hi, I'm working on a RF attenuator project and I need fast gpio state change. I know registers is the way to go, but can't get the simple blink sketch work using registers.


This is what I did try:


#define PIN 13
#define MASK (1 << 13)

void setup() {
GPIO6_GDIR |= MASK; // Configurar el pin 13 como salida (GPIO6)
IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // Configurar el pin en modo GPIO
}

void loop() {
// Encender el pin 13
GPIO6_DR_SET = MASK;
delay(500);

// Apagar el pin 13
GPIO6_DR_CLEAR = MASK;
delay(500);

// Alternar el estado del pin
GPIO6_DR_TOGGLE = MASK;
delay(500);
}
 
I know registers is the way to go,

For single pin use, digitalWriteFast() with const pin number compiles to exactly this direct register code. You won't gain any performance, but it will cost you code readability.

Direct register access can sometimes be faster for manipulating multiple pins at the same time.


but can't get the simple blink sketch work using registers.

If you really want to use the registers even though you'll get exactly the same performance as digitialWriteFast(), your code needs to use the correct bitmask. The error is with "#define MASK (1 << 13)".
 
Hi Paul. Finally I make it work with the following code for complete bank 07.
Now I can start selecting which pins change state at different rates for my project

thanks

void setup() {
GPIO7_GDIR = 0xFFFFFFFF;

void loop() {
GPIO7_DR_SET = 0xFFFFFFFF;
delay(100); //

GPIO7_DR_CLEAR = 0xFFFFFFFF;
delay(100); //
}
 
Back
Top