C PROGRAMMING BASICS, Passing string as argument

Status
Not open for further replies.

LUNAR

New Member
hello, i am working with pic18f4550 i have interfaced lcd with it.
i am trying to make generic functions to display strings on lcd.
when ever i use this santax i get garbage on lcd.
lcd_display_line((char*)"display this",12, LINE2);
but when i use this
#define bb[]="print this";
lcd_display_line(&bb[0],12,LINE2);
i dont get any error.

please help with the code.
Code:
void lcd_display_line(char* , char  , char  );

void main()
{

lcd_display_line([COLOR="Red"][B](char*)"display this",[/B][/COLOR]12, LINE1);

}

void lcd_display_line(char* line,  char line_length,  char line_number)
{	 
		int  n;
		lcd_command(line_number);
		for (n=0;n<line_length;n++) 
		{
			LCD_senddata(*line);
				line++;
}

void LCD_senddata(unsigned char var)
{
	LCD_DATA = var;      //Function set: 2 Line, 8-bit, 5x7 dots
	LCD_RW   = 0;        //We are writing
	LCD_RS   = 1;        //Selected data register
	LCD_EN   = 1;        //Enable H->L
	delay(180);
	LCD_EN   = 0;
	delay(180);
}
 
Last edited:
i think your not setting the end of line. like "\0" but not sure i never used it like that. Would be nice to try it brb.

EDIT:
doesnt work... i use mines like
Code:
	sprintf(string,"  AtomSoftTech",0);
	lcd_string(string);

String is:;
string[16];
 
Last edited:
This is a problem on pics as the string may be in ram or rom and the called routine doesn't know which. From your code above your LCD_senddata expects a ram pointer and when you put a string as a parameter the compiler knows it is a constant and places it in rom.

You could try doing,
Code:
lcd_display_line(([COLOR="Red"]ram[/COLOR] char*)"display this",12, LINE1);

This is how I write rom strings,
Code:
void main(void){
    ADCON1=0x0f;
    CMCON=7;
    InitLCD();
    InitI2C();    
    PutMessageAt(1,1,(rom char*)"  Hello World");
    PutMessageAt(2,2,(rom char*)" Second Line");
    while(1);
}

//Write a string to the LCD
void PutMessage(rom char *Message){
    while(*Message!=0)
        WriteChar(*Message++);
}

//Set position and write string.
void PutMessageAt(unsigned char X,unsigned char Y,rom char *Message){
    WriteCommand(0x80+(--Y<<6)+--X);
    while(*Message!=0)
        WriteChar(*Message++);
}
You can of course have two functions, one for ram and one for rom. Or copy rom strings to ram prior to printing.

Mike.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…