little trick in the 8051

Thread Starter

FBorges22

Joined Sep 11, 2008
109
Greetings,

I am looking for way to read the P1 Port in the 8051 and send to the serial port when the uC receives the "a" character. However I need to send a decimal string that corresponds to the state of the P1 Port. What is the correct way to do that?

I tried those methods below however I always receive a ASCII character istead of a decimal string.

Rich (BB code):
void send_serial()
{
    SBUF=P1;
    while(!TI);
    TI=0;
    SBUF=0x0A; // sends the "\n" ascii character
    while(!TI);
    TI=0;
}

void receive_serial() interrupt 4 using 0
{
    while(!RI);
    RI=0;
    sbuffer=SBUF;
    if (sbuffer == 0x61)
    {
        send_serial();
    }
}
To clarify even more... For example, consider that P1 register is at 7D and when I press the "a" character in the hyperterminal I need to see the number 125 on the screen and not the "}" symbol.

Thanks,
FBorges22
 

RiJoRI

Joined Aug 15, 2007
536
I bet that the "ASCII character" you receive translates to the data on P1. You'll need to use a function that changes the hex value to a decimal one. There may be something in your C compiler's library. Basically, you want the effect of printf("%d\n",P1);" without the overhead of the printf() function.

--Rich
 

Thread Starter

FBorges22

Joined Sep 11, 2008
109
I tried to import the stdio.h library to use the printf function... However I am experiencing a compiling error in my program. Note I am using the SDCC compiler.

Here is a sample of my code:

Rich (BB code):
#include <at89s8252.h>
#include <stdio.h>

#define s115200 65530 // Baudrate Set

char sbuffer; // Received serial data

void init_serial(void) // Baud-rate: 115200 - crystal 22.1184MHz
{
    TCON=0x00;
    PCON=0x00;
    SCON=0x50;
    TCLK=1;
    RCLK=1;
    C_T2=0;
    TR2=0;
    RCAP2H=(s115200/256);
    RCAP2L=(s115200%256);
    IE=0x90;
    IP=0x01;
    TR2=1;
}

void send_serial()
{
    SBUF=printf("%d\n",P1);
    while(!TI);
    TI=0;
}

void receive_serial() interrupt 4 using 0
{
    while(!RI);
    RI=0;
    sbuffer=SBUF;
    if (sbuffer == 0x61)
    {
        send_serial(); // send the P1 port status
    }
}

void main()
{
    init_serial(); // Initialize the serial port
    while(1);
}
The output error is:

?ASlink-Warning-Undefined Global '_putchar' referenced by module 'vprintf'
 

Thread Starter

FBorges22

Joined Sep 11, 2008
109
Is there any olther way to do this conversion istead of printf function? Because the SDCC is not recognizing the printf command even with stdio.h library...
 

RiJoRI

Joined Aug 15, 2007
536
"The effect" means you want what it does, without actually using it. Here is one way:
Rich (BB code):
char Hundreds_Cnt;
char Tens_Cnt;
char Ones_Cnt;

void xtod(char Hex_Num)
{
	/* Clear holders */
	Hundreds_Cnt = 0;
	Tens_Cnt = 0;
	Ones_Cnt = 0;

	/* Count the hundreds we have */
	while(Hex_Num > 99) /* same as >= 100 */
	{
		Hundreds_Cnt++;
		Hex_Num -= 100;
	}

	/* Count the tens we have */
	while(Hex_Num > 9)
	{
		Tens_Cnt++;
		Hex_Num -= 10;
	}

	/* Counting the ones would be silly */
	Ones_Cnt = Hex_Num;
}
--Rich
 

Thread Starter

FBorges22

Joined Sep 11, 2008
109
Let me check if I understand correctly...

I need to call the xtod function with P1 value with this?

Rich (BB code):
xtod(P1)
And after that I need to send all the three parameters to the serial port using this function:

Rich (BB code):
void send_serial()
{
    SBUF=Hundreds_Cnt;
    while(!TI);
    TI=0;
    SBUF=Tens_Cnt;
    while(!TI);
    TI=0;
    SBUF=Ones_Cnt;
    while(!TI);
    TI=0;
}
 

Thread Starter

FBorges22

Joined Sep 11, 2008
109
I am still receiving a ASCII character in the hyperterminal... :(

I need to receive a decimal number that correspond to the state of the P1 Port...

For example, if the P1 port register is at 0xFF I need to receive the "255" number in the HyperTerminal when I press the "a" key.

That problem is sure very tricky... Any olther ideas to correct this?
 

RiJoRI

Joined Aug 15, 2007
536
OK, so now it looks like it shouldn't work! :(

The function changes 255 into 2,5,5. And HT sees 3 characters, I'm guessing -- 0x02, 0x05, 0x05.

Before sending the characters out, add 0x30 to them -- the ASCII value for zero -- "SBUF = '0' + Hundreds_Cnt;". Then HT should see 0x32, 0x35, 0x35, or "255"

--Rich
 

Thread Starter

FBorges22

Joined Sep 11, 2008
109
We are almost there...

I modified the send_serial function to this:

Rich (BB code):
void send_serial()
{
    //SBUF=printf("%d\n",P1);
    xtod(P1);
    SBUF='0'+Hundreds_Cnt;
    while(!TI);
    TI=0;
    SBUF=0x30+Tens_Cnt;
    while(!TI);
    TI=0;
    SBUF=0x30+Ones_Cnt;
    while(!TI);
    TI=0;
    SBUF=0x0A; // sends the "\n" character to change the line in HT
    while(!TI);
    TI=0;
}
And I am receiving a "00/" when I press the "a" key in the HT. I was expecting "255" because the P1 register is at 0xFF. What we should do next?
 

Thread Starter

FBorges22

Joined Sep 11, 2008
109
RiJoRI,

I corrected the problem. The error was in the declaration of the char variables. In this application we need to use the unsigned keyword because the char has a range of -127 to +127 and I need a range of 0 to 255.

The function xtod was correct and the program is working now and now I have to develop and take care of the rest. Thanks for the tips :)
 
Top