Why does my code display random characters?

Discussion in 'Embedded Systems and Microcontrollers' started by NorwegianFunk, Jan 20, 2010.

  1. NorwegianFunk

    Thread Starter New Member

    Nov 8, 2009
    Hi guys, I am working with a PIC18F micro, interacting with an oled screen. I am trying to develop a comparator that will take a value A and iterate through values B from 0 on up until it matches A. It then should print the values on the screen. But instead of numbers, I am getting random characters (" ^ $(_ " specifically). I have tried declaring A and B as both char and int, and both signed and unsigned. all combinations yield the same result...I am stumped.


    A = 6;
    k = 0;
    while (k < 20) {
    for( B = 0; B < A; B++ ) {
    stringC[k] = B;
    //code to display stringC on oled screen
  2. BMorse

    Senior Member

    Sep 26, 2009
    try converting values to hex and then display them..... or create an ASCII table with the hex equivalents of he ASCII chars to display.....
  3. Markd77

    Senior Member

    Sep 7, 2009
    The ascii characters "0123456789" start at 48 (decimal) so try adding 48 to your values before you add them to the string.

    Oops, this only works for 0-9. Best use a binary to BCD conversion.
    Last edited: Jan 20, 2010
  4. NorwegianFunk

    Thread Starter New Member

    Nov 8, 2009
    well I already have a file that takes care of displaying the correct values on the oled screen. I can print strings and other numbers directly, and it will display them right...but for some reason this for loop is quirky.
  5. DumboFixer

    Active Member

    Feb 10, 2009
    C isn't my strongest language but the way I see it, for the 1st iteration k will be 0 so stringC[0] will be set to 0 in the for loop, then 1 then 2 then and so on to 5. Next iteration of the while loop k will be 1 so stringC[1] will be set to 0,1,2,3,4,5 and so on.

    In the end stringC will contain a sequence of 5 - that is the control character 5, i.e. "ENQ".
  6. eng1ne


    Dec 4, 2009
    What is the stringC[k] function supposed to do? I am not questioning your programming, I have just never come across it before.
  7. AlexR

    Well-Known Member

    Jan 16, 2008
    A bit more information might help (like your full listing). The snippet of code you posted just seems to fill stringC with a repeating pattern of hex 0 to hex 5. We obviously can't tell if this is what you intended to do and since we don't know what you do with the string to display it we can't say where you are going wrong.