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.

stack overflow problem in pic 16f628a

Status
Not open for further replies.

rakhiwilliams

New Member
hi,
we are doing project using pic 16f628a.we completed coding (in assembly)
we are debugging our code using MPLAB simulator.here we are getting stackoverflow error at table(we are using for 7 seven segment display).
how to rectify this problem.we dont know the reason why we are getting this type of error.please anybody explain this.
thank you,
 
pic problem

sir,
could you tell me the reasons when will we get this stackoverflow problem.how can we overcome this problem generally.
 
Stack overflow means you have used all the available stack locations. The pic only has 8 (2 on some) stack locations. This is normally enough. Are you doing a call without a return by any chance?
I.E.
Code:
Loop		Call	Subroutine
		goto	Loop


Subroutine	;some code
		goto	Loop

The above would cause a stack overflow because every time the call is made a return address is placed on the stack.

Mike.
 
As Pommie says, or if you have multiple call's without returning them.

e.g. one routine calls another. Within this called routine, you have yet another call, and so on and so forth... If you have these repeats for more than 8 levels, you will hit the Stack Overflow error

I have had to use is some cases the dreaded GOTO, to reduce the number of calls. The problem with a GOTO routine is that you would almost always have to return to the original caller with a GOTO command back to the requesting routine, which makes the code not really re-usable as opposed to CALL routines.

As Nigel said, post the code, some of the Guru's could possibly help you debug, else you will have to run the MPLAB simulation yourself, and keep note of the STACK Register.
 
Improperly constructed interrupt service routines can do the same thing. It is a 99.999% chance that the problem is in your code. Review it carefully, simulate it if necessary, pay someone to fix it if all else fails.
 
sir,
Though there is a stack overflow error, we are getting output correctly .
we dont know whether there will be any problem in future.what will happen to registers when stack overflow occurs.
 
To ask that question, I am guessing that the overflow is caused by using call instead of goto or goto instead of return (the alternative is that you have nested calls deeper than 8 - unlikely - unless your code is very complex or your imitating a windoze programmer :D :D ). Post your code and we may be able to fix it or have a guess what will happen in the real world.

Mike.
 
we are getting stackoverflow error at table(we are using for 7 seven segment display).
You need to call the table lookup function. Do not use goto.
For example:

Code:
; # is in W for lookup table
[B]call[/B] getcode

getcode
	addwf PCL,F
codes
	retlw Key1
	retlw Key6
	retlw Key5
	retlw Key9

Do NOT do this:
Code:
goto getcode
 
Pommie said:
Your example would cause a stackunderflow error.
Mike.
The code was just an example, not something you could compile and run. To clarify:

Code:
; # is in W for lookup table and is between 0 and 3
call getcode
[B]goto somewhere[/B]

getcode
	addwf PCL,F
codes
	retlw Key1
	retlw Key6
	retlw Key5
	retlw Key9

EDIT: Ok Mike. I see what you mean now. The goto getcode would cause an underflow not an overflow. :eek:
 
Last edited:
hi

i also get error message stack overflow. the problem is, when i simulated this program using MPLAB IDE, it runs perfectly for the first cycle but it stopped running when it reaches the middle of the program for 2nd cycle. i tried to put return for every subroutine i sued..but it still doesn't work

it always stopped at subroutine WaitxTE which i pasted below. can you check this subroutine for me please?

WaitxTE
movwf Count2 ; [1]

waitxlp
movlw D'30' ; [1]
movwf Count ; [1]

;wait400lp
;nop ; [1]
;nop ; [1]
;decfsz Count,F ; [1]
; goto wait400lp ; [2]
; --------
; 79 x 5 = 395us

decfsz Count2,F ; [1]
goto waitxlp ; [2]

retlw 0 ; [2]


did i miss something??

please help me..i've been working for this program quite a long time now
 
sorry..the code is below

Code:
;nur azrin hilmi
;gdp 17-telemetry 
;use external clock

 	list        P=pic12f675        ; list directive to define processor
 	#include    <p12f675.inc>  ; processor specific variable definitions


;************************************************************8
; declaring all the registers



	cblock 0x20

	
	status_temp		
	w_temp		

	
	CSR0                      ; TX buffer shift register
	CSR1			
;	CSR2			
;	CSR3			
;	CSR4			
;	CSR5			
;	CSR6			
;	CSR7			
;	CSR8		
	
	Count		
	Count2		
	BitCount		
			
	counter
	TimeHi
	TimeLo		
	count			
	address
	FuncBits  
	input
	store
	nobit  
	rotateright                
	endc	
;***********************************************************


;************setting the configuration bit******************8

; Data Memory Code Protection bit:  disabled
; _CPD_ON = Enabled
; _CPD_OFF = Disabled
;
; Program Memory Code protection: disabled
; _CP_ON = Enabled
; _CP_OFF = : Disabled
;
; Brown-out Detection Enable bit: disabled
; _BODEN_ON = Enabled
; _BODEN_OFF = Disabled
;
; GP3/MCLR pin function select:  mclr_on
; _MCLRE_ON = GP3/MCLR pin function is /MCLR
; _MCLRE_OFF = GP3/MCLR pin function is digital I/O, 
;              /MCLR internally tied to Vdd
;
; Power-up Timer Enable bit: disable
; _PWRTE_ON = Enabled 
; _PWRTE_OFF = Disabled
;
; Watchdog Timer Enable bit: disable
; _WDT_ON = Enabled
; _WDT_OFF = Disabled
;
; Oscillator Selction bits: _INTRC_OSC_NOCLKOUT
; _EXTRC_OSC_NOCLKOUT = CLKOUT function on GP4 pin, RC on GP5 pin.
; _EXTRC_OSC_CLKOUT = I/O function on GP4 pin, RC on GP5 pin.
; _INTRC_OSC_CLKOUT =  Internal oscillator, CLKOUT function on GP4 pin,
;                      I/O function on GP5 pin.
; _INTRC_OSC_NOCLKOUT = Internal oscillator, I/O function on GP4 and GP5 pins.
; _EC_OSC = I/O function on GP4 pin, CLKIN on GP5 pin.
; _HS_OSC = High speed crystal/resonator on GP4 and GP5 pins.
; _XT_OSC = Crystal/resonator on GP4 and GP5 pins.
; _LP_OSC = Low power crystal on GP4 and GP5 pins.
;


    __CONFIG    _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_ON & _PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
;***************************************************************
;**************define variables*********************************

	#define	sensor	GPIO, 1
	#define txd     GPIO, 2             ; (Output) Transmit Data
	#define pb3     GPIO, 3             ; (Input Only) Push button switch GP3
	#define pb4     GPIO, 4             ; (Input) Push button switch GP4
	#define rfena   GPIO, 5             ; (Output) RF Enable

 	; GPIO Pins =    xx543210
;	#define gptris b'00011011'

	#define TGUARD  d'18'
;	#define rotateright d'1'	
	#define	countermax d'8'
;***************************************************************

	org  0x00
	goto initialize
	
	org  0x004
	
	movwf	w_temp
	swapf	STATUS,W
	bcf		STATUS, 5		;select bank 0
	movwf	status_temp		;save status register
;***************************************************************

;	interrupt service routine (ISR)
;   interrupt on change on pin of gpio

	movfw	GPIO
	bcf		INTCON,0
	
;**************************************************************

	swapf	status_temp,w	;set bank into original state
	movwf	STATUS
	swapf	w_temp,F
	swapf	w_temp,W
	
	retfie

;***************************************************************
;subroutine data_eeprom read
data_eeprom_read 

	bsf  	STATUS,5
	movwf	EEADR
	bsf		EECON1,0
	movfw   EEDATA
	bcf     STATUS,5



data_eeprom_write

	bsf		STATUS,5
	bsf		EECON1,2
	bcf		INTCON,7

	movlw	0x55
	movwf	EECON2
	movlw	0xAA
	movwf	EECON2
	bsf		EECON1,1

	bsf		INTCON,7
	bcf		EECON1,2

	bcf     STATUS,5

	return

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

