Continue to Site

Welcome to

Welcome to our site! 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 send an (int) on 8-bit port in 8051?qk help pls

Not open for further replies.


Member level 3
Sep 3, 2009
Reaction score
Trophy points
Activity points
how to send data on port0 in c


that is my first microcontroller project so i am a newbie.

i am using 8051 , c language , keil as a compiler

i am now in the designing and programing step , and i want to send data from 8051 to another using port 0 (8-bit) and also to send data to lcd 16x2 ,

the problem that one of the data i want to send is (int)
and as reviewed c books i found that (int) data type takes 16-bit , so i don't know what
will happen if i send an int on port 0, or how the 8051 will act in that case.

for ex

int x;

waiting for ur opinions........

If you use
int x;
P0 = x;

P0 will output only lower 8-bits of x (truncated / typecasted).

to output 16-bits, you have to use one more pin which indicates what you are sending on P0. While sending lower 8-bits, keep this pin low and while sending higher 8-bits keep this pin high. Your code should be something like this

int x;
P1^0 = 0; // Transmitting lower 8-bits
P0 = (unsigned char)x;
P1^0 = 1; // Transmitting upper 8-bits
P0 = (unsigned char)(x >> 8 );

For LCD, convert int to ASCII string (sprintf is the easiest function for this) and send ASCII characters one by one to LCD.

thx CMOS

ur help was very valuable,

but what is the library of the fn (sprintf) ? and i also want to know the whole expression of it .

i have another question but with the wiring this time,

when i connect a pin to ground, do i have to put a resistor and a capacitor in series?
i remember that i heard something like that but i don't remember the details .

This is how you use sprintf.
#include <stdio.h>

void print_integer (int x)
	char strAscii[10]={0};

	sprintf (strAscii,"%d", x);
	puts (strAscii);
	// Print string on LCD
	// lcd_puts(strAscii); // This is your custom LCD function which accepts pointer to char array

You need not use capacitor / resistor when grounding a digital input pin.

Not open for further replies.

Part and Inventory Search

Welcome to