Garbage out from RS232 on PIC

Thread Starter

sbf2009

Joined Jul 21, 2010
8
I am working on a display project and am trying to test out displaying characters using an 8X8 array. When I put the this test program in, I get the following result, which is totally wrong as is aprent in the definition of the array. I am using THe latest version of MPLAB, the latest version of CCS's PIC MCU C compiler, and a PIC16F877A from CCS.

Code:
#include <16F877A.h>
#include <stdlib.h>
#include <stdint.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define button PIN_A4
short A[8][8] =
{
{0,0,0,1,1,0,0,0}
{0,0,1,0,0,1,0,0}
{0,0,1,0,0,1,0,0}
{0,1,0,0,0,0,1,0}
{0,1,0,0,0,0,1,0}
{0,1,1,1,1,1,1,0}
{0,1,0,0,0,0,1,0}
{0,1,0,0,0,0,1,0}
};
int i,j;
void waitpress()
{
while(input(button));
while(!input(button));
}
void main()
{
while(TRUE)
{
i=0;
j=0;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%d (%d,%d) ", A[j], i, j);
}
printf("\n");
}
waitpress();
}
}



Result:

0 (0,0) 0 (0,1) 0 (0,2) 1 (0,3) 1 (0,4) 0 (0,5) 0 (0,6) 0 (0,7)
1 (1,0)
0 (1,1) 0 (1,2) 0 (1,3) 0 (1,4) 0 (1,5) 0 (1,6) 0 (1,7)
1 (2,0) 0 (2,1)
0 (2,2) 0 (2,3) 1 (2,4) 1 (2,5) 0 (2,6) 0 (2,7)
0 (3,0) 0 (3,1) 0 (3,2
) 0 (3,3) 0 (3,4) 0 (3,5) 1 (3,6) 1 (3,7)
0 (4,0) 0 (4,1) 0 (4,2) 1 (4,
3) 1 (4,4) 0 (4,5) 0 (4,6) 0 (4,7)
1 (5,0) 0 (5,1) 1 (5,2) 0 (5,3) 0 (5
,4) 0 (5,5) 0 (5,6) 0 (5,7)
1 (6,0) 0 (6,1) 0 (6,2) 0 (6,3) 1 (6,4) 1 (
6,5) 0 (6,6) 0 (6,7)
0 (7,0) 0 (7,1) 0 (7,2) 0 (7,3) 0 (7,4) 0 (7,5) 1
(7,6) 1 (7,7)
 

retched

Joined Dec 5, 2009
5,208
Just at first glance;

I see you have i and j values assigned INSIDE of the while loop.

I would think that every loop would RESET i and j to 0(zero) at the loop then again in the FOR

