8bit port scan and send has weird behaviour?

Status
Not open for further replies.

pix-os

Well-known member
i am currently trying to write to an 8bit port on my arduino mega, and am trying to translate it back over serial using a teensy.

since the teensy 3.2 has 5V tolerance, i wired the pins directly to each oher, and interconnected ground as well.

i am getting the right input check of command/data (which i set by a pin toggle)
the output is always 1 the first time (as should), but then it immediately goes to 3 and repeats itself each time i try to send new data..

anyone has an idea what i might be doing wrong?

code arduino mega:
Code:
char command;
const uint8_t portpins[8]= {3,4,5,6,7,8,9,10};
const uint32_t controlpin_data_command = 2;
const uint32_t controlpin_trigger = 1;
const uint32_t controlpin_shift = 0;

inline void send_command(char c) {
  //set command
  digitalWrite(controlpin_data_command,HIGH);

  //set only 8bit
  digitalWrite(controlpin_shift,LOW);

  //set pins
  digitalWrite(portpins[0],bitRead(c,0));
  digitalWrite(portpins[1],bitRead(c,1));
  digitalWrite(portpins[2],bitRead(c,2));
  digitalWrite(portpins[3],bitRead(c,3));
  digitalWrite(portpins[4],bitRead(c,4));
  digitalWrite(portpins[5],bitRead(c,5));
  digitalWrite(portpins[6],bitRead(c,6));
  digitalWrite(portpins[7],bitRead(c,7));

  //trigger the command
  digitalWrite(controlpin_trigger,!digitalRead(controlpin_trigger));
}

void setup() {
  // put your setup code here, to run once:
  for (uint8_t i = 0; i < 8; i++) {
    pinMode(portpins[i], OUTPUT);
  }
  pinMode(controlpin_trigger, OUTPUT);
  pinMode(controlpin_shift, OUTPUT);
  pinMode(controlpin_data_command, OUTPUT);
  command = 0;
  send_command(command);
}

void loop() {
  delay(5000);
  command++; 
  send_command(command);
  // put your main code here, to run repeatedly:

}


and the code for the teensy 3.2:
Code:
const uint32_t portpins[8]= {3,4,5,6,7,8,9,10};
const uint32_t controlpin_data_command = 2;
const uint32_t controlpin_trigger = 1;
const uint32_t controlpin_shift = 0;

volatile bool read_buffer[2][8]; //max 2x8bit (aka 16bit)
volatile bool busy_reading;
volatile bool has_data[2]; //1x lower bit, 1x higher bit
volatile bool command_or_data;
volatile bool shift = 0;
volatile char read_buffer_char[2]; //the char made out of 8 bits

void clock_isr() {
  busy_reading = 1;
  command_or_data = !digitalReadFast(controlpin_data_command);  
  if (digitalReadFast(controlpin_shift)) {
    shift = 1;
    //get the higher 8 bits
    for (uint8_t xxx = 0; xxx < 8; xxx++) {
      read_buffer[1][xxx] = digitalReadFast(portpins[xxx]);
      bitSet(read_buffer_char[1],read_buffer[1][xxx]);
      has_data[1] = 1;
    }
  } else {
    shift = 0;
    for (uint8_t xxx = 0; xxx < 8; xxx++) {
      read_buffer[0][xxx] = digitalReadFast(portpins[xxx]);
      bitSet(read_buffer_char[0],read_buffer[0][xxx]);
      has_data[0] = 1;
    }
  }
  busy_reading = 0;
}

void setup() {
  delay(1000);
  Serial.begin(9600);
  // put your setup code here, to run once:
  for (uint8_t i = 0; i < 8; i++) {
    pinMode(portpins[i], INPUT);
  }
  pinMode(controlpin_data_command, INPUT);
  pinMode(controlpin_trigger, INPUT);
  pinMode(controlpin_shift, INPUT);
  attachInterrupt(digitalPinToInterrupt(controlpin_trigger),clock_isr,CHANGE);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(5);
  noInterrupts();
  if (!busy_reading) {
    //done reading port..
    if (has_data[0]) {
      if (command_or_data) {
        Serial.println("Found command.");
      } else {
        Serial.println("Found data.");
      }
      Serial.print("lower char: ");
      Serial.println(read_buffer_char[0],HEX);
      read_buffer_char[0] = 0;
      has_data[0] = 0;
    }
    if (has_data[1]) {
      if (command_or_data) {
        Serial.println("Found command.");
      } else {
        Serial.println("Found data.");
      }
      Serial.print("higher char: ");
      Serial.println(read_buffer_char[1],HEX);
      read_buffer_char[1] = 0;
      has_data[1] = 0;
    }
  }
  interrupts();
}

thanks in advance.

______________________________________-

EDIT: i found the solution to my problem i was trying to solve after a day, here's the solution in it's simplest form:

i initially used BitSet, which sets the bit to 1, regardless of the digitalread

so i needed to change that to BitWrite.

also, i changed the code alot (removed shifting code for 16bit stuff as well)

as i can live with an 8bit interface as well :)


the output is now:
Found command.
char: 1
Found command.
char: 2
-
-
Found command.
char: 4A
Found command.
char: 4B


and so on.
 
Last edited:
Status
Not open for further replies.
Back
Top