Stack Under flow


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2005
    Location
    Hernando,Florida USA
    Posts
    8

    Red face Stack Under flow

    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
    '*************************************
    Last edited by Wayne; - 14th September 2005 at 02:07. Reason: more data

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    sinoteq's Avatar
    sinoteq Guest


    Did you find this post helpful? Yes | No

    Default GOTO and GOSUB

    .................................................. .........................
    Last edited by sinoteq; - 14th September 2005 at 01:52.

  4. #4
    Join Date
    Sep 2005
    Location
    Hernando,Florida USA
    Posts
    8


    Did you find this post helpful? Yes | No

    Default goto and gosub

    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

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Code:
    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
    Code:
    WDTCON		    equ     105h
    LCDCON		    equ     107h
    but shouldn't be the cause IMO
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Sep 2005
    Location
    Hernando,Florida USA
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Mplab Sim

    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

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    So to be clear does
    Code:
    main:
    GoSub getkey
    Goto Start     ' <=== Add this line
    
    getkey:
    kp1=0 'Clear kp1
    fix the problem... i guess yes !?!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Sep 2005
    Location
    Hernando,Florida USA
    Posts
    8


    Did you find this post helpful? Yes | No

    Talking stack under flow

    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

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    Sep 2005
    Location
    Hernando,Florida USA
    Posts
    8


    Did you find this post helpful? Yes | No

    Smile stack underflow

    OK makes sence


    Again Thanks for your help




    Wayne

Similar Threads

  1. Hall Effect flow meter with 16F876
    By revelator in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th March 2010, 20:42
  2. Software Stack
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th December 2007, 10:04
  3. Stack over flow with IF THEN RETURN ENDIF???
    By kdoske in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 9th December 2007, 17:20
  4. hot water flow sensor
    By jessey in forum Off Topic
    Replies: 16
    Last Post: - 24th January 2007, 04:05
  5. 48 level Software Stack
    By Darrel Taylor in forum Code Examples
    Replies: 0
    Last Post: - 30th November 2003, 01:07

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts