Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

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

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Posts
    7

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

    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!

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    2,810
    use the toggle register


    Code:
    GPIOC_PTOR = 32;

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,696
    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/17532...PIO_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.

  4. #4
    Junior Member
    Join Date
    Dec 2016
    Posts
    7
    Thank you both for your answers. This helps tremendously.

  5. #5
    Junior Member
    Join Date
    Nov 2017
    Posts
    10
    I've used this successfully on the teensyLC. you might need to change the registers according to the manual of the teensy3.2
    https://github.com/asura6/Teensy-LC-...l_blink/main.c

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •