Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interrupt stack problem

Status
Not open for further replies.

zhaniko93

New Member
I have a code:
Code:
 #define main_eng PORTA, 1
 #define accel PORTB, 6
 #define slow_down PORTB, 5
 #define RF_in PORTB, 1
 #define RF_out PORTB, 2
 
 goto Setup
 org 4
 call ISR 
 goto Main

 Setup nop
 list P=16F84A
 include "c:\pic\p16f84A.inc"
 __config _XT_OSC & _WDT_OFF & _CP_OFF
 ;============
 ; Declarations:
 acc_level equ 0x0c
 Test equ 0x0d
 clrf acc_level
;;;
 clrf PORTA ; resets Port A
 clrf PORTB
 bsf RF_out
 bsf STATUS, RP0 ; BANK1
 movlw b'0000' ; RA0: LED, RA1-3: not connected
 movwf TRISA
 movlw b'01011011'
 movwf TRISB
 movlw b'10010000'
 movwf INTCON
 movlw b'11010100'
 movwf OPTION_REG
 bcf STATUS, RP0
  ;============


 Main 
 movfw acc_level
 movfw Test
 btfsc accel
 call gazi
 btfsc slow_down
 call tormuzi
 ; Start Engine
 call Going
 call RFC
 goto Main
 
 Going clrf TMR0
 bsf main_eng
 Mda movfw acc_level
 subwf TMR0, 0
 btfss STATUS, Z
 goto Mda
 call W8ing
 retlw 0




 ISR bsf PORTB, 7 
 bcf PORTB, 7
  bcf INTCON, 1
retfie



 

 W8ing movfw TMR0
 sublw 0xFF
 bcf main_eng
 movwf Test
 decfsz Test
 goto W8ing
 retlw 0


 gazi movfw acc_level
 sublw 0xFE
 btfss STATUS, Z
 incf acc_level
 retlw 0

 tormuzi movfw acc_level
 sublw 0x00
 btfss STATUS, Z
 decf acc_level
 retlw 0
 

 RFC btfsc RF_in
 bcf RF_out
 btfss RF_in
 bsf RF_out
 retlw 0

 

 END
and when interrupt happens, everything goes ok but when instructon counter goes to retfie instruction, before executing it has used 2 level stack for interrupt (one for location and second for call to ISR) and when executing retfie, stack level decreaces by 1 and after several interrupts, I get stack owerflow! whats wrong? P.S. when I chage retfie with return, everything goes OK!
 

ericgibbs

Well-Known Member
Most Helpful Member
hi,
Try this, your code sequencing was a mess.:)

Code:
	list      p=16F84A            ; list directive to define processor
	#include <p16F84A.inc>        ; processor specific variable definitions
	errorlevel -302, -207


	__config _XT_OSC & _WDT_OFF & _CP_OFF
	;============ 
	; Declarations: 
	acc_level equ 0x0c
	Test	equ 0x0d


	#define	main_eng PORTA, 1
	#define	accel PORTB, 6
	#define	slow_down PORTB, 5
	#define	RF_in PORTB, 1
	#define	RF_out PORTB, 2
		 
	org 	0x0000
	goto	Setup

	org	0x0004
	ISR	bsf PORTB, 7 
	bcf	PORTB, 7
	bcf	INTCON, 1
	retfie

Setup	nop

	clrf	acc_level
;;;
	clrf	PORTA		; resets Port A
	clrf	PORTB
	bsf	RF_out
	bsf	STATUS,RP0	; BANK1
	movlw	b'0000'		; RA0: LED, RA1-3: not connected
	movwf	TRISA
	movlw	b'01011011'
	movwf	TRISB

	movlw	b'10010000'
	movwf	INTCON
	movlw	b'11010100'
	movwf	OPTION_REG

	bcf	STATUS, RP0
	;============ 


	Main	 
	movfw	acc_level
	movfw	Test
	btfsc	accel
	call	gazi
	btfsc	slow_down
	call	tormuzi
	; Start Engine 
	call	Going
	call	RFC
	goto	Main
		 
	Going	clrf TMR0
	bsf	main_eng
	Mda	movfw acc_level
	subwf	TMR0, 0
	btfss	STATUS, Z
	goto	Mda
	call	W8ing
	retlw	0

	W8ing	movfw TMR0
	sublw	0xFF
	bcf	main_eng
	movwf	Test
	decfsz	Test,F
	goto	W8ing
	retlw	0


	gazi	movfw acc_level
	sublw	0xFE
	btfss	STATUS, Z
	incf	acc_level,F
	retlw	0

	tormuzi	movfw acc_level
	sublw	0x00
	btfss	STATUS, Z
	decf	acc_level,F
	retlw	0
		 

	RFC	btfsc RF_in
	bcf	RF_out
	btfss	RF_in
	bsf	RF_out
	retlw	0

		 

	end
 

zhaniko93

New Member
Thanks ericgibbs! But why can't I call a subroutine ISR from 0x004?
 

ericgibbs

Well-Known Member
Most Helpful Member
Thanks ericgibbs! But why can't I call a subroutine ISR from 0x004?

hi,
When you get an interrupt the PIC pushes the return address on the Stack, in your program you CALLED the ISR from within the '0x0004' interrupt address and only had a RETFIE at the end of your ISR, so you had 2 pushes to every pop...overflow.!
 

zhaniko93

New Member
so 1 needed 1 retfie and 1 retlw? but how? can you give an example or make correction in my one? call ISR then retfie and retlw
 

zhaniko93

New Member
ok ok I got it thank you very much!
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Just in case anyone finds this in the future, I'll add that you should only use goto if all your code and data fit in the first code bank. If you use any address above 2k then it will crash.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Top