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

Need Help!

Status
Not open for further replies.

Viann

New Member
I've been cracking my head for hours, and I couldn't come up with a solution.

PIC I am using is PIC16F877A

Alright, I'm supposed to receive a value from the ADC and convert it to its appropriate value and output it to an LCD.

Attached is the code I did... but the problem is the values that come out are garbage and after that, I am getting "Hardware Stack Overflow". I tested it using PIC Simulator IDE.

Could someone help me debug the code?


Based on the Source Code, this is how I output the value:
  • Add whatever value in the memory labelled First with b'00110000' in order to get the appropriate value.
  • After that, I send out a '.', a dot signifying the next two values being decimal places.
  • Then, I add whatever value in the memory labelled Second with b'00110000' in order to get the appropriate value.
  • After that, I add whatever value in the memory labelled Third with b'00110000' in order to get the appropriate value.

And then the code goes back to the beginning to check for the ADC value again, and outputs a newer value when changed.

I know the code is long, but I sincerely hope you guys can help me out.
 

Attachments

Last edited:

Viann

New Member
I suggest you look at ny tutorials, which do what you're looking for.
Nigel, I have, your site has been very helpful.
Unfortunately I don't know what is wrong with my coding.

The problem starts here: OUTPUT CURRENT READING

I sent the output well, but I still can't see why garbage comes out.
And on top of that, the Hardware Stack Overflow. I managed my calls quite well.

EDIT:
Also, what does "Using default destination of 1" mean?

EDIT2:
Ah I see, I was missing the location. That was very lame. (i.e. movf First, W - I was missing the W)
Since it's solved, is it possible for me to know why the Hardware Stack Overflow happened due to that reasoning?
 
Last edited:

Nigel Goodwin

Super Moderator
Most Helpful Member
The most likely reason would be too many calls without returns, or a missing return somewhere.

Using a call to change banks is a total waste for no reason, either just enter the line itself, or use a macro to do it, don't waste valuable stack space for nothing.
 

ericgibbs

Well-Known Member
Most Helpful Member
The most likely reason would be too many calls without returns, or a missing return somewhere.

Using a call to change banks is a total waste for no reason, either just enter the line itself, or use a macro to do it, don't waste valuable stack space for nothing.
hi Nigel,
As you say far too many nested CALL's.


Viann:
Oshonsoft shows the program running often at 7, 8 levels, then tries for a 9!!.

Default destination means you have not specified in prog W or F.
You have 3 instances of that default.
Code:
I have added the W
        movf First,W
	addlw b'00110000'
	call sendData

	movlw b'00101110'
	call sendData

	movf Second,W
	addlw b'00110000'
	call sendData

	movf Third,W
	addlw b'00110000'
	call sendData
 
Last edited:

Viann

New Member
Using a call to change banks is a total waste for no reason, either just enter the line itself, or use a macro to do it, don't waste valuable stack space for nothing.
Enter the line itself... would that mean using banksel?

hi Nigel,
As you say far too many nested CALL's.
I'm a bit confused.. when you say nested call, does it mean I performed too many calls without return, or just plainly too many calls?

If it is the latter... why would too many calls not be good?
From what I've read, calls charge two extra microseconds.


Viann:
Oshonsoft shows the program running often at 7, 8 levels, then tries for a 9!!.
7 or 8 levels, meaning?

So sorry for the confusion, I'm fairly new to programming the PIC.
 
Last edited:

ericgibbs

Well-Known Member
Most Helpful Member
Enter the line itself... would that mean using banksel?

I'm a bit confused.. when you say nested call, does it mean I performed too many calls without return, or just plainly too many calls?

If it is the latter... why would too many calls not be good?
From what I've read, calls charge two extra microseconds.
7 or 8 levels, meaning?
.
hi Viann,
The Stack in that PIC is only 8 levels deep, if you exceed 8 levels the program crashes!.

The Stack is where a RETURN address is saved when you make a CALL.

Make too many CALLs and the Stack overflows.
At the end of a subroutine CALL the RETURN pops the saved return address.

As a quick test I converted the BANK CALLs to MACRO, it still crashes at this section.
Code:
Vread
	call VoltageReadingTop;**************
	goto SwitchCheck1
	goto Vread
Code:
	BANK0 MACRO 
	bcf STATUS,RP0
	bcf STATUS,RP1
	endm

	BANK1 MACRO 
	bsf STATUS,RP0
	bcf STATUS,RP1
	endm

	BANK2 MACRO	
    	bcf STATUS, RP0
	bsf STATUS, RP1
	endm

	BANK3 MACRO
    	bsf STATUS, RP0
    	bsf STATUS, RP1
    	endm

	org 0x0000
 
Last edited:

Viann

New Member
hi Viann,
The Stack in that PIC is only 8 levels deep, if you exceed 8 levels the program crashes!.

The Stack is where a RETURN address is saved when you make a CALL.

Make too many CALLs and the Stack overflows.
At the end of a subroutine CALL the RETURN pops the saved return address.
Ah I see.
So basically, inside a call function, it's not right to have more than 7 calls, or else overflow occurs?


ericgibbs;767081As a quick test I converted the BANK CALLs to MACRO said:
Vread
call VoltageReadingTop;**************
goto SwitchCheck1
goto Vread
[/CODE]

Code:
	BANK0 MACRO 
	bcf STATUS,RP0
	bcf STATUS,RP1
	endm

	BANK1 MACRO 
	bcf STATUS,RP0
	bsf STATUS,RP1
	endm

	BANK2 MACRO	
    	bcf STATUS, RP0
	bsf STATUS, RP1
	endm

	BANK3 MACRO
    	bsf STATUS, RP0
    	bsf STATUS, RP1
    	endm

	org 0x0000
I just checked through my code, inside that call function there was a few call-return function that, I suppose, increases the stack count by 1, and then decreases it back because of the return function.

I guess I'm still confused. Why would it crash there?
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Enter the line itself... would that mean using banksel?
Banksel is a ready written Macro, so that's one solution - or just enter a single line setting the bit you need to change (check my tutorials for examples).

I'm a bit confused.. when you say nested call, does it mean I performed too many calls without return, or just plainly too many calls?

If it is the latter... why would too many calls not be good?
From what I've read, calls charge two extra microseconds.


7 or 8 levels, meaning?

So sorry for the confusion, I'm fairly new to programming the PIC.
Like Eric has said, the PIC only have a very small stack size (8 calls only), if you do too many nested calls you run out of stack space and it crashes.

You need to drop your number of nested calls a LOT - because once you start using interrupts, you're going to hit much bigger problems.

For a simple to understand 'example', place eight coins on the table in front of you.

First pick a coin up, that's a 'call', now put it back, that's a 'return' - now pick two coins up (two nested calls), now pick six more up (six more nested calls) - now try and do another 'call' - no coins to pickup, and your fingers are bleeding on the table.

That's what crashes the PIC, it's little silicon fingers are bleeding on the table.
 

ericgibbs

Well-Known Member
Most Helpful Member
hi viann,

I guess you wrote most of that program before you actually tried it.?

I would suggest you consider a modular approach to programming.

Write short, freestanding sub/routines, debug and prove them out before adding more code.

An example would be to debug the LCD, Switch Reading and Maths routines first.

As a personal quirk, I dont place 'data strings' inside the program code I prefer to place them at the end of the program and use labels.

Have you managed to get the LCD to display any data.?
 
Last edited:

ericgibbs

Well-Known Member
Most Helpful Member
hi
If you do use Macro's for Bank selection, note there is an error in my post due to copy and pasting.:rolleyes:

Code:
I have this:
BANK1 MACRO 
	bcf STATUS,RP0
	bsf STATUS,RP1
	endm

;it should be this:

BANK1 MACRO 
	b[COLOR="Red"]s[/COLOR]f STATUS,RP0
	b[COLOR="Red"]c[/COLOR]f STATUS,RP1
	endm
 

Viann

New Member
Banksel is a ready written Macro, so that's one solution - or just enter a single line setting the bit you need to change (check my tutorials for examples).



Like Eric has said, the PIC only have a very small stack size (8 calls only), if you do too many nested calls you run out of stack space and it crashes.

You need to drop your number of nested calls a LOT - because once you start using interrupts, you're going to hit much bigger problems.

For a simple to understand 'example', place eight coins on the table in front of you.

First pick a coin up, that's a 'call', now put it back, that's a 'return' - now pick two coins up (two nested calls), now pick six more up (six more nested calls) - now try and do another 'call' - no coins to pickup, and your fingers are bleeding on the table.

That's what crashes the PIC, it's little silicon fingers are bleeding on the table.
I see, that clears things up quite a bit.

hi viann,

I guess you wrote most of that program before you actually tried it.?

I would suggest you consider a modular approach to programming.

Write short, freestanding sub/routines, debug and prove them out before adding more code.

An example would be to debug the LCD, Switch Reading and Maths routines first.

As a personal quirk, I dont place 'data strings' inside the program code I prefer to place them at the end of the program and use labels.

Have you managed to get the LCD to display any data.?
Eric, yes I did write most of the program, save the initialization part.
And the LCD actually displayed whatever it needed to, plus I could change between readings (i.e. the VoltageReading part you said it would crash at).

Which comes to my next question, why would it crash there Eric?
Within the loop, where it includes the call function, it does not contain any nested loop that will cause an overflow.
Or am I missing something once again? :(
 

ericgibbs

Well-Known Member
Most Helpful Member
hi,
Are you using the MPLAB IDE simulator to test your program.??


EDIT: please post your latest version.

Note the 'Manage upload' will accept files with asm extension.
 
Last edited:

ericgibbs

Well-Known Member
Most Helpful Member
hi,
The problem seems to be within the 'multiplication' routines.:)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top