gpt4 book ai didi

c - EEPROM 数据传输至 TeraTerminal

转载 作者:行者123 更新时间:2023-11-30 17:13:34 24 4
gpt4 key购买 nike

我正在尝试使用 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) 放入监 window 口时,它会得到优化,当我声明为 volatile 时,它​​仍然会得到优化,但是当最大值设置为 4 时,循环会正确退出,但如果最大值高于4卡住了...感谢您的帮助。

最佳答案

我通过查看优化帖子发现了这一点。如果不使用循环变量,编译器会将变量设置为最大值。我不完全了解编译器或优化。但话虽这么说,如果我只是将 uspagesSent 变量传递给 TeraTermUART0Transmit,然后在该函数中将其乘以 64 以获得下一个(页面)地址,那么一切都很好。希望这对以后的人有帮助。以下是有用帖子的链接:for loop being ignored (optimized?) out

关于c - EEPROM 数据传输至 TeraTerminal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30760255/

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