This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

type conversion problem

I want to convert characters recieved from MatLab into unsigned or signed integers in Keil... how do I do that?

  • We are going to need more information.

    Do you mean that the characters are coming from a file or serial input? That is really a radix conversion rather than a type conversion (casting) problem.

  • If you are converting human-readable ASCII text to binary values, the scanf() or atoi() functions are probably what you want.

  • detailed explaination:

    actually i am interfacing MatLab & Keil for my final year project at college.

    My group fellows are doing image processing in MatLab and sending me data serially which i am recieving in Keil.

    They detect a moving object and send me its distance from threshold value (center of the screen). I recieve that value and move my mount which resultantly moves my camera which is mounted on a two axes movable mount. Two axes are handled by two servo motors.

    Now the problem I am facing is... If they send me plain characters ike 'a', 'b', 'c',..... they are directly read as the same characters in Keil.

    My fellow group members will not send me characters but they will send me integer values .... I am using the following code segment to read in the integers but to no avail...

    SCON=0x52;
    TMOD=0x21;
    TH1=-12;
    TR1=1;
    TI=1;
    RI=1;

    unsigned int a;
    scanf("%u",&a);

    One alternative to this is to recieve the characters and convert them to integers....Is there any routine which does that?

    There is a routine "toint" what does it do?

    Please help..

  • "There is a routine "toint" what does it do?"

    Look it up in the Manual!

  • "My fellow group members will not send me characters but they will send me integer values"

    The 8051 serial interface (and a PC's COM: port) handles data a byte at a time. Therefore, you need to know:

    1. How many bytes make up an "integer?!

    2. If your "integers" are more than 1 byte, in what order are those bytes sent?

  • scanf() converts ASCII to binary. If the producer program is sending you binary integers, you do not need scanf(). Instead, you would just receive the data directly into your destination integer.

    You might use a C union, with a byte array overlaying an integer. Or just use pointer arithmetic.

        U16 myInt;
        U8* nextByte;
    
        len = 0;
        nextByte = &myInt;
        while (len < sizeof(myInt))
            {
            *nextByte++ = WaitForSerialChar();
            ++len;
            }
    

    You're likely to want the ability to abort the routine on a timeout, and other such improvements for a serious application.

    C51 stores integers in big-endian format. If the transmitter sends little-endian, you'll have to reverse the order in which you fill the destination.