简体   繁体   中英

How to change a char to ASCII form?

I need change my program, how do I use some function change a char to ASCII? my mobile receive data from 8051, always show '0', it's not true.

char to ASCII code.

void Data_TX(unsigned char Y)
{
    unsigned char Buff_Y[3];

    Buff_Y[2] = (Y / 100) + 0x30;
    Buff_Y[1] = (Y / 10) % 10+0x30;
    Buff_Y[0] = (Y % 10) + 0x30;

    SBUF = *Buff_Y;
    while (TI == 0);
    TI = 0;
} 

this's my circuit original code, LCD interfacing with 8051.

bit Sensor_read(unsigned char read[5]);

use my function.

Data_TX(read[2]); //read data.
Data_TX((int)read[2]); //read data, this program can't to run. 
Buff_Y[2] = (Y / 100) + 0x30;
Buff_Y[1] = (Y / 10) % 10+0x30;
Buff_Y[0] = (Y % 10) + 0x30;

This is all good. If you print those three bytes, you will see that they contain the right values.


SBUF = *Buff_Y;

This, however, indicates confusion. *Buff_Y is equivalent to Buff_Y[0] . That is a single char value... If you can only store a single char value into SBUF , then you can't store the three char values you want to store into it.

If we use logic, we can make this inference: That char value will be '0' if the input you provide is evenly divisible by 10 (that is, when Y % 10 is 0 )...

While you're thinking about how to change this line of code (and the parts of your code that we can't see), you might also want to think carefully about while (TI == 0); . There ought to be a better solution to whatever problem that is attempting to solve.

I'm change this code, can be run, but just received digit in ones.

void Data_TX(unsigned char Y)

{ unsigned char Buff_Y[3];

Buff_Y[3] = (Y / 100) + 0x30;
Buff_Y[2] = (Y%100)/10+0x30;
Buff_Y[1] = (Y / 10) % 10+0x30;
Buff_Y[0] = (Y % 10) + 0x30;

SBUF = *Buff_Y;
while (TI == 0);
TI = 0;

}

Data_TX((int)read[2]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM