Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to get rid of these weird charcters?

Status
Not open for further replies.

kgavionics

Full Member level 3
Full Member level 3
Joined
Jun 12, 2012
Messages
167
Helped
7
Reputation
14
Reaction score
11
Trophy points
1,298
Location
Alberta.Canada
Activity points
2,482
Hello
I have this code that sends a string to an LCD and I have a problem (see picture) when the number is less than 3 digits! How can I get rid of the weird characters and display for instance 0 as 000,10 as 010 and so on!
Thank you guys in advance!
C:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <stdlib.h>



int main(void)
{
unsigned char str[3],value=0;
    LCDinit();//init LCD bit, dual line, cursor right
    LCDclr();//clears LCD
LCDcursorOFF();  
itoa(value,str,10);
LCDstring(str,3);
//LCDsendChar(value);

while(1);//loop demos

    return 0;
}
//working on hardware
 

Attachments

  • lcd.jpg
    lcd.jpg
    76.6 KB · Views: 113

You tell us absolutely nothing about your hardware. You tell us next to nothing about your software, other than the fact that you're calling some undefined subroutines.

Impossible to even GUESS what's going on.

Have you looked at the data you're sending to the display? Does it look right?
 

You tell us absolutely nothing about your hardware. You tell us next to nothing about your software, other than the fact that you're calling some undefined subroutines.

Impossible to even GUESS what's going on.

Have you looked at the data you're sending to the display? Does it look right?
You don't need to know my hardware, this is C program and an LCD library which works fine! I'm using an arduino uno if that you want to know!
As I said before, if I send a value of let's say 100, it shows it correctly, it shows weird characters only if the number of digits are less than 3. Something has to do with itoa function, most likely!
 

Your array for char storage must be # char + 1 to hold the string termination
character. Right now its off in lala land when you send the unterminated string
to the LCD.


Regards, Dana.
 
Perhaps if you wrote better code with comments you may achieve better results.
C:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <stdlib.h>

int main(void)
{
unsigned char str[17],value=0;   /* make str large enough to hold the maximum output of itoa() */
    LCDinit();//init LCD bit, dual line, cursor right
    LCDclr();//clears LCD
    LCDcursorOFF(); 
    memset(str,' ',sizeof(str)); /* fill str with blanks */
    itoa(value,str,10);          /* WARNING: unsafe string function, buffer overflow possible */
    str[sizeof(str)-1] = 0;      /* mark end of a maximum size string */
    LCDstring(str,3);
    //LCDsendChar(value);

    while(1);//loop demos

    return 0;
}
//working on hardware
 

When you write a string do you need to indicate the number of chars
you are sending ? Eg. read into a variable the length of sting about to
be sent and use that value in the write string to display function ?

In other words your string array is dimensioned to worst case max + 1,
but when you do write it must be precise in number of chars to take
out of buffer as itoa() will write the buffer with # chars + a string terminator,
NUL character.

Also a write to locate cursor, maybe I am missing that.....

Regards, Dana.
 

Perhaps if you wrote better code with comments you may achieve better results.
C:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <stdlib.h>

int main(void)
{
unsigned char str[17],value=0;   /* make str large enough to hold the maximum output of itoa() */
    LCDinit();//init LCD bit, dual line, cursor right
    LCDclr();//clears LCD
    LCDcursorOFF();
    memset(str,' ',sizeof(str)); /* fill str with blanks */
    itoa(value,str,10);          /* WARNING: unsafe string function, buffer overflow possible */
    str[sizeof(str)-1] = 0;      /* mark end of a maximum size string */
    LCDstring(str,3);
    //LCDsendChar(value);

    while(1);//loop demos

    return 0;
}
//working on hardware
Thank you for the code, it still shows a weird character instead of 2!
 

Attachments

  • lcd2.jpg
    lcd2.jpg
    72.1 KB · Views: 101

Perhaps if you wrote better code with comments you may achieve better results.
C:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <stdlib.h>

int main(void)
{
unsigned char str[17],value=0;   /* make str large enough to hold the maximum output of itoa() */
    LCDinit();//init LCD bit, dual line, cursor right
    LCDclr();//clears LCD
    LCDcursorOFF();
    memset(str,' ',sizeof(str)); /* fill str with blanks */
    itoa(value,str,10);          /* WARNING: unsafe string function, buffer overflow possible */
    str[sizeof(str)-1] = 0;      /* mark end of a maximum size string */
    LCDstring(str,3);
    //LCDsendChar(value);

    while(1);//loop demos

    return 0;
}
//working on hardware

Just curious, should not this -

str[sizeof(str)-1] = 0;

be this -

str[sizeof(str)-1] = '\0';

Regards, Dana.
 

Just curious, should not this -

str[sizeof(str)-1] = 0;

be this -

str[sizeof(str)-1] = '\0';

Regards, Dana.
AFAIK, in C we can use either of the two symbols!
--- Updated ---

I just found the solution!
sprintf function fixed the problem, but it takes about 1kbytes of program memory! which is huge to be honest!

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
unsigned char str[4],value=1; 
    LCDinit();//init LCD bit, dual line, cursor right
    LCDclr();//clears LCD
    LCDcursorOFF();
 
 
    sprintf(str,"%03d",value);
    LCDstring(str,3);
 
 
    while(1);//loop demos
 
    return 0;
}

 
Last edited:

Yes sprintf pulls in a lot of code. There are versions, for some compilers, that
reduce the library impact. I know of one, nano library, used with GNU.

What IDE / compiler are you working with ?

Regards, Dana.
 
I came up with this code which only uses 886 bytes as opposed to 2300 bytes with the sprintf function !

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd_lib.h"
#include <stdlib.h>
 
unsigned char lowb,medb,hib,hexv;   
 
 
int main(void)
{
 
hexv=0x0; // hex value to convert
lowb=(hexv%10)| 0x30;  //low byte binary to decimal conversion, then OR it with 0x30 to obtain the ascii code!
medb=((hexv/10)%10)|0x30; //middle byte binary to decimal conversion, then OR it with 0x30 to obtain the ascii code!
hib=((hexv/10/10)%10) | 0x30; //high  byte  binary to decimal conversion, then OR it with 0x30 to obtain the ascii code!
LCDinit();//init LCD bit, dual line, cursor right
LCDclr();//clears LCD
LCDcursorOFF();   
LCDsendChar(hib);
LCDsendChar(medb);
LCDsendChar(lowb);
while(1);//loop demos
 
    return 0;
}

 
Last edited:

The problem with the original code is you are sending a fixed string length of 3 although the string can have 1 to 3 characters. You can either use strlen(str) in the function call or fill the string with spaces, optionally producing a right aligned string.
 
Hello!
Is it solved now?
For your information, the characters you see are not that weird to me, they are simply
Japanese. The character LCDs usually have means to reconfigure the character set, so I don't think it's a matter of wrong atoi or anything else, it's just a matter of LCD configuration. There must be a register somewhere allowing you to choose plain ascii rom or "katakana" as you show. Is this LCD a new one that you bought for instance at Digikey, or was it used earlier, and potentially configured for Japanese fonts?
 

Hello!
Is it solved now?
For your information, the characters you see are not that weird to me, they are simply
Japanese. The character LCDs usually have means to reconfigure the character set, so I don't think it's a matter of wrong atoi or anything else, it's just a matter of LCD configuration. There must be a register somewhere allowing you to choose plain ascii rom or "katakana" as you show. Is this LCD a new one that you bought for instance at Digikey, or was it used earlier, and potentially configured for Japanese fonts?
Everything works fine with the last code I wrote!
Thank you!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top