Continue to Site

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.

Stack Underflow

Status
Not open for further replies.

RobertD

New Member
(I'm not an OP btw)

Hi folks, I get the message 'stack underflow' after the return from delay command. As discussed earlier, I included this code

Code:
[b]main	btfsc	PORTB,4
	goto	main[/b]

main1   bsf     PORTA,6
	call	delay
	bcf	PORTA,6
	call	delay
	goto	main1

I guess to initiate the stack, I don't understand the operation of the stack too well, when does it push, and when it pops. When I run the sim, it works fine, but on the chip, TIMER1 hangs around 88 all the time. And the LED doesn't flash.

Code:
list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

cblock	0x40

	TIMER1,	TIMER2

	endc

	banksel	TRISA		;bank 1
	movlw	0x42		;1MHz clock
	movwf	OSCCON
	movlw	0x07		;turn comparators off
	movwf	CMCON

	movlw	B'11111111'
	movwf	TRISB		;all input.
	movlw	B'00000010'	;all RA ports all outputs except A/D (RA1) input
	movwf	ANSEL
	movwf	TRISA
	movlw	B'00000000'
	movwf	OPTION_REG	;make all PORTB pins 1
	banksel	PORTA
	
	clrf	PORTA
 	goto 	main



main
	btfsc	PORTB,4
	goto	main

main1
	bsf     PORTA,7
	call	delay
	bcf		PORTA,7
	call	delay
	goto	main1
	


delay
	MOVLW   D'100'          
    MOVWF   TIMER1                      
DELAY2
	MOVLW	D'200'			
	MOVWF	TIMER2
	DECFSZ  TIMER2,F        
	GOTO    $-1             
               
	DECFSZ	TIMER1,F		
	GOTO	DELAY2				
	return			

	end
 
Last edited:
A stack underflow means you have a "return" without a corresponding "call"

With the 16F88 you've got to save and later restore several registers within the interrupt routine.
 
What about the return at the end of the delay routine?

Doesn't it return to the call in main1...? My logic and program is: call from main, execute sub-delay, return to main. Obviously the chip doesn't see it this way. Is it the counter that is badly designed? I tried with various counter models, and got the same result.

You say I have to store and restore several registers within the interrupt routine. You mean more than one, like two or three...? I should write a routine that is more complicated then.
 
Last edited:
Org statements... do I need more than one? I put one in the beginning.

This is my first program, and the first time I use MPLAB, and the first time I program a chip. That's a lot of ground to cover, having to learn to program, to debug, to use MPLAB, to get the chip to work. Plus I'm not on this full time, only after work, a few days a week. I got so far, and I appreciate all the help I'm getting. Now I'm beginning to get the chip to work, and I had to learn about stack, I don't get the stack underflow anymore, since I added the main loop.

I tried to run in debug, and in release mode, and in sim, sim works fine, stepping works fine in all mode, it's just when I run the chip alone that it hangs. I tried various delay routines, with the same result. I'll try to find delay loops within complete code and see what's the line I'm missing.
 
Do you have a 10K-47K pull up resistor on MCLR? A 0.1uF capacitor across Vdd and Vss? Can you post the updated code which runs in SIM but not on PIC?
 
Yes I have a 10K across MCLR, and I'm using in circuit programming with Junebug. And I have a decoupling cap across the Vin, but not on Vout.

I'm trying with this delay routine I got from a calculator.
When I stop running the program, I get an error message in MPLAB PK2 error 0029. Failed pickit2 operation Halt I don't know where to find error codes for thePK2, so I don't know if this is relevant.

Code:
list	p=16F88
	include <p16f88.inc>
	__config _CONFIG1, _WDT_OFF & _INTRC_IO & _MCLR_ON & _LVP_OFF

	cblock 0x40
	d1, d2,	d3
	endc

	org		0x00

	banksel	TRISA		;bank 1
	movlw	0x42		;1MHz clock
	movwf	OSCCON
	movlw	0x07		;turn comparators off
	movwf	CMCON

	movlw	B'11111111'
	movwf	TRISB		;all input.
	movlw	B'00000010'	;all RA ports all outputs except A/D (RA1) input
	movwf	ANSEL
	movwf	TRISA
	
	banksel	PORTA

	movlw	0x08
	movwf	d1
	movlw	0x2F
	movwf	d2
	movlw	0x03
	movwf	d3
	
	clrf	PORTA
 	goto 	main



main
	btfsc	PORTB,6
	goto	main

main1
	bsf     PORTA,7
	call	delay
	
	bcf		PORTA,7
	call	delay
	goto	main1
	
	

delay
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	delay

			
	goto	$+1
	nop
	return

	end
 
Last edited:
Could it be an oscillator problem...?

I'm looking into this osc/fosc stuff.
 
Last edited:
I don't understand the operation of the stack too well
think of a stack of plates.
when you make a call, a new plate is added to the top(the current program counter), and the program counter is changed to the new address.
when you make a return, the last plate is picked from the top and the address on the plate is jumped to. the top of the stack is now one plate down.

the stack is a LIFO(last-in first-out) data structure, where the element removed(popped) is always the last one to be inserted(pushed).
 
I tried to run in debug, and in release mode, and in sim, sim works fine, stepping works fine in all mode, it's just when I run the chip alone that it hangs. I tried various delay routines, with the same result. I'll try to find delay loops within complete code and see what's the line I'm missing.

Under what circumstance did you get the "stack underflow" message?
 
When I did not have this line :

main
btfsc PORTB,6
goto main

Now I don't get the message, but the chip hangs, and doesn't go through the delay code. It hangs halfway through. I'm thinking it's something to do with the oscillator set up. I'm working on it, but not tonight, since I have something to do.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top