Memory Problem in PIC18F46K22

ErnieM

Joined Apr 24, 2011
8,415
Rich (BB code):
SHOWDispWhat = (SHOWDispWhat+1)%13;
This will count from 0 to 12 (instead of 1 to 13).
Of course, after you post some pure genius some twit will come by and show you an even better way which is really your own way:

Rich (BB code):
SHOWDispWhat = 1 + (SHOWDispWhat + 1)%13;
This will count from 1 to 13 (instead of 0 to 12):
 

joeyd999

Joined Jun 6, 2011
6,279
Not really. %13 is a very slow operation on this PIC. So, Joeyd's first take was better.
Not that I would know. Helping someone to write C code for a PIC makes me feel dirty.

For Ernie's sake, I just did this (on my linux box):

Rich (BB code):
#include <stdio.h>

void main(void)
{
	int SHOWDispWhat,i;
	
	SHOWDispWhat = 0;

	for (i=1; i < 100; i++)
	{
		SHOWDispWhat = 1 + (SHOWDispWhat + 1)%13;
		printf("Iteration %d, SHOWDispWhat = %d\n",i,SHOWDispWhat);
	}
}
and I got this, as expected:

Rich (BB code):
Iteration 1, SHOWDispWhat = 2
Iteration 2, SHOWDispWhat = 4
Iteration 3, SHOWDispWhat = 6
Iteration 4, SHOWDispWhat = 8
Iteration 5, SHOWDispWhat = 10
Iteration 6, SHOWDispWhat = 12
Iteration 7, SHOWDispWhat = 1
Iteration 8, SHOWDispWhat = 3
Iteration 9, SHOWDispWhat = 5
Iteration 10, SHOWDispWhat = 7
Iteration 11, SHOWDispWhat = 9
Iteration 12, SHOWDispWhat = 11
Iteration 13, SHOWDispWhat = 13
Iteration 14, SHOWDispWhat = 2
Iteration 15, SHOWDispWhat = 4
Iteration 16, SHOWDispWhat = 6
Iteration 17, SHOWDispWhat = 8
Iteration 18, SHOWDispWhat = 10
Iteration 19, SHOWDispWhat = 12
Iteration 20, SHOWDispWhat = 1

...
 

THE_RB

Joined Feb 11, 2008
5,438
Not really. %13 is a very slow operation on this PIC. So, Joeyd's first take was better.
Agreed that %13 is nasty.

This code is very fast and compiles down to maybe 5 asm instructions;
Rich (BB code):
  SHOWDispWhat++;
  if(SHOWDispWhat > 13) SHOWDispWhat = 1;
Probably the best implementation and is also very easy to convert to ASM or any other language.
 

BobTPH

Joined Jun 5, 2013
11,515
For your info, C is a *high* level language.
My comment was meant to be tongue in cheek.

But, high-level is relative.

A language like Java, for example would never have memory corruption problems. That makes it a higher-level language than C.

Bob
 

takao21203

Joined Apr 28, 2012
3,702
Agreed that %13 is nasty.

This code is very fast and compiles down to maybe 5 asm instructions;
Rich (BB code):
  SHOWDispWhat++;
  if(SHOWDispWhat > 13) SHOWDispWhat = 1;
Probably the best implementation and is also very easy to convert to ASM or any other language.
while((SHOWDispWhat++)<13){};SHOWDispWhat=1;
 

joeyd999

Joined Jun 6, 2011
6,279
Rich (BB code):
#include <stdio.h>