WaitxTE
        movwf   Count2      ; [1]

waitxlp            
        movlw   D'30'       ; [1]
        movwf   Count       ; [1]

;wait400lp
      ;nop                 ; [1]
      ;nop                 ; [1]
     ;decfsz  Count,F     ; [1]
    ; goto    wait400lp  ; [2]
;                          --------
;                         79 x 5 = 395us

      decfsz  Count2,F    ; [1]
     goto    waitxlp    ; [2]

        retlw   0           ; [2]

;                       total 2 (call) + W x (395 + 5) + 2 (return)
;                       w = 1 -> 406us @4MHz
;                       w = 2 -> 806us @4MHz
;*************************************************************
;initialize pic
;*************************************************************

initialize

;clearing all ports
	CLRF	nobit
	clrf	input
	clrf	store
	clrf	rotateright
	clrf	Count
	clrf	Count2
;disable global interrupts during initialization

	bcf     INTCON,7

;calibrating the internal oscillator

;	bsf     STATUS,5		;select bank 1
	call	0x3FF
	movwf	OSCCAL		
;	bcf		STATUS,5
	
;turn off the comparator
	bsf		STATUS,5

	clrf 	ANSEL			; clear ansel register
	bcf		STATUS,5
	
	clrf 	GPIO			; initialize General Purpose Input Output to 0
		
;setting gpio register to the cosrresponding io pin
	bsf 	STATUS,5
	
	bcf		TRISIO,0
	bsf		TRISIO,1
	bsf		TRISIO,4
	bcf		TRISIO,2
	bCf		TRISIO,5

	bcf     STATUS,5		;select bank 0

;set bit 0:2 at register cmcon	;setting gp2 as digital output |offkan comparator
	bsf		STATUS,5
	bsf		CMCON,0
	bsf		CMCON,1
	bsf		CMCON,2
	BCF		STATUS,5
; VRCON	register ; CVref circuit powered down,no Idd drain

	bsf		STATUS,5
	bcf		VRCON, 7
	bcf		STATUS,5


;****************************************************************
;****timer1 module gate control**********************************
; disabling timer1
	bsf		STATUS, 5		; select Bank 1
	movlw   b'01000100'		; disable TIMER1
	movwf	T1CON		;
	bcf		STATUS, 5		; select Bank 0

;****************************************************************
;weak pull-up register (wpu)
;each pull-up is automatically turned off when the pin is configured as an output
	bsf     STATUS,5
	movlw	b'00010000'
	movwf	WPU
	bcf     STATUS,5

;***************set pin for config_address regsiter

;OPTION Register 

;   OPTION_REG contains control bits to configure:
;   Weak pull-ups on GPIO (see also WPU Register above)
;   External GP2/INT interrupt
;   TMR0
;   TMR0/WDT prescaler

        bsf     STATUS,5	         	; ---- Select Bank 1 -----

        bcf     OPTION_REG, NOT_GPPU  	; GPIO pull-ups: enabled

        bsf     OPTION_REG, INTEDG  	; Interrupt Edge: on rising edge of GP2/INT pin

        bcf     OPTION_REG, T0CS    	; TMR0 Clock Source: internal instruction cycle (CLKOUT)
        bcf     OPTION_REG, T0SE    	; TMR0 Source Edge: increment low-to-high transition on GP2/T0CKI pin

        bcf     OPTION_REG, 3     		; Prescaler Assignment: assigned to TIMER0

        ; TMR0 Prescaler Rate: 1:2
        bcf     OPTION_REG, 2     		; Prescaler Rate Select bit 2
        bcf     OPTION_REG, 1     		; Prescaler Rate Select bit 1
        bcf     OPTION_REG, 0     		; Prescaler Rate Select bit 0

        bcf    STATUS, 5         		; ---- Select Bank 0 -----




