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.

What i can better, regarding LCD interface with 8051

Status
Not open for further replies.

kumeemb

Junior Member level 3
Junior Member level 3
Joined
Nov 12, 2006
Messages
26
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,502
Hi,
I need your piece of advice. I am kind of novice to programming C. I am trying to create a Generic LCD_printf function, that can print decimals, string, characters.

The following is the code i am trying

Code:
#include <all necessary h file>

// declaration
void lcd_printf (const char *,...);

//definition
void lcd_printf(const char *identifier, const int idata *datastring)
 {
char id;
while  (*identifier !='\0')
{
 if (*identifier == '%')
        {
                identifier++;
                id = *identifier;
                switch (id)
                        {
                                case 'd' :
                                                dectolcd(datastring); // converts decimal to                                             string
                                                break;

        }               }
 else
        {
        _lcd_data(*identifier); // prints the character in LCD
        }
identifier++;
}
 }

void dectolcd(int *datastring)
  {     char *temp;
        sprintf(temp,"%d",datastring); // converts decimal to string
        lcd_display(temp);   // prints string on the lcd
  }

Void main
{
lcd_printf("Initialising");   // this works fine
lcd_printf("%d",123);
}
Now when I run this code, I am not getting the 123 on the LCD. I know i am messing with the pointers somewhere. Can someone help as what i can do better ??
 

See this:

Generic formats for LCD_Printf function
https://help.bipom.com/index/319513/exa/exa.115.html

Code:
#include <stdio.h>
#include <stdarg.h>
#include <8052.h>
#include <mcs51\bipomlib\types.h>
#include <mcs51\bipomlib\bipomlib.h>
#include <mcs51\bipomlib\lcd.h>

void main()
{
   	// Initialize the serial port 

   	serinit(CBR_19200);
    
	puts( "\rSDCC LCD PRINTF Example" );
	
	// Initialize the LCD 

	LCD_Init();

	// -----------------------------------------

	LCD_Clear();
	LCD_SetTopLine();
	LCD_Write("TEXT FORMAT");
	
	LCD_SetBottomLine();
	LCD_Printf("%s and %s", "PART1", "PART2");

	delay(5000);
	
	// -----------------------------------------

	LCD_Clear();
	LCD_SetTopLine();
	LCD_Write("CHARACTER FORMAT");
	
	LCD_SetBottomLine();
	LCD_Printf("Char1: '%c', Char2: '%c'", 'A', 'Z');

	delay(5000);

	// -----------------------------------------

	LCD_Clear();
	LCD_SetTopLine();
	LCD_Write("INTEGER FORMAT");
	
	LCD_SetBottomLine();
	LCD_Printf("Num1: %d, Num2: %d", 100, -200);

	delay(5000);
	
	// -----------------------------------------

	LCD_Clear();
	LCD_SetTopLine();
	LCD_Write("FLOAT FORMAT #1");
	
	LCD_SetBottomLine();
	LCD_Printf("Number1: %f", 10.5);

	delay(5000);

	// -----------------------------------------

	LCD_Clear();
	LCD_SetTopLine();
	LCD_Write("FLOAT FORMAT #2");
	
	LCD_SetBottomLine();
	LCD_Printf("Number1: %.3f", 1.23456789);

	delay(5000);
	
	puts( "\rDone" );
	while(1);
}



Why to avoid sprintf() ? You can have all formating.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top