Hi all,
I am having a problem with a nested FOR loop in C which causes the PIC to reset itself. I am trying to copy the content of an SRAM card (32kB) to a bankstick. If I use only the inner loop for reading/writing 256 bytes, everything is smooth. As soon as I activate the outer loop, the app increases the [tt]bankstick_addr[/tt] counter to around 1736 -1741 (that is 0x06C8 to 0x06CD) and then crashes. The counter varies, but the crash is always there, and the core resets itself.
This is my code:
// read whole SRAM card and copy to Bankstick
else if (pin == 0x14){
// SRAM_Read_Page();
bankstick_addr = 0x0000;
MIOS_LCD_PrintCString("Copy Card to Bankstick ");
// outer loop increments the address highbyte
for(card_h_addr=0x00; card_h_addr<0x7F; ++card_h_addr){
// inner loop increments the address lowbyte
for(card_l_addr=0x00; card_l_addr<0xFF; ++card_l_addr){
SRAM_Read();
error |= MIOS_BANKSTICK_Write(bankstick_addr, ram_byte);
++bankstick_addr;
if( error ) {
// here you could do some error handling
MIOS_LCD_CursorSet(0x00);
MIOS_LCD_PrintCString("Bankstick write Error ");
}
// print current address
MIOS_LCD_CursorSet(0x19);
MIOS_LCD_PrintCString("Address: ");
MIOS_LCD_PrintBCD5(bankstick_addr);
//app_flags.DISPLAY_UPDATE_REQ = 1;
}
}
[tt]bankstick_addr[/tt] is a counter that determines the address in the bankstick where the byte shall be put; it’s of type unsigned int
[tt]card_l_addr[/tt] determines the low byte of the 16bit SRAM address; type unsigned char
[tt]card_h_addr[/tt] is the high byte of the 16bit SRAM address; also unsigned char
All three are global variables because they will get modified at various places. The LCD output is mainly for debugging.
Any thoughts on this?
Best regards, ilmenator
PS: Thanks stryd, you see your explanations about that assembler code actually got me here…