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.

pic16f88 LED flash,error using MPLAB

Status
Not open for further replies.

mseta47

New Member
i am writing a code for a pic16f88 to turn the LED on and off,when i try to run.it reports "
CORE-E0002: Stack under flow error occurred from instruction at 0x000000"
here is my code

; Example of using Shared Uninitialized Data Section
INT_VAR UDATA_SHR
W_TEMP RES 1 ; w register for context saving (ACCESS)
STATUS_TEMP RES 1 ; status used for context saving (ACCESS)
PCLATH_TEMP RES 1 ; variable used for context saving

; Example of using GPR Uninitialized Data
GPR_VAR UDATA
MYVAR1 RES 1 ; User variable placed by linker
MYVAR2 RES 1 ; User variable placed by linker
MYVAR3 RES 1 ; User variable placed by linker

;------------------------------------------------------------------------------
; EEPROM INITIALIZATION
;
; The 16F88 has 256 bytes of non-volatile EEPROM, starting at address 0x2100
;
;------------------------------------------------------------------------------

DATAEE CODE 0x2100
DE "MCHP" ; Place 'M' 'C' 'H' 'P' at address 0,1,2,3

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

RESET CODE 0x0000 ; processor reset vector
; pagesel START
; GOTO START ; go to beginning of program

;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

INT_VECT CODE 0x0004 ; interrupt vector location

; Context saving for ISR
MOVWF W_TEMP ; save off current W register contents
MOVF STATUS,W ; move status register into W register
MOVWF STATUS_TEMP ; save off contents of STATUS register
MOVF PCLATH,W ; move pclath register into W register
MOVWF PCLATH_TEMP ; save off contents of PCLATH register

;------------------------------------------------------------------------------
; USER INTERRUPT SERVICE ROUTINE GOES HERE
;------------------------------------------------------------------------------

; Restore context before returning from interrupt
MOVF PCLATH_TEMP,W ; retrieve copy of PCLATH register
MOVWF PCLATH ; restore pre-isr PCLATH register contents
MOVF STATUS_TEMP,W ; retrieve copy of STATUS register
MOVWF STATUS ; restore pre-isr STATUS register contents
SWAPF W_TEMP,F
SWAPF W_TEMP,W ; restore pre-isr W register contents
RETFIE ; return from interrupt

;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------

;PROGRAM CODE

;START

;------------------------------------------------------------------------------
; PLACE USER PROGRAM HERE

;------------------------------------------------------------------------------
STATUS equ 03h ;addres of the status register
TRISA equ 85h ;address of the tristate register for port A
PORTA equ 05h ;Address of port A
COUNT1 equ 08h ;FIRST COUNTER FOR OUR DELAY LOOP
COUNT2 equ 09h ;SECOND COUNTER FOR OUR DELAY LOOP
;******SET UP THE PORTS*******
bsf STATUS,5 ;switch to bank 1
movlw 00h ;set the port A pins
movwf TRISA ;to output
bcf STATUS,5 ;switch back to bank 0

;********TURN ON LED*******
start movlw 02h ;turn the LED on by first putting it into the w register and than on the port
movwf PORTA

;******start the delay loop1********
loop1 decfsz COUNT1,1 ;Subtract 1 from 255
goto loop1 ;if count is zero,carry on
decfsz COUNT2,1 ;Subtract 1 from 255
goto loop1 ;go back to the start of our loop.


;******TURN OFF LED**********
movlw 00h ;turn the led off by first putting
movwf PORTA

;******ADD ANOTHER DELAY*******
loop2 decfsz COUNT1,1 ;Subtract 1 from 255
goto loop2 ;if count is zero,carry on
decfsz COUNT2,1 ;Subtract 1 from 255
goto loop2 ;go back to the start of our loop.


;------------------------------------------------------------------------------

GOTO $

END
 
Hi,

Thats a very poor example of assembly code.

Here is some easier to follow code, also see these good tutorials





https://www.amqrp.org/elmer160/lessons/

https://www.gooligum.com.au/tutorials.html




Code:
; flash pins RB2 and RB3 at 1 second intervals using 4mhz internal oscillator


    LIST    P=PIC16F88
       
    #include    <P16f88.inc>
    org        0x00


; CONFIGURATION*-*-*-*-*-*-*-*-*-
;*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

     __CONFIG  _CONFIG1, _LVP_OFF &_WDT_OFF &_PWRTE_ON &_CP_OFF & _INTRC_IO & _MCLRE_OFF


; user_ registers:
    cblock 0x20

    d1,d2,d3            ; delay work files
   
    endc

reset:
    nop
    goto    start
    org        0x20

start:   
; oscillator                            ; 4Mhz internal clk, mode defined
    movlw        b'01100100'           
    movwf        OSCCON                    ; system clock source from main oscillator

; led
   
    banksel        PORTA                     ; select bank PORTA
    clrf        PORTA
    clrf        PORTB                        
    banksel        ANSEL                    ; Configure all pins as digital inputs   
    clrf        ANSEL   
    movwf        0xFF                    ; make PortA inputs
    movwf        TRISA
    clrf        TRISB                    ; make PORTB output


; MAIN PROGRAM*-*-*-*-*-*-*-*-*-*
;*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Led_flicker:                            ; FLASH A LED ON AND OFF RB3
    banksel        PORTB
    bsf            PORTB,3                    ; turn on led
    bcf            PORTB,2
    call        delay1s                    ; delay 1/2 second
    bcf            PORTB,3
    bsf            PORTB,2
    call        delay1s                    ; delay 1/2 second
    goto        Led_flicker                ; return to start of the loop
;---end Led flicker


; SUB ROUTINES*-*-*-*-*-*-*-*-*-*
;*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


delay1s                                    ; 1 second delay based on 4mhz oscillator
    movlw    0x08
    movwf    d1
    movlw    0x2F
    movwf    d2
    movlw    0x03
    movwf    d3
Delay_0
    decfsz    d1, f
    goto    dy2
    decfsz    d2, f
dy2    goto    dy3
    decfsz    d3, f
dy3    goto    Delay_0

    return


; END MAIN PROGRAM*-*-*-*-*-*-*-*-*-*
;*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    end
 
In the top example someone has commented out the reset vector... So it runs straight into the ISR ending with the retfie... Underflow as the interrupt wasn't called...

Un comment the
goto Start Line.. and the Start label..
 
Status
Not open for further replies.

Latest threads

Back
Top