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.

errors in mplab simulator

Status
Not open for further replies.

danuke

New Member
I'm having trouble in MPLab Sim. I've written a small program to see if I have grasped some of the theory behind the programming language. Everytime I try to debug/simulate it, it gives the error: "CORE-E0002: Stack under flow error occurred from instruction at 0x000005".

Can someone look at the program and see where I'm messing up? I've got it assigned to program a 16F628A. I've got the watch window open and viewing the data on PORTA, and that's really all that the program is supposed to do is make the PORTA data go 0000 > 0001 > 0011 then repeat.

Thanks,
Joe



;**********************************************************************
; *
; Files required: *
; 16F628A.lkr *
; *
; *
;**********************************************************************

list p=16F628A ; list directive to define processor
#include <p16F628A.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file

__CONFIG _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT



;***** VARIABLE DEFINITIONS (examples)

PORTA EQU 05h
TRISA EQU 85h

;**********************************************************************

MAIN CODE


main

;******SETUP PORTA******
BSF STATUS, 5
MOVLW 00H
MOVWF TRISA
BCF STATUS, 5

START BSF PORTA, 0
BSF PORTA, 1
BSF PORTA, 2
MOVLW 00h
MOVWF PORTA

GOTO START



END ; directive 'end of program'
 
You forgot to include an
Code:
org 0x000
at the beginning

Also no need to redefine the PORTA & TRISA, your include file does that for you.
 
Last edited:
blueroomelectronics said:
You forgot to include an
Code:
org 0x000
at the beginning

Also no need to redefine the PORTA & TRISA, your include file does that for you.

included as follows:
org 0x000
list p=16F628A ; list directive to define processor
#include <p16F628A.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file

I also removed the defining of the TRISA and PORTA as suggested. It still comes up with the error after rebuilding all. :confused:
 
danuke said:
included as follows:


I also removed the defining of the TRISA and PORTA as suggested. It still comes up with the error after rebuilding all. :confused:


disregard the last post. I got it to working now. Thanks for the help!
 
Put a comment ; in front of the words "MAIN CODE" or remove it.
This is being treated as a code command by the linker.

You have the org statment in the wrong place.
It should be on line above the word "main" for this example.

You don't need to compile with the linker - that is for re-locatable code.
 
danuke said:
BSF PORTA, 1
BSF PORTA, 2

That is not good practice.

You should not change one bit in a port on the line after you have changed another line.

The reason is that each instruction reads the port, modifies one bit, and writes all 8 bits back to the port.

The voltage on the port is read only about 1 oscillator cycle after it has been set. If there is some small capacitance on the port, the voltage will not have been established, and the second instruction will read porta, 1 as low so it will be written as low just as porta, 2 goes high.
 
picasm said:
Put a comment ; in front of the words "MAIN CODE" or remove it.
This is being treated as a code command by the linker.

You have the org statment in the wrong place.
It should be on line above the word "main" for this example.

You don't need to compile with the linker - that is for re-locatable code.

When I remove the MAIN CODE line and move the org line to be right above the "main", it starts giving an error of "Error - section '.org_0' can not fit the absolute section. Section '.org_0' start=0x00000000, length=0x00000012".

I'm new to the MPLab software so I probably sound green behind the ear to some of you more knowledgeable people.


Diver300 said:
That is not good practice.

You should not change one bit in a port on the line after you have changed another line.

The reason is that each instruction reads the port, modifies one bit, and writes all 8 bits back to the port.

The voltage on the port is read only about 1 oscillator cycle after it has been set. If there is some small capacitance on the port, the voltage will not have been established, and the second instruction will read porta, 1 as low so it will be written as low just as porta, 2 goes high.

I will try to refrain from doing back to back bit changing in my coding from now on. I'm mainly trying to get some experience with MPLab. If looking to change more than one bit I'd write the to the w register then to the porta register. I appreciate the help in coding practice and will try to keep it in mind for the future.
 
Works fine.
Code:
        list    p=16F628A 
        include <p16F628A.inc> 
        errorlevel -302 
        __CONFIG _LVP_OFF & _WDT_OFF & _INTOSC_OSC_NOCLKOUT
MAIN
;******SETUP PORTA******
        BSF     STATUS, RP0
        clrf    TRISA
        BCF     STATUS, RP0

START   BSF     PORTA, 0
        BSF     PORTA, 1
        BSF     PORTA, 2
        clrf    PORTA
        GOTO    START

        END
 
danuke said:
When I remove the MAIN CODE line and move the org line to be right above the "main", it starts giving an error of "Error - section '.org_0' can not fit the absolute section. Section '.org_0' start=0x00000000, length=0x00000012".

I think that error is caused by using the linker script,which adds a bit too much complexity at this stage of learning.
To remove the linker script, click on "project" "remove file from project" then click on the "16f628a.lkr" file to remove it.
 
thanks. Removing the linker file fixed the error I was receiving.

Whenever I'm in the watch mode watching the PORTA, it doesn't change the values as I single step through it using the debugger. Any ideas on what may be causing that? The values of TRISA change from FF to 20 though. But PORTA never changes values?
 
danuke said:
Whenever I'm in the watch mode watching the PORTA, it doesn't change the values as I single step through it using the debugger. Any ideas on what may be causing that? The values of TRISA change from FF to 20 though. But PORTA never changes values?
Add these lines to your program:
Code:
main
    movlw    0x07    
    movwf    CMCON

and re-start the simulation.
 
list p=16f84a ; initialize to the correct PIC type
#include <p16f84a.Inc>
errorlevel -302

org 0x0000 ;Reset vector



Bsf 03h,5
Movlw 01h
Movwf 85h
Bcf 03h,5


end


I ALSO GOT THE SAME Error "CORE-E0002: Stack under flow error occurred from instruction at 0x000005"

can anyone help me pls?
 
I think that the problem is what your program does after it has executed all your code. You haven't got a goto at the end to make it loop somewhere, and line 0x000005 is after the 4 lines of code in your example.

Most programs have an initialise that runs once, then a loop that cycles forever after that.

There a a couple of ways you could make things easier for yourself.

1) Use a pic16F627a. It is newer, better and cheaper than a pic16F84A

2) Use the names of registers and bits. It makes code easier to read. That is why you have the include file. The geeks and anoraks (https://en.wikipedia.org/wiki/Anorak_(slang)) here will know that:-

Bsf 03h,5
means
Bsf status, rp0

which sets the banking bit to page 1, but there is no point making it more difficult to read.

Similarly, 85h is the TRISA register (short for Tri-state, port A) and it is easier to refer to it as that.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top