CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

4byte array convert to float

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
seyoung



Joined: 11 Nov 2016
Posts: 8

View user's profile Send private message Send e-mail

4byte array convert to float
PostPosted: Fri Nov 11, 2016 10:25 am     Reply with quote

Code:

void OPC_N2_data_read()
{   
   unsigned long PM_int[3] = { 0x00, };
   unsigned char iobyte[3][4] = {0x38,0x95,0x86,0x42,0xd8,0xfe,0x9d,0x42,0xe3,0xb3,0x9f,0x42 };
   float PM[3] = { 0x00, };
   int i;
   for(i = 0; i<3; i++)
   {
    //PM_int[i] = ((iobyte[i][3] & 0xFF) <<24);
    //PM_int[i] += ((iobyte[i][2] & 0xFF) <<16);
    //PM_int[i] += ((iobyte[i][1] & 0xFF) <<8);
    //PM_int[i] += iobyte[i][0] & 0xFF;
    PM_int[i] = ((iobyte[i][3]<<24)|(iobyte[i][2]<<16)|(iobyte[i][1]<<8)|(iobyte[i][0]));
           
    PM[i] = *((float*)&PM_int[i]);
   }
   printf ("%x%x%x%x\r\n",iobyte[0][0],iobyte[0][1],iobyte[0][2],iobyte[0][3]);
   printf ("%x%x%x%x\r\n",iobyte[1][0],iobyte[1][1],iobyte[1][2],iobyte[1][3]);
   printf ("%x%x%x%x\r\n",iobyte[2][0],iobyte[2][1],iobyte[2][2],iobyte[2][3]);
   printf ("%lx,%lx,%lx\r\n", PM_int[0],PM_int[1],PM_int[2]);
   printf ("%3.5f,%3.5f,%3.5f\r\n", PM[0],PM[1],PM[2]);
}

If i run the above code using another compiler, it will behave normally follow.

38958642
d8fe9d42
e3b39f42
42869538,429dfed8,429fb3e3
67.29144,78.99774,79.85134

However, if you compile with pcw and run it through pic18j10, you will get follow:

38958642
d8fe9d42
e3b39f42
0038,00d8,00e3
0.00000,0.00000,0.00000

I do not know where the problem is.
gaugeguy



Joined: 05 Apr 2011
Posts: 291

View user's profile Send private message

PostPosted: Fri Nov 11, 2016 10:32 am     Reply with quote

You need to cast iobyte to an int32 in the expression before shifting it.

(int32)iobyte[i][3]<<24
seyoung



Joined: 11 Nov 2016
Posts: 8

View user's profile Send private message Send e-mail

PostPosted: Fri Nov 11, 2016 10:37 am     Reply with quote

Code:

PM_int[i] = (((int32)iobyte[i][3]<<24)|((int32)iobyte[i][2]<<16)|((int32)iobyte[i][1]<<8)|((int32)iobyte[i][0]));


change and test ..

however. i receive following..

38958642
d8fe9d42
e3b39f42
9538,fed8,b3e3
0.00000,0.00000,0.00000
seyoung



Joined: 11 Nov 2016
Posts: 8

View user's profile Send private message Send e-mail

PostPosted: Fri Nov 11, 2016 10:38 am     Reply with quote

unsigned long PM_int[3] = { 0x00, }

is this code change like..

unsigned int32 PM_int[3] = { 0x00, } ??
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 11, 2016 12:38 pm     Reply with quote

I modified your program to make it work. It now outputs:
Quote:

38958642
d8fe9d42
e3b39f42
42869538,429dfed8,429fb3e3
67.29144,78.99774,79.85134

Test program:
Code:
#include <18F46K22.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

// The following file has routines to convert between IEEE floating
// point format, and Microchip PIC 32-bit floating point format.
#include <ieeefloat.c>

void OPC_N2_data_read()
{   
int32 PM_int[3] = {0,0,0};

unsigned char iobyte[3][4] =
{
{0x38,0x95,0x86,0x42},  // 67.29144 in IEEE format
{0xd8,0xfe,0x9d,0x42},  // 78.99774 in IEEE format
{0xe3,0xb3,0x9f,0x42}   // 79.85134 in IEEE format
};

float PM[3] = {0,0,0};
int i;

for(i=0; i<3; i++)
   {
    PM_int[i] = (((int32)iobyte[i][3]<<24) | ((int32)iobyte[i][2]<<16) | ((int32)iobyte[i][1]<<8) | ((int32)iobyte[i][0]));
    PM[i] =  f_IEEEtoPIC((int32)PM_int[i]);
   }

   printf ("%x%x%x%x\r\n",iobyte[0][0],iobyte[0][1],iobyte[0][2],iobyte[0][3]);
   printf ("%x%x%x%x\r\n",iobyte[1][0],iobyte[1][1],iobyte[1][2],iobyte[1][3]);
   printf ("%x%x%x%x\r\n",iobyte[2][0],iobyte[2][1],iobyte[2][2],iobyte[2][3]);
   printf ("%lx,%lx,%lx\r\n", PM_int[0],PM_int[1],PM_int[2]);
   printf ("%3.5f,%3.5f,%3.5f\r\n", PM[0],PM[1],PM[2]);

}
//======================================
void main()
{

OPC_N2_data_read();

while(TRUE);
}


These posts contain a link to the floatconv.exe program and explain
how to use it. You can use it to convert from floating point numbers
to bytes, or to Microchip format floats, or to IEEE floats, etc.
http://www.ccsinfo.com/forum/viewtopic.php?t=22336&start=1
http://www.ccsinfo.com/forum/viewtopic.php?t=50713&start=4

Example of how to use FloatConv.exe:

Start the program. Select "IEEE754 32bit". Then type the following
numbers in the boxes for Byte1 through Byte4: 42 86 95 38
The click the "Convert To Float" button. It will display 67.2914428711
in the Float box. This is correct.

This CCS FAQ page explains the Microchip floating point format,
which is used by CCS:
http://www.ccsinfo.com/faq.php?page=mchp_float_format
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group