PDA

View Full Version : Stack Under flow



Wayne
- 13th September 2005, 23:39
I seem to have a problem with the stack.
But a stack under flow ??
When my program get to a return I get this message
"Stack under flow error occurred from instruction at 0x000126"
the return instruction is at 0x000126.
The tools are PIC Pro 2.46 MPLAB 7.20 and the device is 16F913
Just don't know where to start with this problem.
I start the program, it will go through the getkey loop 2 times
and then give me the stack error. then it resets and starts over.

************************************************** ****

Define osc 8
OSCCON = %01110001 'SET FOR INTERNAL CLK @8 MHz
WDTCON = %00000000
LCDCON = %01000000
LCDSE0 = %00000000
LCDSE1 = %00000000
ANSEL = 0
CMCON0 = $07


TRISB=%00001111 'set PORTb, 1=input, 0=output
TRISC=%11110000 'set PORTc, 1=input, 0=output
TRISA=%11100100 'set PORTa, 1=input, 0=output


PORTB.4=0 ' set portb pin 4 low for LED sink
PORTA.0=0
INTCON = %00000000 'Turn interrups off

kp VAR BYTE 'Key press value
kp1 VAR BYTE 'Key press
LED1 VAR PORTA.0
LED2 VAR PORTA.1
LED3 VAR PORTA.3
LED4 VAR PORTA.4
LEDCOM VAR PORTB.4
TESTPIN VAR PORTC.5
COL1 VAR PORTC.0 'KeyPad col1
COL2 VAR PORTC.1 'KeyPad col2
COL3 VAR PORTC.2 'KeyPad col3
COL4 VAR PORTC.3 'KeyPad col4
ROW VAR BYTE
i VAR BYTE 'for next variable

'************************************************* **
start:

LED1=0 '
LED2=0 ' set all leds to Off
LED3=0 '
LED4=0 '
LEDCOM=0
kp=16
kp1=0


LED1 = 1
Pause 500
LED2 = 1
Pause 500
LED3 = 1
Pause 500
LED4 = 1
Pause 500
LED1 = 0
LED2 = 0
LED3 = 0
LED4 = 0

main:
GoSub getkey

getkey:
kp1=0 'Clear kp1


'col4

PORTC=%00000111 'Make PORTC.3 low PINs .0.1.2 High
kp1 = PORTB & $0f
IF kp1=14 Then kp=13
IF kp1=13 Then kp=14
IF kp1=11 Then kp=15
IF kp1=7 Then kp=15
IF kp1<>15 Then debounce

'col3

PORTC=%00001011 'Make PORTC.2 low PINs .3.1.0 High
kp1 = PORTB & $0f
IF kp1=14 Then kp=3
IF kp1=13 Then kp=6
IF kp1=11 Then kp=9
IF kp1=7 Then kp=12
IF kp1<>15 Then debounce

'col2

PORTC=%00001101 'Make PORTC.1 low PINs .3.2.0 High
kp1 = PORTB & $0f
IF kp1=14 Then kp=2
IF kp1=13 Then kp=5
IF kp1=11 Then kp=8
IF kp1=7 Then kp=10
IF kp1<>15 Then debounce

'col1

PORTC=%00001110 'Make PORTC.0 low PINS .3.2.1 High
kp1 = PORTB & $0f
IF kp1=14 Then kp=1
IF kp1=13 Then kp=4
IF kp1=11 Then kp=7
IF kp1=7 Then kp=11
IF kp1<>15 Then debounce

GoTo nokey
debounce:
pause 50
row = PORTB & $0f
if row = $f then showkp
goto debounce

showkp: For i = 1 to kp
High led1
Pause 500
Low led1
Pause 500
Next i
'DTMFout PORTC.5,[kp]
nokey:

Return
end
'*************************************

mister_e
- 13th September 2005, 23:55
look if there's a part of your code ended with a return but uncalled by a Gosub

well this is the only thing that make sense to me.

Will be easier to say if you post your code Here.

sinoteq
- 14th September 2005, 01:43
.................................................. .........................

Wayne
- 14th September 2005, 02:00
HI Sinoteq
There is one gosub and one return. so I would think the return
would go back to GETKEY. Yes
Or did I not understand your comment.

Thanks
Wayne

mister_e
- 14th September 2005, 02:22
start:
'
'
'

LED1=0 '

main:
GoSub getkey

getkey:
kp1=0 'Clear kp1

after Main, should have a goto start or something like that :)

But i didn't get any error within MicroCode Studio using MPASM but get error with PM about WDTCON and LCDCON... in the PM M16F91X.inc file it miss those two lines


WDTCON equ 105h
LCDCON equ 107h

but shouldn't be the cause IMO

Wayne
- 14th September 2005, 02:30
Hi Steve
You only get the error if you use the MPLAB SIM tool or program the PIC
and install it in the board.


Thanks
Wayne

mister_e
- 14th September 2005, 02:32
So to be clear does


main:
GoSub getkey
Goto Start ' <=== Add this line

getkey:
kp1=0 'Clear kp1

fix the problem... i guess yes !?!

Wayne
- 14th September 2005, 02:52
Hi Steve (MR. E)

Yes that did work and thank you very much for the help.
But one more thing WHY and what is a stack underflow?

Again Thanks

Wayne

mister_e
- 14th September 2005, 12:36
A short explanation:
once the program RETURN from GETKEY, it RETURN after the GOSUB line, then do once again the GETKEY sub, meet a RETURN without a previous gosub.

Gosub => stack+1 => stack = +
Return => stack-1 => stack = 0
Return +> stack-1 => stack=-1 <=== here's the underflow ;)

Wayne
- 14th September 2005, 13:22
OK makes sence


Again Thanks for your help




Wayne