I may have had too much Karmel Sutra Ice Cream (Ben & Jerry's)...
 

eblc1388

Joined Nov 28, 2008
1,542
The output result is correct for the initial row where i=0. It starts to differ when the array index i=1.

You need to find out if the correct value has been actually placed into the array in the first place.

I would suggest you temporarily initialize the array element with values like [0,1,2,3,4,5,6,7, 10,11..,17,20,..70,77] and repeat the debugging to see where the problem lies. The output will enable you to see what is wrong.
 
Last edited:

Thread Starter

sbf2009

Joined Jul 21, 2010
8
I'll try that out, but I don't think there's anything wrong with the loops, this kind of code structure has worked for a bunch of other programmers on the internet. Also, it doesn't explain why, when I power down the chip and then power up again, it gives me a different result.
 

tom66

Joined May 9, 2009
2,595
On some of the lower cost PICs, the memory after power on reset is undefined; it can take any state, but it usually depends on the prior state of the memory, plus a few bits flipped depending on how long it has been out of power for. This even happens with the more expensive dsPICs if the power interruption is less than about 5 seconds (after which it usually fades to zeroes), so remember to initialize all your variables to zero or whatever value. In your case can you be certain that the array you have designed is being initialized properly? Some compilers won't automatically initialize data structures; try turning it into a const and see what happens.
 

retched

Joined Dec 5, 2009
5,208
After looking over it again, it may have been the ice cream. I apologize.

I agree with tom66 in stating that you should initialize the values of the variables at the start of the code just in case the PIC doesn't reset them to 0(zero) on reset. (Its good practice for the future)
 

John P

Joined Oct 14, 2008
2,025
The array is being initialized outside the main() section, and i and j are both defined as part of for{} loops. I don't think anything like that is the problem.

My question is whether short integers (single bits) can be used in an array as you've done, and printed out as if they were regular integers. I'm also wondering why the output has carriage returns spaced so randomly. It seems clear enough that there should be 8 outputs from the array per printed line, and that only happens once. A clue?

I'd say check the LST file and see if anything looks wrong.
 

Thread Starter

sbf2009

Joined Jul 21, 2010
8
@ John P, Acutally, that was a failrue on my part for not putting /r in the print line when talking to the RS232.
@ Tom, just tried that, too.

Here is my new code:

#include <16F877A.h>
#include <stdlib.h>
#include <stdint.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define button PIN_A4
const short A[8][8] =
{
{0,0,0,1,1,0,0,0}
{0,0,1,0,0,1,0,0}
{0,0,1,0,0,1,0,0}
{0,1,0,0,0,0,1,0}
{0,1,0,0,0,0,1,0}
{0,1,1,1,1,1,1,0}
{0,1,0,0,0,0,1,0}
{0,1,0,0,0,0,1,0}
};
int i,j;
void waitpress()
{
while(input(button));
while(!input(button));
}
void main()
{
while(TRUE)
{
i=0;
for(i=0;i<8;i++)
{
j=0;
for(j=0;j<8;j++)
{
printf("%d", A[j]);
}
printf("\r\n");
}
printf("\r\n\r\n");
waitpress();
}
}


And my new result:

00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000



I think making it a const made it worse, I'm not sure if what John said about short's in arrays is true, but if so, it's gunna suck because I don't have enough room on the chip for an entire alphabet's worth of 8-bit integer arrays.
 

Thread Starter

sbf2009

Joined Jul 21, 2010
8
New try, this time with a less intuitive approach.

#include <16F877A.h>
#include <stdlib.h>
#include <stdint.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define button PIN_A4
int i,j;
void waitpress()
{
while(input(button));
while(!input(button));
}
void main()
{
short A[64]= {0,0,0,1,1,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0};
while(TRUE)
{
i=0;
for(i=0;i<8;i++)
{
j=0;
for(j=0;j<8;j++)
{
printf("%d", A[8*i+j]);
}
printf("\r\n");
}
printf("\r\n\r\n");
waitpress();
}
}


Give this:

00011000
00100100
00100100
01000010
01000010
01111110
01000010
01000010



Apparently my compiler does not like multidimensional arrays. ; _ ;
 

DumboFixer

Joined Feb 10, 2009
217
Could it be the way the data was being initialised ?

Try
Rich (BB code):
const short A[8][8] =
 {
  0,0,0,1,1,0,0,0,
  0,0,1,0,0,1,0,0,
  0,0,1,0,0,1,0,0,
  0,1,0,0,0,0,1,0,
  0,1,0,0,0,0,1,0,
  0,1,1,1,1,1,1,0,
  0,1,0,0,0,0,1,0,
  0,1,0,0,0,0,1,0
 };
 

cheezewizz

Joined Apr 16, 2009
82
just to chuck in another one, i'd intialise a multidimensional array with more commas i think, like
Rich (BB code):
const short A[8][8] =
 {
  {0,0,0,1,1,0,0,0},
  {0,0,1,0,0,1,0,0},
  {0,0,1,0,0,1,0,0},
  {0,1,0,0,0,0,1,0},
  {0,1,0,0,0,0,1,0},
  {0,1,1,1,1,1,1,0},
  {0,1,0,0,0,0,1,0},
  {0,1,0,0,0,0,1,0}
 };
 
Top