0

我正在研究只有2K RAM的atmega328

我使用脚本从代表 LCD 计时器的 10 位的 10 个位图创建 10 字节数组。这些字节数组存储在我的源代码中,但由于我一次只写一个数字,因此没有理由将它们全部保存在 RAM 中。这样我可以使用更大的数字并消耗更少的 RAM!

这就是我想要做的:

void load_digit_ba(uint8_t digit, uint8_t digit_ba[]) {

  // from 0 to 9 I copy the current digit in the byte array form into digit_ba
  if (digit == 0) {
    uint8_t digit_ba_tmp[10] = { 24, 40, 0, 0, 0, 224, 240, 248, 252, 254 };

    memcpy(digit_ba, digit_ba_tmp, 10);
  }
...

}

但似乎编译器正在为所有数组静态保留内存。在我的情况下,122 字节 * 10 位 = ~1K,超过我 RAM 的一半。

Sketch uses 7,310 bytes (23%) of program storage space. Maximum is 30,720 bytes.
Global variables use 1,695 bytes (82%) of dynamic memory, leaving 353 bytes for local variables. Maximum is 2,048 bytes

如果我将一个单元格添加到数组 [11] 而不是 [10] 并且只传递 10 值作为初始化程序,它将改为在运行时分配内存(看起来)

void load_digit_ba(uint8_t digit, uint8_t digit_ba[]) {

  if (digit == 0) {
    uint8_t digit_ba_tmp[11] = { 24, 40, 0, 0, 0, 224, 240, 248, 252, 254 };

    memcpy(digit_ba, digit_ba_tmp, 10);
  }
...

}

Aurdino IDE 说:

Sketch uses 11,396 bytes (37%) of program storage space. Maximum is 30,720 bytes.
Global variables use 597 bytes (29%) of dynamic memory, leaving 1,451 bytes for local variables. Maximum is 2,048 bytes.

如果我uint8_t digit_ba_tmp[]让编译器计算长度,则行为相同,它会保留 ~1K RAM。

为什么添加一个单元格会这样做并且它是否干净?在我看来不是。这里的假设是,由于数字数组的长度对于每个数字都是固定的,只是改变了内容,并且我通过串行发送一个数字到显示器,因此将当前数字加载到 ram 然后发送是有意义的它。我在这里看不到任何堆/堆栈内存碎片问题,对吗?笑脸悲伤

4

0 回答 0