2017-05-04 39 views
-2

我是新的C和我創造了一些代碼,不工作...複製多維列C

我得到一個警告,而initLetterLib():整數轉換導致截斷

我嘗試memcpy我的libraryLetter到我的outputLED,但它不起作用。 我只是得到0x00到我的outputLED。 我試圖在outputLED中複製別的東西 - 這真的很好。 但我不明白爲什麼有我的libraryLetters一個問題...

#define LETTER_WIDTH 6 

    typedef unsigned char letter[LETTER_WIDTH]; 

    letter libraryLetters[128]; 

    void initLetterLib(){ 
    *libraryLetters[0x20] = 0x000000000000; // Blank 
    *libraryLetters['A'] = 0xFE909090FE00; 
    *libraryLetters['H'] = 0xFE101010FE00; 
    *libraryLetters['L'] = 0xFE0202020200; 
    *libraryLetters['O'] = 0xFE828282FE00; 
    *libraryLetters['U'] = 0xFE020202FE00; 
    *libraryLetters['R'] = 0xFE9894946200; 
    *libraryLetters['Z'] = 0x868A92A2C200; 
    *libraryLetters['I'] = 0x0000FE000000; 
    *libraryLetters['F'] = 0xFE9090808000; 
} 

    // takes a String and generates the outputsequence for LEDs 
    unsigned char * stringToLEDText(char* textString) 
    { 
     static unsigned char outputLED[LED_STEPS]; 
     unsigned char i = 0; // index 
     // check length of string text 
     unsigned short length = strlen(textString); 
     // if more than 10 letters are used return error 
     if (length > LETTERS_LED_OUTPUT) 
     { 
      printf("Error: Too much letters. Just 10 Letters are allowed\n"); 
      return 0; 
     } 
     // through complete string 
     for (i = 0; i < length; i++) 
     { 
    memcpy(&outputLED[i * LETTER_WIDTH], &(libraryLetters[textString[i]]), 
         LETTER_WIDTH); 
      } 
      // fills rest with 0 
      for (i = length * LETTER_WIDTH; i < LED_STEPS; i++) 
      { 
      outputLED[i] = 0x00; 
     } 
     return outputLED; 
    } 

任何想法?

感謝 費邊

+0

你期望使用什麼整數類型(MCU)來處理48位常量,比如0xFE0202020200?也許你的MCU是64位的?將'letter'類型定義爲'int64'或類似的64位'long',而不是數組。否則,你不能完成任務。 – i486

+3

您的代碼格式和縮進遍佈整個地方。請[編輯]並修復它。 – user694733

回答

2

您的代碼並沒有太大的意義。首先,將一個數組隱藏在typedef後面並不是一個好主意。擺脫這一點。

使用C的默認「原始數據類型」也不是一個好主意,因爲它們是不可移植的且長度各不相同。請使用stdint.h類型。這在嵌入式系統編程中非常必要。

至於實際的問題,你不能指定數組這樣

*libraryLetters[0x20] = 0x000000000000;

這沒有任何意義。您正在告訴編譯器在您的6字節數組的第一個字節中存儲64位整數。你可能要做的是這樣的:

const uint8_t letters [128][LETTER_WIDTH] = 
{ 
    [0x20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 
    ['A'] = {0xFE, 0x90, 0x90, 0x90, 0xFE, 0x00}; 
    ... 
}; 

假設這是一些顯示器的符號表。如果是這樣,它應該是const並分配在閃存中。

0

定義您letter類型unsigned char將只持有一個字節,但你嘗試存儲6字節的整數。所以如果你想能夠使用任意長度的字母數組,你只能得到最後一個字節,它在你的所有字母中都是零。否則,如評論所示,使用64字節類型會更容易。

相反,你應該添加字母

libraryLetters['H'][0] = 0xFE; 
libraryLetters['H'][1] = 0x90; 
... 

或者你可以使用memcpy(libraryLetters['A'], letter_number, LETTER_WIDTH)由伊恩·雅培的建議。

0

*libraryLetters[x]unsigned char類型和你想的一些分配給它的unsigned char的範圍之外。

它看起來像你試圖分配一個6字節的序列*libraryLetters[x]。要做到這一點的方法之一是使用memcpy,例如:

memcpy(libraryLetters['A'], "\xFE\x90\x90\x90\xFE\x00", 6);