Using digitalWrite and Read with Port Manipulation

Discussion in 'Embedded Systems and Microcontrollers' started by Abdel_Rahman, Jul 22, 2016.

  1. Abdel_Rahman

    Thread Starter Member

    Jul 19, 2016
    36
    0
    Can I use both direct functions digitalWrite and digitalRead along with port manipulation in the same code
    Such as:

    void setup() {
    pinMode(2, INPUT); pinMode(3, INPUT);
    }

    void loop() {
    if ( PINE & B00100000 ) {//dosomething
    }}
    and does that have a positive effect on the arduino speed as I expect?
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,375
    494
    I don't see how.
     
  3. DickCappels

    Moderator

    Aug 21, 2008
    2,647
    631
    Are you hoping this code is faster than some other code?

    If so, please show us the other code.
     
    Abdel_Rahman likes this.
  4. dannyf

    Well-Known Member

    Sep 13, 2015
    1,767
    355
    Yes.

    Depends on what you meant by "positive".
     
    Abdel_Rahman likes this.
  5. Abdel_Rahman

    Thread Starter Member

    Jul 19, 2016
    36
    0
    Thanks for Reply :)

    The Other Code is:

    void setup() { pinMode(2, INPUT); pinMode(3, INPUT);}
    void loop() {if ( digitalRead(3)) {//dosomething}}

    I know that Port manipulation is faster than using digitalRead() and digitalWrite() functions. isn't it??
     
  6. m zaid

    Member

    Jan 9, 2016
    46
    5
    It pretty much depends on the compiler for how is the translation of your C code to assembly. Coding by assembly is using the specific and the most basic instructions a microcontroller can support. Each instruction takes a few oscillator cycle to know how fast.

    For example for a 8 bit PIC microchip device, the best you could do for a 'digital read and test of a bit' is using the single instruction 'btfsc' which reads bit test file skip next line if clear. If you were to read the port and compare it to a literal byte, you'd be doing more assembly i.e.
    movlw 0x20
    andwf PINE,0
    movwf buffer
    btfsc buffer,5
    goto FALSE
    .
    .

    See a microcontroller's datasheet about it's instruction set. Page 55.
     
    Last edited: Jul 23, 2016
  7. Abdel_Rahman

    Thread Starter Member

    Jul 19, 2016
    36
    0
    Its my mistake, I am talking about arduino, the thread really in need for more clarifying.
     
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,767
    355
    Yeah. Per my test (https://dannyelectronics.wordpress.com/2016/05/01/the-price-of-avr-arduino/), the speed gains from using register access ranges from 20x - 70x. The arduino team could have done a much better job there.

    For STM32-based Arduino, the speed penalty isn't as much: 5 - 20x (https://dannyelectronics.wordpress.com/2016/04/30/the-price-of-stm32-arduino/).
     
    4WorX and Abdel_Rahman like this.
  9. Abdel_Rahman

    Thread Starter Member

    Jul 19, 2016
    36
    0
    Thanks, That is what i am searching for.

    I don't understand what you mean by STM32, is arduino mega STM32-based??
     
  10. 4WorX

    New Member

    Jul 27, 2016
    1
    1
     
    Abdel_Rahman likes this.
Loading...