Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

PIC "Hello, World!" program fails with "stack under flow"
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PIC "Hello, World!" program fails with "stack under flow"

by brunnenguk on Wed May 05, 2010 11:26 am

Hi all, I'm starting out learning assembly for the PIC microcontroller. The main resource I've been using for this is "The PIC Microcontroller, your personal introductory course" by John Morton. In terms of hardware/software I'm working with MPLAB 8.5, using a PicKit2 to program microchips 44-pin Demo board (board contains a 16F887 controller)

As a starter I'm using a program based on the one introduced in the introduction (and included in the apppendix, "program A" for anyone familiar with the book). The aim is to switch on one of the LEDs on the board. The bank of LEDs are connected to the ports on RD.

First problem I encountered was that the program uses the "tris" instruction which does not seem to work for ports higher than B, so instead I've tried to use the movfw instruction to move the bitmask to trisd. The program gets stuck looping through "Init" and fails to get any further. I expect I've probably made a really noobish error that's going to make me look (and feel) really silly :-(

The code is as follows:
Code: Select all | TOGGLE FULL SIZE
list P = 16F887
include "C:\Program Files\Microchip\MPASM Suite\p16f887.inc"

portd equ 08
trisd equ 88

org 1FFh
goto Start
org 0

Init clrf portd
movlw b'00000000'
movfw trisd
retlw 0

Start
call Init

Main
bsf portd,0
goto Main
END


As there is no definitaion for portd and trisd I've defined these myself. I checked the spec sheet for the 887 and they appear to be correct.

the error that I get when simulating is:
CORE-E0002: Stack under flow error occurred from instruction at 0x000003

Is this something to do with the "org 1FFh" instruction?

EDIT:
Comparing datasheets, org 1FFh refers to the reset vector. Between the 54 and 887, the reset vector seems to have been moved from the end (54) to the beginning of memory. I'm confused :(
brunnenguk
 
Posts: 9
Joined: Fri Nov 28, 2008 12:00 pm

Re: PIC "Hello, World!" program fails with "stack under flow"

by brunnenguk on Wed May 05, 2010 4:01 pm

Update:

Did some reading and found that 1FFh was the reset vector for the chip used in the book. the 16F887 uses memory location 0 as a reset vector so I replaced it with the appropriate value and it now executes the main method when it should.

It still doesn't actually light up the LED but looks like I need to do some bank switching to access the trisd register.
brunnenguk
 
Posts: 9
Joined: Fri Nov 28, 2008 12:00 pm

Re: PIC "Hello, World!" program fails with "stack under flow"

by oPossum on Wed May 05, 2010 5:28 pm

Code: Select all | TOGGLE FULL SIZE
movlw  b'00000000'
bsf    STATUS, RP0  ; Select register bank 1
movwf  PORTD        ; TRISD
bcf    STATUS, RP0  ; Back to register bank 0
I am the Possum, and I approve of this message. Sent from MacBook Wheel Sorry for my bad German.
oPossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am
Location: Michigan, USA

Re: PIC "Hello, World!" program fails with "stack under flow"

by brunnenguk on Fri May 07, 2010 9:00 am

Thanks for the help oPossum :-)

Still not getting anything lighting up so I'll see if I can set up virtual breadboard (or another application along those lines) to test it in a simulated controller.
brunnenguk
 
Posts: 9
Joined: Fri Nov 28, 2008 12:00 pm

Re: PIC "Hello, World!" program fails with "stack under flow"

by westfw on Fri May 07, 2010 11:26 am

Just to check: do you have JP1 installed on the demo board?

westfw
 
Posts: 1720
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: PIC "Hello, World!" program fails with "stack under flow"

by brunnenguk on Fri May 07, 2010 11:38 am

Yes, JP1 is installed
brunnenguk
 
Posts: 9
Joined: Fri Nov 28, 2008 12:00 pm

Re: PIC "Hello, World!" program fails with "stack under flow"

by westfw on Fri May 07, 2010 6:48 pm

What does the current iteration of code look like?

westfw
 
Posts: 1720
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: PIC "Hello, World!" program fails with "stack under flow"

by brunnenguk on Sat May 08, 2010 10:32 am

Here is the code:
Code: Select all | TOGGLE FULL SIZE
   list   P = 16F887
   include   "C:\Program Files\Microchip\MPASM Suite\p16f887.inc"

;=============
; Declarations

portd   equ 08 ; portd has LED built in, use this
trisd   equ 88 ; for some reason lacks a definition

      org 0
      goto Start

;============
; Subroutines
Init   clrf portd

      movlw b'00000000'
      bsf STATUS,RP0   ; select register bank 1
      movwf trisd
      bcf STATUS,RP0   ; return to bank 0

      retlw 0

;==============
; Program Start
Start
      call Init

Main
      bsf portd,0 ; turn on first LED
      goto Main
      END
brunnenguk
 
Posts: 9
Joined: Fri Nov 28, 2008 12:00 pm

Please be positive and constructive with your questions and comments.