Unclear on how to use DDRx and PORTx (teensy 3.2)

Status
Not open for further replies.

Xecuter

Member
Hello everyone,

I wanted to figure out how to manipulate the registers directly in an effort to shave off some bytes from my binary.
My first goal is simply to blink the LED on pin 13 on and off. If I'm reading the schematics right, pin 13 is on PORT C, 5th bit.

As such, here is my code:
Code:
void setup() {
	DDRC = (1 << 5);
}

void loop() {
	PORTC |= (1 << 5);
	delay(100);
	PORTC &= ~(1 << 5);
	delay(100);
}

From what I understand, this should set up the correct register to output, and then subsequently alternate that same register between high and low with a 100 millisecond delay in between.
I can't figure out with this isn't working. Any clue on what i'm doing wrong?

Thank you!
 
I recommend using pinMode() in setup and digitalWriteFast() loop. Readable code is usually far more important, and digitalWriteFast() compiles to the same code as the most efficient direct register access.

But if you *really* want to access registers directly, maybe this can help?

https://forum.pjrc.com/threads/1753...PORT-DDR-D-B-registers-vs-ARM-GPIO_PDIR-_PDOR

You should probably also be aware those registers DDRC and PORTC are from AVR. On Teensy 3.2 they are emulated with software using C++ classes with operator overloading, so you're *NOT* directly accessing hardware when you use those AVR register names on Teensy 3.2.
 
Status
Not open for further replies.
Back
Top