Returning from Subroute

Hi,

I used Atmega32(DIP) in my prototype board to test part of my source code, the execution is smooth. Then I ported the code to Atmega64, only little code modifications are required.

However, I found the program counter quickly goes to an unknown address when the program runs in the beginning. I then trace the program using the debugger and find the program retrieve a wrong program counter after calling subroute, which finally brings the program to reset. Why this happens? Could anyone help me? The code segment is attached in the following. Thanks!

int main(void) { unsigned char key,sc; unsigned char uc;

timerInit();

: :

timerAttach(TIMER0OVERFLOW_INT,&timer); initKB();

: : }

void timerInit(void) { u08 intNum; // detach all user functions from interrupts for(intNum=0; intNum

Reply to
terry
Loading thread data ...

Have you checked the actual machine code against the assembler listing? It could be a bug in the compiler. I've known compilers (usually beta releases, but not always) generate the wrong code, in some circumstances. 8-(

Leon

Reply to
Leon Heller

Those are basically the last few instructions to execute. I'd guess there is a bug in your interrupt handler. Even if it works correctly (in terms of processing interrupts) it may trash the stack or otherwise fail to preserve all of the necessary state to resume after it runs.

--
Ben Jackson

http://www.ben.com/
Reply to
Ben Jackson

The function stores a 16 bit zero in a location of the vector. It can clobber the return address in the stack if the array and/or the stack is improperly set up.

The code doubles the index input in r24 and subtracts

0xfde7 from it, effectively pointing to the location 0x219 + 2*index, and stores two bytes of zeroes in the location pointed to and the next byte.

The compiler is not defined, but is seems to be GCC-AVR.

--
Tauno Voipio
tauno voipio (at) iki fi
 Click to see the full signature
Reply to
Tauno Voipio

Is the call to timerDetach that resets the one called from timerInit or is it a later call?

Post your timer interrupt service routine. I assume it's checking if TimerIntFunc[x] is 0 before jumping or calling the routine? I'm guessing that you assign 0 to TimerIntFunc[x], a timer interrupt happens, and you jump to 0 (don't know if that's the reset address for AVR).

Reply to
Gary Kato

I have checked both the machine codes of CALL and RET instructions and they are both correct.8-(

Reply to
terry

You don't show all the file: are you aware that the interrupt vector table needs to customized for each AVR chip. An interrupt vector can be at one address on the mega32 and at another adress on the mega64. The effect is that anything can happen. Do you initialize all unused interrupts to the "RETI" instruction?

--
Best Regards
Ulf at atmel dot com
 Click to see the full signature
Reply to
Ulf Samuelsson

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.