void main(void)
{
	int SHOWDispWhat,i;
	
	SHOWDispWhat = 0;

	for (i=1; i < 100; i++)
	{
		SHOWDispWhat = SHOWDispWhat % 13 + 1;
		printf("Iteration %d, SHOWDispWhat = %d\n",i,SHOWDispWhat);
	}
}
Rich (BB code):
Iteration 1, SHOWDispWhat = 1
Iteration 2, SHOWDispWhat = 2
Iteration 3, SHOWDispWhat = 3
Iteration 4, SHOWDispWhat = 4
Iteration 5, SHOWDispWhat = 5
Iteration 6, SHOWDispWhat = 6
Iteration 7, SHOWDispWhat = 7
Iteration 8, SHOWDispWhat = 8
Iteration 9, SHOWDispWhat = 9
Iteration 10, SHOWDispWhat = 10
Iteration 11, SHOWDispWhat = 11
Iteration 12, SHOWDispWhat = 12
Iteration 13, SHOWDispWhat = 13
Iteration 14, SHOWDispWhat = 1
Iteration 15, SHOWDispWhat = 2
Iteration 16, SHOWDispWhat = 3
Iteration 17, SHOWDispWhat = 4
Iteration 18, SHOWDispWhat = 5
Iteration 19, SHOWDispWhat = 6
Iteration 20, SHOWDispWhat = 7
Iteration 21, SHOWDispWhat = 8
Iteration 22, SHOWDispWhat = 9
Iteration 23, SHOWDispWhat = 10
Iteration 24, SHOWDispWhat = 11
Iteration 25, SHOWDispWhat = 12
Iteration 26, SHOWDispWhat = 13
Iteration 27, SHOWDispWhat = 1

...
 

joeyd999

Joined Jun 6, 2011
6,279
while((SHOWDispWhat++)<13){};SHOWDispWhat=1;
Ummmmm....

Rich (BB code):
Iteration 1, SHOWDispWhat = 1
Iteration 2, SHOWDispWhat = 1
Iteration 3, SHOWDispWhat = 1
Iteration 4, SHOWDispWhat = 1
Iteration 5, SHOWDispWhat = 1
Iteration 6, SHOWDispWhat = 1
Iteration 7, SHOWDispWhat = 1
Iteration 8, SHOWDispWhat = 1
Iteration 9, SHOWDispWhat = 1
Iteration 10, SHOWDispWhat = 1
Iteration 11, SHOWDispWhat = 1
Iteration 12, SHOWDispWhat = 1
Iteration 13, SHOWDispWhat = 1
Iteration 14, SHOWDispWhat = 1

...
 

joeyd999

Joined Jun 6, 2011
6,279
A language like Java, for example would never have memory corruption problems...
Bad code can be written in any language, regardless of the overhead available to prevent it.

Never underestimate the ingenuity of a fool. And most of the programmers I've met -- and worked with -- were ingenious fools.
 

takao21203

Joined Apr 28, 2012
3,702
Ummmmm....

Rich (BB code):
Iteration 1, SHOWDispWhat = 1
Iteration 2, SHOWDispWhat = 1
Iteration 3, SHOWDispWhat = 1
Iteration 4, SHOWDispWhat = 1
Iteration 5, SHOWDispWhat = 1
Iteration 6, SHOWDispWhat = 1
Iteration 7, SHOWDispWhat = 1
Iteration 8, SHOWDispWhat = 1
Iteration 9, SHOWDispWhat = 1
Iteration 10, SHOWDispWhat = 1
Iteration 11, SHOWDispWhat = 1
Iteration 12, SHOWDispWhat = 1
Iteration 13, SHOWDispWhat = 1
Iteration 14, SHOWDispWhat = 1

...
That's a pretty weird reply, since you dont show the modified source code.
How long do you use C language?

Actually, I tested with the MPLABX simulator right now, and each time, it is increasing correctly.

Disassembly (I use a baseline device):

Rich (BB code):
!    while(1)
0x1FE: GOTO 0x1F4
!    {
!	while((SHOWDispWhat++)<13){};SHOWDispWhat=1;
0x1F4: MOVLW 0x1
0x1F5: MOVWF 0x1F
0x1F6: MOVF 0x1F, W
0x1F7: ADDWF __pcstackBANK0, F
0x1F8: MOVLW 0xE
0x1F9: SUBWF __pcstackBANK0, W
0x1FA: BTFSS STATUS, 0x0
0x1FB: GOTO 0x1F4
0x1FC: CLRF __pcstackBANK0
0x1FD: INCF __pcstackBANK0, F
Of course you'd have to insert some code in the brackets.

I'd suggest to test your code in the simulator, before assuming memory corruption. That would be weird happening just on random memory addresses, if at all, it would happen on some memory address ranges.

Maybe you configured wrongly, some things like WDT or brownout are on. Interrupts are normally off. Also you dont need to clear memory yourself, there is an option for it.

If you are unfamiliar with C you can try in the simulator.
I know the variable evalution does not work on the expression, but you can hover over the definition.

After each press of F5, it increases by one. And you have to set a breakpoint on the line.

so what?

You'd have to post your complete source, including all lines, all .c and .h files, and the configuration bits especially, or not much can be said at all about the problem.

If it is really a hardware memory corruption, it would be of importance to see the ranges where it occurs.
 
I have written code for about 40 years.
In that time I have blamed assemblers and compilers for lots of problems I have had.
In 99% of cases in the end it was down to an error in my coding.

In the 1980's I came across some rubbish assemblers and compilers but nowadays things are much better.
 

joeyd999

Joined Jun 6, 2011
6,279
That's a pretty weird reply, since you dont show the modified source code.
How long do you use C language?
I've been programming C at an expert level for at least the last 3 days. After learning it last week, I was surprised at how similar it is to BASIC. The only difference seems to be that in BASIC you type "print" and in C you type "printf". Everything else seems the same, though, so it was pretty easy to quickly get to a 7334 level.

I'd suggest to test your code in the simulator, before assuming memory corruption. That would be weird happening just on random memory addresses, if at all, it would happen on some memory address ranges.

Maybe you configured wrongly, some things like WDT or brownout are on. Interrupts are normally off. Also you dont need to clear memory yourself, there is an option for it.

If you are unfamiliar with C you can try in the simulator.
I know the variable evalution does not work on the expression, but you can hover over the definition.

After each press of F5, it increases by one. And you have to set a breakpoint on the line.

so what?

You'd have to post your complete source, including all lines, all .c and .h files, and the configuration bits especially, or not much can be said at all about the problem.

If it is really a hardware memory corruption, it would be of importance to see the ranges where it occurs.
That took a lot of typing. Thank you. But, regardless of your objections to the contrary, I am sure the memory leak in my project is due to the bugs in your code.
 

takao21203

Joined Apr 28, 2012
3,702
I've been programming C at an expert level for at least the last 3 days. After learning it last week, I was surprised at how similar it is to BASIC. The only difference seems to be that in BASIC you type "print" and in C you type "printf". Everything else seems the same, though, so it was pretty easy to quickly get to a 7334 level.



That took a lot of typing. Thank you. But, regardless of your objections to the contrary, I am sure the memory leak in my project is due to the bugs in your code.
Awesome. My typing work gets acclaimed. I'm a programmer, at least I did go to college for it. So I can type very fast, the chicklet keyboard on the MSI netbook I use is the best keyboard I ever had.

The last 3 days! Now you can tell the old assembler whizards the difference between a software and a hardware stack- only to get the reply there is no need for any stack it's just an arrangement of GOTOs and you could call it whatever you wanted to.

I found the simulator very useful for learning both assembler and C, as for C, to become familiar with the spelling, and verify effects of more obscure type casting and brackets at the right (or wrong) place.

Yes the memory corruption was caused by the line I entered some days ago.
 

joeyd999

Joined Jun 6, 2011
6,279
...it's just an arrangement of GOTOs...
It's funny that you mention that!

One of the things I learned last week is that unlike BASIC, C, for some strange reason, doesn't use line numbers. I suppose this so that you can write more concise code (and probably save the memory that would normally store the line numbers, I guess). But, without line numbers, I can't figure out where the GOTOs go to.

Maybe the memory is leaking from the space where the line numbers are missing?

I'm going to put line numbers back in my C code to see if that fixes the problem. I'll let you know.
 

takao21203

Joined Apr 28, 2012
3,702
That's what typecasting is - the brackets at the wrong (or left) place.

But tell me:

Rich (BB code):
x = (y*(y))+1;
Is this typecasting or what?
Yes it is a conversion of y (the value it contains) into a pointer to y (the address the pointer points to will be the value of y, and the displacement will be multiplied by the size of the type of y).

similar to
Rich (BB code):
(unsigned char*(y))
.
The compiler wont neccessarily add 1 literally.

You'll get a compiler warning too.
 
Top