Why does my code display random characters?

Thread Starter

NorwegianFunk

Joined Nov 8, 2009
6
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.

code:


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

BMorse

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

Markd77

Joined Sep 7, 2009
2,806
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:

Thread Starter

NorwegianFunk

Joined Nov 8, 2009
6
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.
 

DumboFixer

Joined Feb 10, 2009
217
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".
 

eng1ne

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

AlexR

Joined Jan 16, 2008
732
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.
 
Top