简体   繁体   中英

Arduino UNO loops Serial unexpectedly, when doing more complex programs

I have a piece of code like this:

#include<LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void leftShift(bool toShift[28], int noOfShifts, bool destination[28]) {

  for (int i = 0; i < 28; i++) {
    destination[(i - noOfShifts + 28) % 28] = toShift[i];
  }
}



void divideBinary(bool binary[], size_t sizeOfBinary, bool LB[], bool RB[]) {
  size_t half = sizeOfBinary / 2;

  // LB - first half
  size_t i = 0;
  for (; i < half; i++) {
    LB[i] = binary[i];
  }

  // RB - second half
  for (; i < half * 2; i++) {
    RB[i - half] = binary[i];
  }
}

void createSubkeys(bool binaryKey[8 * 8], bool subkeys[16][48]) {
  Serial.println("just entered subkeys"); Serial.flush();
  int pc_1[56] = {
    57, 49, 41, 33, 25, 17,  9,
    1, 58, 50, 42, 34, 26, 18,
    10,  2, 59, 51, 43, 35, 27,
    19, 11,  3, 60, 52, 44, 36,
    63, 55, 47, 39, 31, 23, 15,
    7, 62, 54, 46, 38, 30, 22,
    14,  6, 61, 53, 45, 37, 29,
    21, 13,  5, 28, 20, 12,  4
  };
  bool keyPermutation[56];


  // according to pc_1 create from 64-bit key 56-bit keyPermutation
  for (int i = 0; i < 56; i++) {
    keyPermutation[i] = binaryKey[pc_1[i] - 1];
  }

  // C and D will be saved here: [C/D] [index] [28 bools]
  bool CD[2][16 + 1][56 / 2];
  Serial.println("CD ready"); Serial.flush();
  // divide keyPermutation into halves to C0 a D0 - each consists of 28 bits
  divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);

  // from C0, D0 and shifts make C1, D1 -> C16, D16
  int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
  for (int i = 1; i < 17; i++) {
    leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
    leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
  }

  // each subKey out of 16 is made from one out of 16 CD with the use of pc_2
  int pc_2[48] = {
    14,    17,   11,    24,     1,    5,
    3,    28,   15,     6,    21,   10,
    23,    19,   12,     4,    26,    8,
    16,     7,   27,    20,    13,    2,
    41,    52,   31,    37,    47,   55,
    30,    40,   51,    45,    33,   48,
    44,    49,   39,    56,    34,   53,
    46,    42,   50,    36,    29,   32
  };

  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 48; j++) {

      // find out which part of CD we should look at - that means C, or D? for C CorD is 0, for D 1
      int where = pc_2[j] - 1;
      bool CorD = 0;
      if (where >= 56 / 2) {
        CorD = 1;
        where -= 56 / 2; // subtract 28, to start indexing from 0 again in case of D
      }

      subkeys[i][j] = CD[CorD][i + 1][where];
    }
  }

//  Serial.println("subkeys ready");
}
void setup() {
  // put your setup code here, to run once:

   Serial.begin( 9600 );

  lcd.begin(16, 2);
  Serial.println("ready"); Serial.flush();
  bool binaryKey[8 * 8];
  bool subkeys[16][48];
  createSubkeys(binaryKey, subkeys);

}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0,0);
  lcd.print("haf");
}

It is not really important what it does, it is just so you can roughly see its complexity.

Why won't this work on Arduino, even if it were to be much slower? Instead, when I run it, my Arduino UNO really behaves weirdly. In Serial it just repeats a sequence of characters "jready" in a loop. Forever. It never prints anything else and it never reaches the loop() function.

My Arduino and its Serial both work perfectly fine for smaller programs.

EDIT: If I attempt to locate the problem by commenting out blocks of code, it seems to occur here:

  // from C0, D0 and shifts make C1, D1 -> C16, D16
  int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
  for (int i = 1; i < 17; i++) {
    leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
    leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
  }

But if I make my setup() function more complex, it happens earlier, on this line:

  divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);

You are passing variables to methods without correctly specifying whether they should return that value which you modify inside that method. Look at:

void leftShift(bool toShift[28], int noOfShifts, bool destination[28])

You modify destination in there but destination is boolean array copied to the method but when the method finishes that modified value is not returned to the caller. Change your declarations.

Here:

bool CD[2][16 + 1][56 / 2];

You have a third dimension to the array but never use it. The third dimension has 28 elements but you only ever use the second dimension. If you are trying to do pointer operations then you will have to change leftShift

Also consider the above points I made with divideBinary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM