Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

how to send an (int) on 8-bit port in 8051?qk help pls

Status
Not open for further replies.

evilheart

Member level 3
Joined
Sep 3, 2009
Messages
67
Helped
3
Reputation
8
Reaction score
2
Trophy points
1,288
Location
egypt
Activity points
1,962
how to send data on port0 in c

hi

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;
P0=x;

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

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,683
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.
 

evilheart

Member level 3
Joined
Sep 3, 2009
Messages
67
Helped
3
Reputation
8
Reaction score
2
Trophy points
1,288
Location
egypt
Activity points
1,962
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 .
 

CMOS

Advanced Member level 3
Joined
Jan 6, 2004
Messages
864
Helped
94
Reputation
186
Reaction score
50
Trophy points
1,308
Location
USA
Activity points
5,683
This is how you use sprintf.
Code:
#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.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top