;***************************************************************
;interrupt on-change register
	bsf     STATUS,5
	
	movlw  	b'00011010'		;enable global interrupts in order for the individual pin to be configured(kalo switch
	movwf	IOC

	movlw	b'00000000'		;peripheral interrupt register; disable
	movwf 	PIE1			

	bcf    STATUS,5

	movlw	b'00001000'		;enable interrupt control register 
	movwf	INTCON
	

;******************************************************************
;MAIN PROGRAM
;*****************************************************************8
MAIN

              
        bsf     INTCON,7  ; enable global interrupts
	;	bsf		rfena     ; enable	transmitter
		goto    BITCHECK
		
;*****************************************************************
;CHECK INPUT
;*****************************************************************

	movlw	d'1'
	movwf	rotateright	
BITCHECK
	incf	nobit,f
	movfw	GPIO
	movwf	input
	
	movlw	0x00
	xorwf	input,f	

	btfsc	nobit,0
	goto	bit0
    goto	bit1

bit0

	rrf		input,w
	movwf	store
	goto	BITCHECK
bit1

	movlw	0x00
	xorwf	input,w
	xorwf	store,f
	
checkbit
	btfsc	nobit,3
	goto	TRANSMIT
	incf	nobit,f
	movfw	nobit
	movwf	rotateright
	movfw	GPIO
	movwf	input

	movlw	0x00
	xorwf	input,f
	decf	rotateright
	decf	rotateright

rotate
	
	rlf		input,w
	xorwf	store,f
	decfsz  rotateright,f
	goto	rotate
	goto	checkbit
	goto	TRANSMIT

TRANSMIT
		bsf		GPIO,5
; send preamble (50% duty cycle)
Preamble					;send 16 bit
      movlw   d'5'
      movwf   BitCount          ; init number of preamble bits

PreL
        bsf     txd             ; ON
        movlw	1
		call    WaitxTE           ; delay
        bcf     txd             ; OFF
       	movlw	1
		call    WaitxTE             ; delay
        decfsz  BitCount,F      ; loop
        goto    PreL
            
; sync pause

TXloop      
                
       movlw d'10'
	   call    WaitxTE         ; Theader = 10 x Te
            
; send 72 bit pattern

        movlw   CSR1            ; lsb first 
        movwf   FSR
            
TXNextByte
      movlw   D'8'
      movwf   BitCount

TXNextBit
		
        rrf     INDF,W           ; 8 bit rotate | rotate right f through carry
        rrf     INDF,F           ; Carry contain lsb
        BC      ONE				 ; branch on carry | goto ONE if bit is set
ZERO    					;wave pattern if the bit is zero        
        movlw   2               ; 
	    movwf   TimeHi          ;   +---+---+   +--
        movlw   1               ;   |       |   |
   		movwf   TimeLo          ;---+       +---+
        goto    Trasm_BIT       ;   |  2Te   Te |

ONE							;wave pattern if the bit is one
        movlw   1              	;              
        movwf   TimeHi          ;   +---+       +--
        movlw   2               ;   |   |       |
        movwf   TimeLo          ;---+   +---+---+
                                ;   | Te   2Te  |
	
Trasm_BIT

        bsf     txd             ; ON
        movf    TimeHi,W          
        call    WaitxTE
		
        bcf     txd             ; OFF
        movf    TimeLo,W          
        call    WaitxTE

       	decfsz  BitCount,F
        goto    TXNextBit       ; loop on bits

   
; guard time
        movlw   TGUARD
        call    WaitxTE

       org     0x3ff
       retlw    0x80


;----------------------------------------------------------------------
        end                         ; end of program directive
;----------------------------------------------------------------------
 
i tried to post the full code but it doesnot work..it lost everytime i refresh the page

can i have your email..so that i can email you the code?
 
Code:
; guard time
        movlw   TGUARD
        call    WaitxTE

       org     0x3ff
       retlw    0x80

;----------------------------------------------------------------------
        end                         ; end of program directive
;----------------------------------------------------------------------
your code not ending,after call WaitxTE it continue running to the end of memory then repeat from begining, here it found "retlw 0x80" then return to nowhere
solution : use goto
Code:
 ; guard time
       movlw   TGUARD
       call WaitxTE
    goto begin_of_loop

       org     0x3ff
       retlw    0x80
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top