Arduino Uno - 错误/加扰的串行数据

时间:2015-05-24 01:40:42

标签: binary arduino arduino-uno eeprom

我一直在努力编写一个非常简单的Arduino程序,它将地址引脚递增到EPROM,然后通过其他引脚读取数据。当我无法做一些像增加布尔值数组这样简单的事情时(两个值上的MSB几乎也总是保持在1),我的假设是我的代码必须搞砸了,但随后它开始发送两次应该发送许多字符(Serial.println(char);),这在发送字符串时没有发生。然后更糟糕的事情开始发生,例如在编译期间没有发现错误时设备完全没有响应。所以我需要知道的是,我的代码被破坏了还是我的Arduino坏了?我已经在所有波特率下尝试过这个,这是我得到的一些输出。请注意,我在代码中插入延迟以减慢速度,这没有任何变化。我不得不拍摄一个屏幕,因为字符不会粘贴在一个片段中。 Arduino serial output

这是我的半生不熟的代码,你会注意到它一次充满了发送单个字符的循环(因为这避免了一些疯狂的行为)。

/**
 * EEPROM Reader/Dumper for EPROMS or EEPROMS.
 * 
 * @version 1.0.0.0
 * @author  Bit Fracture
 * @date    2015.05.23
 */

//Defining the address lines (four additional pins are manual)
int     a_pins[10]  = {2,3,4,5,6,7,8,9,10,11};

//Defining the 8-bit data pins
int     d_pins[8]   = {12,13,14,15,16,17,18,19};

//Store our current address (start at binary 0)
boolean address[10] = {0,0,0,0,0,0,0,0,0,0};

//Store our serial output data (start at binary 0)
boolean data[8]     = {0,0,0,0,0,0,0,0};

void setup()
{
  //Start communication with the computer
  Serial.begin(115200);
  delay(100);

  //Set address pins to output
  for (int i=0; i < sizeof(a_pins); i += 1)
  {
      pinMode(a_pins[i], OUTPUT);

      //Tell Serial this pin's status
      Serial.println("PO: " + (a_pins[i]));
      delay(100);
  }

  //Set data pins as input
  for (int i=0; i < sizeof(d_pins); i += 1)
  {
      pinMode(d_pins[i], INPUT);

      //Tell Serial this pin's status
      Serial.println("PI: " + (d_pins[i]));
      delay(100);
  }

  //Warn start
  Serial.println("BEGINNING TRANSMISSION");
}

void loop()
{
  //Calculate new binary address
  boolean carry = 1; //Start with a carry to initiate increment process
  for (int i=0; i < sizeof(address); i += 1)
  {
    boolean _temp = ((address[i] || carry) && (!address[i] || !carry));
    carry         = (address[i] && carry);
    address[i]    = _temp;
  }

  //Set output pins to new values
  for (int i=0; i < sizeof(a_pins); i += 1)
  {
    digitalWrite(a_pins[i], address[i]);
  }

  //Allow for the changes to propagate through the EPROM circuitry
  delay(250);

  //Read the inputs
  for (int i=0; i < sizeof(d_pins); i += 1)
  {
    data[i] = digitalRead(d_pins[i]);
  }

  //Output the address
  for (int i = sizeof(address); i > 0; i--)
  {
    if (address[i] == 1)
      Serial.print("1");
    else
      Serial.print("0");
  }
  Serial.print(": ");

  //Output the value
  for (int j = sizeof(data); j > 0; j--)
  {
    if (data[j] == 1)
      Serial.print("1");
    else
      Serial.print("0");
  }
  Serial.println();

  //Keep things from going too fast for now
  delay(1000);
}

简单地说:我需要理解为什么开头的串行数据看起来像是跑出程序存储器而不是发送它应该发送的字符串,为什么我似乎无法像增量一样简单地做一些事情一个二进制数,并在串口输出!

谢谢大家

1 个答案:

答案 0 :(得分:1)

您正在向串口发送二进制数而不将它们转换为ascii,通过将二进制数转换为ascii来对设置代码进行此小改动,

char my_buffer_ax[10];
char my_buffer[200];


  memset(my_buffer, 0, 200);
  strcat(my_buffer, "PO: "); 
  //Set address pins to output
  for (int i=0; i < sizeof(a_pins); i += 1)
  {
      pinMode(a_pins[i], OUTPUT);

      //Tell Serial this pin's status
      memset(my_buffer_ax, 0, 10);
      itoa(a_pins[i], my_buffer_ax, 10); 
      strncat(my_buffer, my_buffer_ax, strlen(my_buffer_ax)); 
      delay(100);
  }
  Serial.println(my_buffer);

  memset(my_buffer, 0, 200);
  strcat(my_buffer, "PI: "); 
  //Set data pins as input
  for (int i=0; i < sizeof(d_pins); i += 1)
  {
      pinMode(d_pins[i], INPUT);

      //Tell Serial this pin's status
      memset(my_buffer_ax, 0, 10);
      itoa(d_pins[i], my_buffer_ax, 10); 
      strncat(my_buffer, my_buffer_ax, strlen(my_buffer_ax)); 
      delay(100);
  }
  Serial.println(my_buffer);