Member Avatar for bhaltulpule

I am trying to do simple math in c using a microcontroller. The C toolset uVision that I have does not have printf scan etc functions.
In any case the code below reads in two parameters p1, p2 from the user over a serial port usig a simple routine GetHexByte () and copies the parameters into p1 and p2 using a couple of union functions for int (16 bit) and long (32 bit).
The results are displaed via a serial port using a simple routine SendASCII (), which displays ascii representation of the results.
if I enter 1234 for p1 and 5678 for p2, I get 00000060 as answer (r1 bytes)
What am I doing wrong ?
Is it not possible to do fixed point math in C ?
if I convert p1, p2 to float then it works as shown below.
==> r1 (int) = (float) p1 * (float)p2);

Can anyone help ?

Thanks.
Bhal
Code is attached below (except for GetHexByte, Disp String and SendAscii) only because they are long and have been working perfectly.
Code follows.......

union FIXTYPE

    unsigned int i; //int = 16 bit//changed to unsigned...
    unsigned char c[2];
};
union FIXTYPE ic;
union LONGTYPE
{
    long l;
    unsigned char c[4];
};
union LONGTYPE lc;
---
void main (void) {
---
    unsigned int xdata p1,p2;
    unsigned long xdata r1;
---
MADSM_DATA_GEN:
    SendByte(0x09);DispString ("enter 2 hex char for param p1, p2 \n");
    GetHexByte();ic.c[0]=USER_BYTE;GetHexByte();ic.c[1]=USER_BYTE;
    p1 = ic.i;
    SendAscii (ic.c[0]);SendAscii (ic.c[1]);
    SendByte(0x09);
    GetHexByte();ic.c[0]=USER_BYTE;GetHexByte();ic.c[1]=USER_BYTE;
    p2 = ic.i;
    SendAscii (ic.c[0]);SendAscii (ic.c[1]);
    r1 = p1 * p2; lc.l = r1;
//  p1 = 1234; p2 = 5678;
//  r1 = (float)p1 * (float)p2;
//  lc.l = (long int)r1;
    SendAscii (lc.c[0]);SendAscii (lc.c[1]);SendAscii (lc.c[2]);SendAscii (lc.c[3]);
goto MADSM_DATA_GEN;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.