gpt4 book ai didi

c - 保持数组不在 RAM 中

转载 作者:太空狗 更新时间:2023-10-29 16:49:51 24 4
gpt4 key购买 nike

我正在和一些 friend 一起学习(并尝试教授)电子/编程。我们决定使用发射台构建一个 LED 立方体来完成我们的任务。

这是我们遇到的问题:

对于我们的动画,我们将它们存储在一个二维数组中。 如果我们将数组定义为常量,我们会得到错误

我不希望这个数组在 RAM 中,因为我们没有太多可用空间。

编辑:代码见下方

我希望这篇文章发布在正确的地方。
不是时候(我想?) 我要更改任何数组,它们都是手动设置一次并循环播放。我不确定要(隐含地?)转换什么

信息(如果需要):

MSP430G255316kB 闪存0.5kB内存

谢谢你的帮助

更新: 嗯,HansPassant 告诉我为什么会出现这个错误。但我仍然遇到 RAM 溢出(显然它没有存储在闪存中?)

我尝试的另一件事是我也将它声明为 const 而不是 #define RON (Row ON)。 (所以当创建一个动画数组时,它是用常量显式创建的)我曾希望将它存储在闪存中。

如果需要,这里是完整的代码:

#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);
// }

}

以及存在的错误:

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

region `ram' overflowed by 142 bytes collect2: ld returned 1 exit status

最佳答案

不确定这是否有帮助,但是对于 ld 你可以指定要使用的内存区域,参见 docs .

除此之外,使用整数会浪费很多空间。减少内存的更简单方法:在声明 typedef unsigned 之后,将 const int frontFrame[3][9] 更改为 const BYTE frontFrame[3][9]字符字节

如果用bits来表示bits,可以节省更多:

#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)}
};

然后您将使用

进行一些测试(在 displayFrame 中)
for (int r = 0; r < ROWS_NUM; ++r)
for (int c = 0; c < COLS_NUM; ++c)
if (frame[r] & ON(c)) { ... }

我也会尝试在数据声明前添加 static,但这很可能不会影响内存放置。

关于c - 保持数组不在 RAM 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com