|
|
View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
using itoa() to show numbers in LCD display |
Posted: Sun Sep 04, 2022 10:46 pm |
|
|
hello,
I have problem to show number in 2x16 standard LCD display...
I'm happy if you can help me to solve this problem ...
why my LCD display show .256789 NOT 1256789 like it should ?
all the text works well
please read this code you'll understand what I mean ...
Code: |
#zero_ram //nollaa kaikki muuttujat alussa
#byte PORTC = 0xF82
#define RS (PIN_E1) //LCD Reg Select, 0 = command, 1 = data
#define LCD_EN (PIN_E2) //LCD enable
void enable_lcd(void);
void lcd_init(void); //sisältää viiveen 10 ms
void show_text(int rivi, int sarake);
void show_numtext(int rivi, int sarake);
void show_merkki(int rivi, int sarake, char merkki);
char merkiksi(int8 number);
void clear_lcd(void); //include delay 10 ms
void init(void);
void init_port_dirs(void);
void display_on(void);
void display_off(void);
void welcome_text(void);
void measure_voltage(void);
void show_voltage(void);
void measure_current(void);
void show_current(void);
void LCD_Send_Command(void); //byte parameter is global G_LCD_command
void LCD_Send_Data(void);
void show_numtext(int rivi, int sarake );
//global identifiers
int8 G_LCD_command; //8 bits lcd command global identifier
int8 G_LCD_data;
int8 text[33]; //row 16 marks + endmark
char G_numtext[33]; //string for itoa
int8 g_tens, g_ones,g_desimal;
int32 G_test_number;
//int16 voltage;
//int16 current;
int16 G_AD_val;
//*********************************************************************************************
void main(){
delay_ms(200);
init_port_dirs();
lcd_init();
welcome_text();
blink:
output_low(pin_A0);
welcome_text();
delay_ms(1000);
clear_lcd();
G_test_number = 1256789; ///this is max. number !
*G_numtext = itoa(G_test_number, 10, G_numtext);
delay_ms(100);
// after that there's .256789 NOT ! 1256789 WHY ??
show_numtext(1,1);
delay_ms(1000);
clear_lcd();
output_high(pin_A0); //test LED
delay_ms(500);
goto blink;
}
//***********************************************************************************************
void show_numtext(int rivi, int sarake ){
unsigned int osoite,address,i;
char merkki;
if(rivi == 0)
osoite = sarake; //address is "osoite" in finnish
if(rivi == 1)
osoite = 0x40 + sarake;
address = 0B10000000 + osoite; //0B10000000 tell it's address
G_LCD_command = address ;
LCD_Send_Command();
for(i= 0; i < strlen(G_numtext); i++){
merkki = G_numtext[i];
G_LCD_data = merkki;
LCD_Send_Data();
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Sep 04, 2022 11:28 pm |
|
|
Delete the part shown in bold below:
Quote: |
*G_numtext = itoa(G_test_number, 10, G_numtext);
|
so it looks like this:
Code: | itoa(G_test_number, 10, G_numtext); |
|
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Sun Sep 04, 2022 11:35 pm |
|
|
PCM programmer wrote: | Delete the part shown in bold below:
Quote: |
*G_numtext = itoa(G_test_number, 10, G_numtext);
|
so it looks like this:
Code: | itoa(G_test_number, 10, G_numtext); |
|
Thank you, now it works fine ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Mon Sep 05, 2022 1:12 am |
|
|
and the bit that makes the return not work, is the *.
G_numtext is a pointer to the string.
*G_numtext, is the contents of the first character of this array.
So you are writing the returned string pointer, into the string itself.
Disaster....
As PCM shows, since the string is actually being written into the array
you are passing, you don't actually have to use the return at all.
Where it can come in useful is to detect the conversion failed. |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Mon Sep 05, 2022 1:21 am |
|
|
Ttelmah wrote: | and the bit that makes the return not work, is the *.
G_numtext is a pointer to the string.
*G_numtext, is the contents of the first character of this array.
So you are writing the returned string pointer, into the string itself.
Disaster....
As PCM shows, since the string is actually being written into the array
you are passing, you don't actually have to use the return at all.
Where it can come in useful is to detect the conversion failed. |
OK, thank you for good explanation ! All the best for you ! |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1347
|
|
Posted: Mon Sep 05, 2022 7:19 pm |
|
|
something else you can do is if you have a function that sends a single byte to display on the LCD, normally called lcd_putc or similar:
Code: |
#define lcd_prntf(format,...) printf(lcd_putc,format,__VA_ARGS__)
|
and the compiler will handle doing the conversions for you without needing extra char arrays and such:
Code: |
lcd_printf("number: %ld",number);
|
lcd_putc() just needs to have a similar declaration to one of these:
Code: |
void lcd_putc(char c);
void lcd_putc(unsigned int8 b);
|
|
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Mon Sep 05, 2022 8:40 pm |
|
|
jeremiah wrote: | something else you can do is if you have a function that sends a single byte to display on the LCD, normally called lcd_putc or similar:
Code: |
#define lcd_prntf(format,...) printf(lcd_putc,format,__VA_ARGS__)
|
and the compiler will handle doing the conversions for you without needing extra char arrays and such:
Code: |
lcd_printf("number: %ld",number);
|
lcd_putc() just needs to have a similar declaration to one of these:
Code: |
void lcd_putc(char c);
void lcd_putc(unsigned int8 b);
|
|
thank you "jeremiah" ... greetings from Finland northern europe ... it's very early in the morning here...
5:35 AM
I really don't know but I think the PIC4525 I'm using don't support LCD glass ...
manual:
<Some families of PICmicro controllers can drive an LCD glass directly, without the need of an LCD
controller. For example, the PIC16C926, PIC16F916 and the PIC18F8490 have an internal LCD
controller>
all the best and thank you very much your good advice
all the best
-arto- |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Tue Sep 06, 2022 1:21 am |
|
|
Doesn't matter.
The point is that CCS supports using standard printf formatting to _any_
output function. It will do it to any LCD, serial, network output, etc. etc..
printf(your_output_function,"Formatstring",what_you_want_to_print);
It'll format 'what_you_want_to_print', using the rules in Formatstring,
and pass the result to your_output_function. The function just needs to
accept standard characters.
Doesn't have to be an internal function, can be anything you have written. |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Tue Sep 06, 2022 2:58 am |
|
|
Ttelmah wrote: | Doesn't matter.
The point is that CCS supports using standard printf formatting to _any_
output function. It will do it to any LCD, serial, network output, etc. etc..
printf(your_output_function,"Formatstring",what_you_want_to_print);
It'll format 'what_you_want_to_print', using the rules in Formatstring,
and pass the result to your_output_function. The function just needs to
accept standard characters.
Doesn't have to be an internal function, can be anything you have written. |
Thank you very much of good advice ...
is there any example how to do it ?
(I have 4 bit interface to 2x16 standard LCD display and I don't understand how to do it with prinf() ...
because 4 bit interface data must send in two parts first MSB 4 bits
and then LSB bits )
all the best
-arto- |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Tue Sep 06, 2022 7:05 am |
|
|
You are making things unnecessarily complex for yourself.
Use PCM_Programmers lcd.c (in the code library). This supports using a
4bit LCD interface (plus two or three control wires), and does all the work
for you, giving a standard 8bit lcd_putc.
This is one of the most fundamental 'do not bother to rewrite what works
well' bits of code used by just about everyone here.
Use this, and you can simply do:
Code: |
lcd_gotoxy(1,1);
printf(lcd_putc,"%5ld",G_test_number);
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|