【发布时间】:2015-04-22 20:56:13
【问题描述】:
基本上,我在 32 位闪存中顺序写入了一堆 2 字节整数。如何以 int16 数组的形式访问它们?
我不考虑单词边界而编写了数组。但如果需要,我可以添加填充,以便数组从单词边界开始。
这是我写入闪存的代码(基本上它只是将每 4 个字节组合成一个字,然后将该字写入闪存):
for(flash_page = 0; flash_page < protocol_pages; flash_page++){ //loop through all the pages
for(flash_address = 0; flash_address < NUMBER_OF_INSTRUCTIONS_IN_PAGE; flash_address++){ //loop through all the words in the page
for (byte_address = 0; byte_address < 4; byte_address++){ //loop through the byte in each word
buffer_check();
flash_word += buffer[buffer_index] << (8*(3-byte_address));
buffer_index++;
bytes_written++;
if(bytes_written >= data_length)
break;
}
///////Write word here
NVMWriteWord((void*) &(proto_data_addr[flash_page][flash_address]), flash_word);
flash_word = 0;
if(bytes_written >= data_length)
break;
}
if(bytes_written >= data_length)
break;
}
混合到这个字节块中的是首尾相连的 2 字节整数序列。将它们写入闪存后,如何将它们作为数组访问?我是否必须填充数组以便每个单词中有 2 个 int16?
谢谢!
【问题讨论】:
-
你用的是什么编译器?
-
buffer是什么类型?如果uint8_t buffer[],则<< (8*(3-byte_address));可能是个问题,具体取决于int的大小。 -
我在 MPLAB X 中使用 xc32。缓冲区是 BYTE 类型的数组,但闪存字是 32 位,因此即使最大位移为 24 也应该没问题。
-
注意:
flash_word类型与buffer[buffer_index] << (8*(3-byte_address))无关。如果移位 24 可以,则取决于int大小,而不是flash_word大小。 IAC、xc32 可能使用 32 位int并且移位不是问题。
标签: c pointers embedded pic pic32