2013-03-23 5 views
10

Sto imparando (e provando a insegnare) l'elettronica/programmazione con alcuni amici. Abbiamo deciso di costruire un cubo LED utilizzando un launchpad per il nostro compito.Mantieni array non in RAM

Ecco il problema ci imbattiamo in:

Per i nostri animazioni, li memorizziamo in un array bidimensionale. Si ottiene un errore se definiamo la matrice come costante

Non voglio che questa matrice sia nella RAM, perché non abbiamo molto disponibile.

EDIT: Vedi sotto per il codice

Spero che questo è pubblicato nel posto giusto.
In nessun momento (credo?) Sto cambiando qualsiasi array, sono tutti impostati una volta sola a mano e appena ricollegati. Io sono sicuro di quello che sta cercando di essere convertito

informazioni, se necessario (implicitamente?):

MSP430G2553 16kB Flash 0.5kb RAM

Grazie per il vostro aiuto

UPDATE: Be ', HansPassant mi ha detto perché quell'errore si stava rivelando. Ma ottengo ancora un overflow di RAM (Apparentemente non viene memorizzato in flash?)

Un'altra cosa che ho provato è invece di #define RON (Riga ON) l'ho dichiarata anche come const. (così, quando è stato creato un array di animazione, è stato creato esplicitamente con solo costanti). Speravo che lo memorizzasse in flash.

Ecco la totalità del codice se necessario:

#define povDelay delayMicroseconds(200) // POV Delay 

const int CON = LOW; 
const int COFF = HIGH; 
const int RON = HIGH; 
const int ROFF = LOW; 

const int col[9] = {P1_0, P1_1, P1_2, P1_3, P1_4, P1_5, P2_0, P2_1, P2_2}; 
const int row[3] = {P1_6, P1_7, P2_3}; 

void setup() 
{ 
// Serial.begin(9600); 
    for (int i = 0; i < 9; i++) 
    { 
    pinMode(col[i], OUTPUT); 
    digitalWrite(col[i], COFF); 
    } 
    for (int i = 0; i < 3; i++) 
    { 
    pinMode(row[i], OUTPUT); 
    digitalWrite(row[i], ROFF); 
    } 
} 

void sideToSide(int delayTime) 
{ 
    int sideDelay = 1000; 
    // Left Frame 
    const int leftFrame[3][9] = { 
    {CON, COFF, COFF, CON, COFF, COFF, CON, COFF, COFF}, 
    {CON, COFF, COFF, CON, COFF, COFF, CON, COFF, COFF}, 
    {CON, COFF, COFF, CON, COFF, COFF, CON, COFF, COFF} 
    }; 
    // Middle Frame 
    const int middleFrame[3][9] = { 
    {COFF, CON, COFF, COFF, CON, COFF, COFF, CON, COFF}, 
    {COFF, CON, COFF, COFF, CON, COFF, COFF, CON, COFF}, 
    {COFF, CON, COFF, COFF, CON, COFF, COFF, CON, COFF} 
    }; 
    // Right Frame 
    const int rightFrame[3][9] = { 
    {COFF, COFF, CON, COFF, COFF, CON, COFF, COFF, CON}, 
    {COFF, COFF, CON, COFF, COFF, CON, COFF, COFF, CON}, 
    {COFF, COFF, CON, COFF, COFF, CON, COFF, COFF, CON} 
    }; 




    // Display this animation 3 times (Frame 1, Frame 2, Frame 3, Frame 2) 
    for (int i = 0; i < 4; i++) 
    { 
    displayFrame(leftFrame, delayTime); 
    displayFrame(middleFrame, delayTime); 
    displayFrame(rightFrame, delayTime); 
    displayFrame(middleFrame, delayTime); 
    } 
    // Last animation 
    displayFrame(leftFrame, delayTime); 



} // ############ END SIDE TO SIDE 

