The main idea is to change the return address of the function when the interrupt returns. Of course, the function address can also be obtained by using a pointer . The following is the content of debugging the microcontroller atmega16 using iccavr simulation compiler software, hex file disassembly software and proteus simulation experiment.
First, we use C language to compile a function and customize 4 different functions. For example: 3 LEDs light up to indicate.
Such as:
#include
#include
unsigned int sp_ZHI=0,SP_init=0;
void LED_2(void) void LED_3(void) Then design an off function, Next is the interrupt initialization and port configuration, PORTA = 0xFF; } unsigned char k=4; k++; //Reset the stack pointer to prevent stack overflow } When the device is turned on, all three LEDs light up. The switching of the light-emitting diodes is interrupted by pressing the button.
void LED_1(void)
{ while(1){PORTB=(1<
{while(1){PORTB=(1<
{ while(1){PORTB=(1<
void LED_OFF(void)
{ while(1){PORTB=0;}}
void INTER_init_0()
{
DDRA = 0xff;
PORTB = 0xff;
DDRB = 0xff;
PORTC = 0xFF;
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
MCUCR|=1<
Interrupt request function
//External interrupt 0 vector port
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
if(k>=4)
k=0;
SP=sp_ZHI-2;//This sp_ZHI-2 is found through disassembly, check the assembly.
}
void SP_INIT(unsigned int a)
{
SP=a;
Then we define the main function
. The function of the main function is:
void main(void)
{ INTER_init_0();
sp_ZHI=SP;
SP_INIT(sp_ZHI);
SREG|=0x80;//使能全局中断
PORTB = 0;
while(1)
{
switch(k)
{
case 1:LED_1();break;
case 2:LED_2();break;
case 3:LED_3();break;
default :LED_OFF();break;
}
}
}
; reassembly of "实验.hex" ; created
by ReAVR V3.2.0
; at 2010/02/02 - 22:04:10
; for ICCAVR assembler
;---------------------------------------
; AVR_TYPE=
; FLASH_SIZE=8KB
; SRAM_START=0x60
;---------------------------------------
.area text
;; older iccavr need different setup:
;; .area text (abs,ovr)
;; .org 0x0000
;---------------------------------------
; byte constants:
;
k00 = 0x00 ;
k01 = 0x01 ;
k02 = 0x02 ;
k03 = 0x03 ;
k04 = 0x04 ;
k10 = 0x10 ;
k40 = 0x40 ; '@'
k5F = 0x5F ; '_'
k60 = 0x60 ; '`'
k65 = 0x65 ; 'e'
kAA = 0xAA ; '?
kBF = 0xBF ; '?
kFF = 0xFF ; '
;
; io register addresses:
;
p11 = 0x11
p12 = 0x12
p14 = 0x14
p15 = 0x15
p17 = 0x17
p18 = 0x18
p1A = 0x1A
p1B = 0x1B
p35 = 0x35
p3A = 0x3A
p3B = 0x3B
p3D = 0x3D
p3E = 0x3E
p3F = 0x3F
;
; bit numbers:
;
b0 = 0x00
b1 = 0x01
b2 = 0x02
b3 = 0x03
b4 = 0x04
b5 = 0x05
b6 = 0x06
b7 = 0x07
;---------------------------------------
;
L0000:
jmp __start ; L0033
; ---------- jump on last line
jmp L0085
; ---------- jump on last line
.word 0xFFFF ; pc=0x0004(0x0008)
.word 0xFFFF ; pc=0x0005(0x000A)
.word 0xFFFF ; pc=0x0006(0x000C)
.word 0xFFFF ; pc=0x0007(0x000E)
.word 0xFFFF ; pc=0x0008(0x0010)
.word 0xFFFF ; pc=0x0009(0x0012)
.word 0xFFFF ; pc=0x000A(0x0014)
.word 0xFFFF ; pc=0x000B(0x0016)
.word 0xFFFF ; pc=0x000C(0x0018)
. word 0xFFFF ; pc=0x000D(0x001A)
.word 0xFFFF ; pc=0x000E(0x001C)
.word 0xFFFF ; pc=0x000F(0x001E)
.word 0xFFFF ; pc=0x0010(0x0020)
.word 0xFFFF ; pc=0x0011(0x0022)
.word 0xFFFF ; pc=0x0012(0x0024)
.word 0xFFFF ; pc=0x0013(0x0026)
.word 0xFFFF ; pc=0x0014(0x0028)
.word 0xFFFF ; pc=0x0015(0x002A)
. word 0xFFFF ; pc=0x0016(0x002C)
.word 0xFFFF ; pc=0x0017(0x002E)
.word 0xFFFF ; pc=0x0018(0x0030)
.word 0xFFFF ; pc=0x0019(0x0032)
.word 0xFFFF ; pc=0x001A(0x0034)
.word 0xFFFF; pc=0x001B(0x0036)
.word 0xFFFF ; pc=0x001C(0x0038)
.word 0xFFFF ; pc=0x001D(0x003A)
.word 0xFFFF ; pc=0x001E(0x003C)
.word 0xFFFF ; pc=0x001F(0x003E)
.word 0xFFFF ; pc=0x0020(0x0040)
.word 0xFFFF ; pc=0x0021(0x0042)
.word 0xFFFF ; pc=0x0022(0x0044)
.word 0xFFFF ; pc=0x0023(0x0046)
.word 0xFFFF ; pc=0x0024(0x0048)
.word 0xFFFF ; pc=0x0025(0x004A)
.word 0xFFFF ; pc=0x0026(0x004C)
.word 0xFFFF ; pc=0x0027(0x004E)
.word 0xFFFF ; pc=0x0028(0x0050)
.word 0xFFFF ; pc=0x0029(0x0052)
.word 0xFFFF ; pc=0x002A(0x0054)
.word 0xFFFF ; pc=0x002B(0x0056)
.word 0xFFFF ; pc=0x002C(0x0058)
.word 0xFFFF ; pc=0x002D(0x005A)
.word 0xFFFF ; pc=0x002E(0x005C)
.word 0xFFFF ; pc=0x002F(0x005E)
nop
nop
; --------- this is skippy
sbrs r16,b4
;
__start::
; L0033:
ldi r28,k5F
; --------- last may be skipped
; pc=0x34(0x68)
;
ldi r29,k04
out p3D,r28
out p3E,r29
subi r28,k10
sbci r29,k00
ldi r16,kAA
std Y+o00,r16
clr r0
ldi r30,k65
ldi r31,k00
ldi r17,k00
L003F:
cpi r30,k65
cpc r31,r17
breq L0044
; ----- branch on last line
st Z+,r0
rjmp L003F
; ----------- jump on last line
L0044:
st Z,r16
ldi r30,k60
ldi r31,k00
ldi r26,k60
ldi r27,k00
ldi r17,k00
L004A:
cpi r30,k65
cpc r31,r17
breq L0051
; ----- branch on last line
lpm
adiw r30,k01
st X+,r0
rjmp L004A
; ----------- jump on last line
L0051:
call L00A3
L0053:
rjmp L0053
; ----------- jump on last line
; pc=0x54(0xA8)
;
L0054:
rjmp L0057
; ----------- jump on last line
L0055:
ldi r24,k01
out p18,r24
L0057:
rjmp L0055
; ----------- jump on last line
ret
;----------------------*
; pc=0x59(0xB2)
;
L0059:
rjmp L005C
; ----------- jump on last line
L005A:
ldi r24,k02
out p18,r24
L005C:
rjmp L005A
; ----------- jump on last line
ret
;----------------------*
; pc=0x5E(0xBC)
;
L005E:
rjmp L0061
; ----------- jump on last line
L005F:
ldi r24,k04
out p18,r24
L0061:
rjmp L005F
; ----------- jump on last line
ret
;----------------------*
; pc=0x63(0xC6)
;
L0063:
rjmp L0066
; ----------- jump on last line
L0064:
clr r2
out p18,r2
L0066:
rjmp L0064
; ----------- jump on last line
ret
;----------------------*
; pc=0x68(0xD0)
;
L0068:
sts (p3E+0x20),r17 ; io register
sts (p3D+0x20),r16 ; io register
ret
;----------------------*
; pc=0x6D(0xDA)
;
L006D:
ldi r24,kFF
out p1B,r24
out p1A,r24
out p18,r24
out p17,r24
out p15,r24
clr r2
out p14,r2
out p12,r24
out p11,r2
in r24,p35
ori r24,k02
out p35,r24
in r24,p35
ori r24,k01
out p35,r24
in r24,p3B
ori r24,k40
out p3B,r24
in r24,p3A
andi r24,kBF
out p3A,r24
sei
ret
;----------------------*
; pc=0x85(0x10A)
;
L0085:
st -Y,r2
st -Y,r24
st -Y,r25
in r2,p3F
st -Y,r2
lds r24,D0064
subi r24,kFF
sts D0064,r24
cpi r24,k04
brcs L0094
; ----- branch on last line
clr r2
sts D0064,r2
L0094:
lds r24,D0060
lds r25,D0061
sbiw r24,k02
sts (p3E+0x20),r25 ; io register
sts (p3D+0x20),r24 ; io register
ld r2,Y+
out p3F,r2
ld r25,Y+
ld r24,Y+
ld r2,Y+
reti
;----------------------*
; pc=0xA3(0x146)
;
L00A3:
rcall L006D
in r2,p3D
in r3,p3E
sts D0061,r3
sts D0060,r2
movw r16,r2
rcall L0068
sei
clr r2
out p18,r2
rjmp L00C7
; ----------- jump on last line
L00B0:
lds r20,D0064
clr r21
cpi r20,k01
ldi r30,k00
cpc r21,r30
breq L00C0
; ----- branch on last line
cpi r20,k02
ldi r30,k00
cpc r21,r30
breq L00C2
; ----- branch on last line
cpi r20,k03
ldi r30,k00
cpc r21,r30
breq L00C4
; ----- branch on last line
rjmp L00C6
; ----------- jump on last line
L00C0:
rcall L0054
rjmp L00C7
; ----------- jump on last line
L00C2:
rcall L0059
rjmp L00C7
; ----------- jump on last line
L00C4:
rcall L005E
rjmp L00C7
; ----------- jump on last line
L00C6:
rcall L0063
L00C7:
rjmp L00B0
; ----------- jump on last line
ret
;----------------------*
; pc=0xC9(0x192)
;
__text_end::
;
; last flash byte address = 0x0191
; last flash word address = 0x00C8
;---------------------------------------
.area bss (abs)
.org 0x0060
;
__first_bss::
;
D0060:
.blkb 1
D0061:
.blkb 3
D0064:
;
__last_bss::
;
; last lds/sts data byte at 0x0064
;---------------------------------------
;
Previous article:AVR MCU fuse setting tips
Next article:Hex disassembly file decoding and description (obtaining function address)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- IAR FOR 430 Failed to re-intialize A possible solution
- Design information for temperature monitoring system
- The MPU9250 magnetometer ID reading does not get the correct value for the following reasons:
- CircuitPython 6.0.0 Beta 1 released
- Controlling LED brightness under ZSTACK
- [Analog Electronics Course Selection Test] + Input and Output Limitation
- Core Experience | Free evaluation and trial of Lingdong MM32 eminiboard, waiting for you!
- The most efficient solution for receiving and sending indefinite length packets on DSP serial port SCI
- (There are still many prizes left in the prize pool!) Mentor downloads the white paper and answers questions for a prize draw
- Be sure to pay attention to safety when driving on the road! Stay away from large vehicles!