Port manipulation latching output of one pin the input of another

Hi. I am looking to for an output of a pin to mimic the input of another as quick as possible so as not to be out of sync.

The simple way:
byte x=digitalRead(pinA);
digitalWrite(pinB,x);

Will take up too much time.

Is there any way to do:
The output of PORTB2 = the input of PORTB3 through bitwise operations? Port manipulation?

An example would be very helpful. Thanks

Try

digitalWrite(pinB, digitalRead(pinA));


Or just Google Arduino port manipulation.

digitalWrite(pinB, digitalRead(pinA));
Is to slow

I do use port manipulation from time to time. But I do not see how to do "bit of portB2 = to bit of portB3.

I only know how to manipulate bits in a register but not one bit based on another bit.

Thanks

Hello,

Try something like this PORTB |= ( ( PORTB >> 3 ) & 1 ) << 2;

Hi,
What model Arduino are you using?

Tom... :smiley: :+1: :coffee: :australia:

Atmega328p bare

Basically looking to do digitalWrite(1,digitalRead(2));
using purely bitwise operations and with the least instructional steps as possible
Thanks

Can you please break it down for me so I know how to apply it?

PINB, PINC and PIND reads ports B,C,D.

Use a mask for the pin you want to read.

K will need to brush up on bit masking. Thanks

PINB & (1 << 4) Pin D12 on the UNO.


digitalWrite(11, PINB & (1 << 4)); // pin 11 set to the level read on pin 12

Check the image for port to pin relationships.

In this case I am still using digitalWrite() which has some latency compared to direct port manipulation.

Direct example:

digitalWrite(8,digitalRead(2));

Which is:
digitalWrite(PB0,digitalRead(PD2));

How would I do THIS specific instruction using ONLY bitwise commands?

Can we stop for a minute, tell us what the timing constraints that are needed to be met ?

What exactly are you needing to to ?

I forgot something, bit 2 need to be cleared first. I can't test now, but try :

uint8_t pinb3 = ( PINB >> 3 ) & 1; // get bit 3 of PINB
uint8_t portb = PORTB;
portb &= ~( 1 << 2 ); // clear bit 2
portb |= pinb3 << 2; // set bit 2 to PINB3
PORTB = portb; // update PORTB

Interfacing with a 45khz signal of another chip. Want to intercept and redirect. Timing has to be perfect. I might have to use interrupts..(i know).
At least want to minimize or eliminate any time constraints with the actual input / output pin read / write.

Designing specialzed remote for hospital bed. Original remote is using a multiplex method to read many buttons.
Output A b c d
Input. 1 2 3 4

  1. Outputs signal on A

  2. Monitors inputs 1234

  3. If let's say a specific button shorts A to 2, the chip knows that by knowing that output A is active and 2 is pulled up.

  4. Outputs signal on B

  5. Monitors inputs 1234

  6. If let's say button connects B to 2, chip knows that by knowing that output B is active and 2 is pulled up.

....and so on

A to B to C to D is at 45khz. I will double check with oscilloscope.

I am trying to emulate that by picking up which pin is being currently being polled and if a certain pin is expected for a certain action, pull up that corresponding pin

bitWrite(bitRead() ) ; ?

The code provided to you by @guix in #16 is about as fast as things will go.

You need to realize your loop code is operating on other events.

Interrupts might help but have their own problems.

Interfacing with existing circuits is difficult at the best of times.

Arduino and health applications is not sanctioned by the makers of the controller chip.

Yes, this will be challenging to get right. A polling frequency if 45kHz means you have maybe 1 or 2 us to respond, depending on the timings of the host device. That's hard to get right in an arduino where other tasks (particularly interrupts) may be active.
I spent wa quite some time getting the timings right for a OneWire interface, which also relies on microsecond-scale timings. I found it quite challenging and the end result, while it works, isn't 100% reliable.