2015-05-24 84 views
0

我一直在努力編寫一個非常簡單的Arduino程序,將地址引腳遞增到EPROM,然後通過其他引腳讀取數據。當我無法像增加一個布爾值數組那樣簡單地完成某些操作時(兩個值上的MSB幾乎總是被固定爲1),我的假設是我的代碼必須被搞亂,但是隨後它開始發送兩次應該有很多字符(Serial.println(char);),這在發送字符串時不會發生。然後更糟糕的事情開始發生,例如在編譯期間沒有發現錯誤時,設備完全沒有響應。所以我需要知道的是,我的代碼是壞了還是我的Arduino壞了?我已經試過所有的波特率,這是我得到的一些輸出。注意我在我的代碼中插入了延遲來減慢速度,這並沒有改變。我不得不彈出一個這樣的屏幕,因爲角色不會粘貼到代碼片段中。 Arduino serial outputArduino Uno - 錯誤/加擾串行數據

這裏是我的一半代碼,你會注意到一次只能發送單個字符的循環(因爲這樣可以避免一些瘋狂的行爲)。

/** 
* 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

您發送二進制數的串行端口,而不是將它們轉換爲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); 
+0

我發送ASCII碼很好,但我需要A)能夠增加作爲布爾數組存儲的二進制值(應該很簡單,但顯然不在我的Arduino上),並且B)通過串行連接正確發送數據。我從來沒有遇到過這個問題,而不是一次。事實上,我會去抓一些舊草圖,看看他們做了什麼。 –

+0

好吧,以前的代碼草圖工作正常。 我一直在發送的唯一二進制數字正在與字符串連接,所以不應該傳輸正常嗎?上面的代碼似乎使發送一些數字比它需要的複雜得多。 –

+0

好的,你的解決方案可以工作,但這裏有一個額外的解決方案,我發現我的代碼的一點點更容易: 'String outstr =「PO:」; outstr.concat(d_pins [i]); Serial.println(outstr);' –