我正在尝试使用atmega324p从外部eeprom读取数据。问题出现在for循环中。我无法读取4页(4 x 64B)的EEPROM数据。如果我将循环计数器从4增加到5,我会陷入无限循环的UART0传输。这是我目前的代码:
// create an array to store the logged off eeprom data to and for use
//in sending bytes to teraterm. Location 0-15 hold first assertLog
UChar aucTeraTermMsgBuf[EXT_EEPROM_PAGE_LENGTH]; //length is 64B
void TeraTermOutputMsg(void)
{
//configure UART0 to what I want for my purposes
TeraTermConfigUART0();
//define the starting address to read from. Each time the button pushed
//we will begin reading from the very beginning of memory array (aka it is a full dump of EEPROM stored data)
UInt16 eepromReadAddress = 0x0000;
//Loop thru whole EEPROM 1 page of data at a time and send it.
//Cant get past 4 pages though.... TODO FIX THIS
for(UInt16 uspagesSent = 0; uspagesSent < 4; uspagesSent++)
{
//get one page of the log from the specified address in the eeprom and store it to an
//array of type UChar and length of 1 page (64B).
TeraTermGetAssertLog(eepromReadAddress);
//send the 1 page of eeprom data to teraterm
TeraTermUART0Transmit();
//update the next read address to pass the get log function
eepromReadAddress += EXT_EEPROM_PAGE_LENGTH; //length is 64Bytes
}
}
//Gets data from the EEPROM and puts it into an array in prep to send out UART0 to PC
void TeraTermGetAssertLog(UInt16 nextReadAddress)
{
//initialize the array we are storing the EEPROM data to
for(UInt16 i = 0; i < EXT_EEPROM_PAGE_LENGTH; i++)
aucTeraTermMsgBuf[i] = 0;
//this will read the bytes from EEPROM and load them into an array
ExtEEPROM_Read(aucTeraTermMsgBuf, nextReadAddress);
}
//function that will output string of data out to PC
void TeraTermUART0Transmit(void)
{
//array to encode the tera term message to using sprintf.
//Takes each hex value (0x_ _ ) and converts it to ASCII code for those numbers/letters
UChar encodeMsgArray[2];
//next character to be transmitted
UChar nextChar;
//counter for bytes sent. After 16 (1 log) want to send new line break point
UInt8 byteCount = 0x01;
//loop through the message buffer and send output to teraterm
for(UInt16 i = 0; i < EXT_EEPROM_PAGE_LENGTH; i++)
{
//get next char from the TeraTerm output buffer array
nextChar = aucTeraTermMsgBuf[i];
//convert the char hex value into the ascii code for the 2 letters/numbers present in 1 hex value
sprintf(encodeMsgArray, "%2.2x", nextChar);
//transmit those two ascii codes on the uart0 to teraterm with a space after
UART0_Transmit(encodeMsgArray[0]);
UART0_Transmit(encodeMsgArray[1]);
UART0_Transmit(" "); //send a space so text file parser can distinguish between bytes of data
//if we have sent a whole assert log, print next to new line
if(byteCount == 16)
{
byteCount = 0x00;
sprintf(encodeMsgArray, "\r\n");
UART0_Transmit(encodeMsgArray[0]);
UART0_Transmit(encodeMsgArray[1]);
}
++byteCount;
}
}
我知道阅读可能有点太多了,但我很难过。当我将循环计数器变量(pagesSent)放入监视窗口时,它会被优化,当我声明为volatile时它仍然会被优化,但是当最大值设置为4时,循环正常退出,但如果最大值高于4卡住...... 谢谢你的帮助。
答案 0 :(得分:0)
我通过查看optmization帖子来解决这个问题。如果未使用循环变量,则编译器将变量设置为最大值。我不完全理解编译器或优化。但话虽如此,如果我只是将uspagesSent变量传递给TeraTermUART0Transmit,然后在该函数中将其乘以64以获得下一个(页面)地址,一切都很好。 希望这有助于有人下线。以下是有用帖子的链接:for loop being ignored (optimized?) out