Garbage out from RS232 on PIC

Discussion in 'Embedded Systems and Microcontrollers' started by sbf2009, Aug 17, 2010.

  1. sbf2009

    Thread Starter New Member

    Jul 21, 2010
    8
    0
    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)
     
  2. sbf2009

    Thread Starter New Member

    Jul 21, 2010
    8
    0
    Also, when I power off and restart the PIC, the result changes a little.
     
  3. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    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)...
     
  4. eblc1388

    Senior Member

    Nov 28, 2008
    1,542
    102
    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: Aug 18, 2010
  5. sbf2009

    Thread Starter New Member

    Jul 21, 2010
    8
    0
    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.
     
  6. tom66

    Senior Member

    May 9, 2009
    2,613
    213
    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.
     
  7. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    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)
     
  8. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.
     
  9. sbf2009

    Thread Starter New Member

    Jul 21, 2010
    8
    0
    @ 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.
     
  10. sbf2009

    Thread Starter New Member

    Jul 21, 2010
    8
    0
    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. ; _ ;
     
  11. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    Could it be the way the data was being initialised ?

    Try
    Code ( (Unknown Language)):
    1. const short A[8][8] =
    2.  {
    3.   0,0,0,1,1,0,0,0,
    4.   0,0,1,0,0,1,0,0,
    5.   0,0,1,0,0,1,0,0,
    6.   0,1,0,0,0,0,1,0,
    7.   0,1,0,0,0,0,1,0,
    8.   0,1,1,1,1,1,1,0,
    9.   0,1,0,0,0,0,1,0,
    10.   0,1,0,0,0,0,1,0
    11.  };
     
  12. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    just to chuck in another one, i'd intialise a multidimensional array with more commas i think, like
    Code ( (Unknown Language)):
    1. const short A[8][8] =
    2.  {
    3.   {0,0,0,1,1,0,0,0},
    4.   {0,0,1,0,0,1,0,0},
    5.   {0,0,1,0,0,1,0,0},
    6.   {0,1,0,0,0,0,1,0},
    7.   {0,1,0,0,0,0,1,0},
    8.   {0,1,1,1,1,1,1,0},
    9.   {0,1,0,0,0,0,1,0},
    10.   {0,1,0,0,0,0,1,0}
    11.  };
     
Loading...