void frontToBack(int delayTime) 
{ 
    // Left Frame 
    const int frontFrame[3][9] = { 
    {CON, CON, CON, COFF, COFF, COFF, COFF, COFF, COFF}, 
    {CON, CON, CON, COFF, COFF, COFF, COFF, COFF, COFF}, 
    {CON, CON, CON, COFF, COFF, COFF, COFF, COFF, COFF} 
    }; 
    // Middle Frame 
    const int middleFrame[3][9] = { 
    {COFF, COFF, COFF, CON, CON, CON, COFF, COFF, COFF}, 
    {COFF, COFF, COFF, CON, CON, CON, COFF, COFF, COFF}, 
    {COFF, COFF, COFF, CON, CON, CON, COFF, COFF, COFF} 
    }; 
    // Right Frame 
    const int backFrame[3][9] = { 
    {COFF, COFF, COFF, COFF, COFF, COFF, CON, CON, CON}, 
    {COFF, COFF, COFF, COFF, COFF, COFF, CON, CON, CON}, 
    {COFF, COFF, COFF, COFF, COFF, COFF, CON, CON, CON} 
    }; 




    // Display this animation 3 times (Frame 1, Frame 2, Frame 3, Frame 2) 
    for (int i = 0; i < 4; i++) 
    { 
    displayFrame(frontFrame, delayTime); 
    displayFrame(middleFrame, delayTime); 
    displayFrame(backFrame, delayTime); 
    displayFrame(middleFrame, delayTime); 
    } 
    // Last animation 
    displayFrame(frontFrame, delayTime); 


} // ############# END FRONT TO BACK 


void diagFunctionOne(int delayTime) 
{ 
    const int firstFrame[3][9] = { 
    {CON, COFF, COFF, COFF, COFF, COFF, COFF, COFF, COFF}, 
    {CON, COFF, COFF, COFF, COFF, COFF, COFF, COFF, COFF}, 
    {CON, COFF, COFF, COFF, COFF, COFF, COFF, COFF, COFF} 
    }; 
    const int secondFrame[3][9] = { 
    {COFF, CON, COFF, CON, COFF, COFF, COFF, COFF, COFF}, 
    {COFF, CON, COFF, CON, COFF, COFF, COFF, COFF, COFF}, 
    {COFF, CON, COFF, CON, COFF, COFF, COFF, COFF, COFF} 
    }; 
    const int thirdFrame[3][9] = { 
    {COFF, COFF, CON, COFF, CON, COFF, CON, COFF, COFF}, 
    {COFF, COFF, CON, COFF, CON, COFF, CON, COFF, COFF}, 
    {COFF, COFF, CON, COFF, CON, COFF, CON, COFF, COFF} 
    }; 
    const int forthFrame[3][9] = { 
    {COFF, COFF, COFF, COFF, COFF, CON, COFF, CON, COFF}, 
    {COFF, COFF, COFF, COFF, COFF, CON, COFF, CON, COFF}, 
    {COFF, COFF, COFF, COFF, COFF, CON, COFF, CON, COFF} 
    }; 
    const int fifthFrame[3][9] = { 
    {COFF, COFF, COFF, COFF, COFF, COFF, COFF, COFF, CON}, 
    {COFF, COFF, COFF, COFF, COFF, COFF, COFF, COFF, CON}, 
    {COFF, COFF, COFF, COFF, COFF, COFF, COFF, COFF, CON} 
    }; 
    // Start from 7 
    const int sixthFrame[3][9] = { 
    {COFF, COFF, COFF, COFF, COFF, COFF, CON, COFF, COFF}, 
    {COFF, COFF, COFF, COFF, COFF, COFF, CON, COFF, COFF}, 
    {COFF, COFF, COFF, COFF, COFF, COFF, CON, COFF, COFF} 
    }; 
    const int seventhFrame[3][9] = { 
    {COFF, COFF, COFF, CON, COFF, COFF, COFF, CON, COFF}, 
    {COFF, COFF, COFF, CON, COFF, COFF, COFF, CON, COFF}, 
    {COFF, COFF, COFF, CON, COFF, COFF, COFF, CON, COFF} 
    }; 
    const int eighthFrame[3][9] = { 
    {CON, COFF, COFF, COFF, CON, COFF, COFF, COFF, CON}, 
    {CON, COFF, COFF, COFF, CON, COFF, COFF, COFF, CON}, 
    {CON, COFF, COFF, COFF, CON, COFF, COFF, COFF, CON} 
    }; 
    const int ninthFrame[3][9] = { 
    {COFF, CON, COFF, COFF, COFF, CON, COFF, COFF, COFF}, 
    {COFF, CON, COFF, COFF, COFF, CON, COFF, COFF, COFF}, 
    {COFF, CON, COFF, COFF, COFF, CON, COFF, COFF, COFF} 
    }; 
    const int tenthFrame[3][9] = { 
    {COFF, COFF, CON, COFF, COFF, COFF, COFF, COFF, COFF}, 
    {COFF, COFF, CON, COFF, COFF, COFF, COFF, COFF, COFF}, 
    {COFF, COFF, CON, COFF, COFF, COFF, COFF, COFF, COFF} 
    }; 

    for (int i = 0; i < 4; i++) 
    { 
    displayFrame(firstFrame, delayTime); 
    displayFrame(secondFrame, delayTime); 
    displayFrame(thirdFrame, delayTime); 
    displayFrame(forthFrame, delayTime); 
    displayFrame(fifthFrame, delayTime); 
    displayFrame(sixthFrame, delayTime); 
    displayFrame(seventhFrame, delayTime); 
    displayFrame(eighthFrame, delayTime); 
    displayFrame(ninthFrame, delayTime); 
    displayFrame(tenthFrame, delayTime); 
    } 
} 



/* 

##################################################### 
##################################################### 
##################################################### 
###################################################### 
####################################################### 


*/ 






// Takes a frame and displays it for the length of animation delay (in ms) 
void displayFrame(const int frame[3][9], int animationDelay) 
{ 
// Serial.println("displayFrame called"); 
unsigned long currentTime = millis(); 

while (1) // Do this continually until animationDelay is met 
{ 
int i, j; 
// Rows 
for (i = 0; i < 3; i++) 
{ 
    // Turn correct ROW on 
    digitalWrite(row[i], RON); 

    // Columns 
    for (j = 0; j < 9; j++) 
    { 
    // Turn correct COLUMNS on 
    digitalWrite(col[j], frame[i][j]); 

    } // End J loop 

    povDelay; 
    digitalWrite(row[i], ROFF); 

} // End I Loop 

if ((millis() - currentTime) >= animationDelay) 
{ 
    delay(10); 
    // Cleanup, Turn all OFF 
    for (int i = 0; i < 9; i++) 
    { 
     digitalWrite(col[i], COFF); 
    } 
    for (int i = 0; i < 3; i++) 
    { 
     digitalWrite(row[i], ROFF); 
    } 

    delay(10); // timefix? 

    return; 

} // End time test if statement 

} 


} 


void loop() 
{ 
    diag(350); 
    sideToSide(350); 
    frontToBack(350); 


// for (int i = 0; i < 9; i++) 
// { 
// digitalWrite(col[i], CON); 
// } 
// for (int i = 0; i < 3; i++) 
// { 
// digitalWrite(row[i], RON); 
// delay(250); 
// digitalWrite(row[i], ROFF); 
// } 

} 

E l'errore esistente:

c:/users/dan/appdata/local/energi~1/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: 

regione `ram' traboccato con 142 byte collect2: ld returned 1 exit status

+1

Basta dichiarare anche l'argomento const. –

+1

@HansPassant Oh wow, sono un idiota. Grazie Hans! – Dan

+4

Tutto ciò assomiglia al codice C e lo hai codificato C, ma il nome del file è '.cpp'. Cosa succede con quello? –

risposta

3

non sicuro che questo sarà di aiuto, ma per ld puoi specificare l'area di memoria da utilizzare, vedere docs.

Questo a parte, stai sprecando molto spazio per un po ', usando un numero intero. Il modo più semplice per ridurre la memoria: cambiare const int frontFrame[3][9] a const BYTE frontFrame[3][9], dopo la dichiarazione di typedef unsigned char BYTE.

si può risparmiare di più se si utilizza bit per rappresentare i bit:

#define ON(b) (1 << b) 

const int frontFrame[3] = { 
    {ON(8) | ON(7) | ON(6)}, 
    {ON(8) | ON(7) | ON(6)}, 
    {ON(8) | ON(7) | ON(6)} 
}; 

e si metterà alla prova un po '(in displayFrame) utilizzando

for (int r = 0; r < ROWS_NUM; ++r) 
    for (int c = 0; c < COLS_NUM; ++c) 
    if (frame[r] & ON(c)) { ... } 

Vorrei anche provare ad aggiungere static di fronte della dichiarazione dei dati, ma molto probabilmente questo non avrebbe alcun effetto sul posizionamento della memoria.

+0

Mi piace il tuo suggerimento sui vincoli di spazio. Tuttavia, ancora di più è statico che li ha salvati in flash invece di tenerli in memoria. Grazie mille CapelliC! Lo apprezziamo molto! – Dan