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

Thread: Read Full Register

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    2

    Read Full Register

    Hi, I'm new to teensy and this forum and I'm looking for some help regarding my Teensy 3.5 project. When reading the "How-to Tips" the section "Using I/O pins" talks about reading a full register (8bit) which I would like to use for my project (reading external hardware which offers a byte interface). Unfortunately I have not found anything (yet) to read a full register (8, 16, 32 bit) with one command for the teensy 3.5. When scanning the "Teensy 3.5 & 3.6 Schematic" it looks to me that the registers and pins are not used in a consecutive way - but I might be wrong. Right now I have a working solution at bit basis:

    byte read_byte() {
    byte b = 0;
    b |= digitalRead(A0)<<0;
    b |= digitalRead(A1)<<1;
    b |= digitalRead(A2)<<2;
    b |= digitalRead(A3)<<3;
    b |= digitalRead(A4)<<4;
    b |= digitalRead(A5)<<5;
    b |= digitalRead(A6)<<6;
    b |= digitalRead(A7)<<7;
    return b;
    }

    which does not look very efficient to me.

    Thanks!

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Quote Originally Posted by Wilfried View Post
    When scanning the "Teensy 3.5 & 3.6 Schematic" it looks to me that the registers and pins are not used in a consecutive way - but I might be wrong.
    You're right. You can't just read a register.

    Quote Originally Posted by Wilfried View Post
    byte read_byte() {
    byte b = 0;
    b |= digitalRead(A0)<<0;
    b |= digitalRead(A1)<<1;
    b |= digitalRead(A2)<<2;
    b |= digitalRead(A3)<<3;
    b |= digitalRead(A4)<<4;
    b |= digitalRead(A5)<<5;
    b |= digitalRead(A6)<<6;
    b |= digitalRead(A7)<<7;
    return b;
    }
    That's not as bad as you might think. If you use digitalReadFast instead, the compiler can optimize it very good.
    which does not look very efficient to me.
    Don't think. Measure the time - you'll see - its better than it looks like

    Edit: You can use different pinnumbers - if I remember correctly, there is a least one full 8bit port. In this case, you can use a register-read. Take a look at the schematics!
    Edit: For example, PortC.

  3. #3
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679

  4. #4
    Junior Member
    Join Date
    Oct 2018
    Posts
    2
    Hi Frank - Thank you for that quick and very informative reply. This really helps.

    Wilfried

Posting Permissions

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