Let's Make Robots!

Direct pin reading/writing instead of digitalRead and digitalWrite

Hi guys, I'm trying to make a bit bang serial connection through mega328 and Arduino seems to lack speed as the connection never happens. Here is the code, so you can better understand what I'm trying to do:

boolean pc2vr;
boolean vr2pc;

void setup() {
    // PC UART Init
  pinMode(0, INPUT);         // sets the digital pin as input
  pinMode(1, OUTPUT);        // sets the digital pin as output
    // VRbot UART Init
  pinMode(3, INPUT);         // sets the digital pin as input
  pinMode(2, OUTPUT);        // sets the digital pin as output
}

//================================================================
// Main Loop
//================================================================
void loop(){
      pc2vr = digitalRead(0);
      digitalWrite(2, pc2vr);   
      vr2pc = digitalRead(3);
      digitalWrite(1, vr2pc);

//================================================================
 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I got help faster from Society of Robots Forum.

Here is the code that works:

#define BIT_SET(REG, bit)                    REG |= (1UL << (bit))
#define BIT_CLR(REG, bit)                    REG &= ~(1UL << (bit))
#define BIT_TST(REG, bit, val)              ( ( (REG & (1UL << (bit) ) ) == ( (val) << (bit) ) ) )

DDRD=B00001010;

while(1){

       if (BIT_TST(PIND,0,1)){
        BIT_SET(PORTD,3);
      }else{
        BIT_CLR(PORTD,3);
      }
      if (BIT_TST(PIND,2,1)){
        BIT_SET(PORTD,1);
      }else{
        BIT_CLR(PORTD,1);
      }
}