Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] ccs c complier int32 to float problem

Status
Not open for further replies.

dasarathan

Newbie level 5
Joined
Apr 19, 2009
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,331
hi guys

im using ccs c complier PCH 16 bit

im reading the float data from modbus rtu as the array of byte and i convert the bytes to int32

while im type casting the int32 to float the result is 0.00

Code:
float ModbusGetdata(int8 in_ValueReg)
{
   int8 Readcount = 0;
   int8 ReadBuffer[15] = {0}; 
   float32 fretval = 0.0; 
 //  int8 Timeoutcount =0; 
                                              
      uart_flush_buffer();                                         
      DOP_RX_EN(1);                                                 
      delay_ms(5);   
      fputc(0x01,RS485TX);   
      fputc(0x03,RS485TX);      
      fputc(0x00,RS485TX);                                                            
      fputc(PanelMeterCrc[in_ValueReg][0],RS485TX);
      fputc(0x00,RS485TX);      
      fputc(0x02,RS485TX); 
      fputc(PanelMeterCrc[in_ValueReg][1],RS485TX);
      fputc(PanelMeterCrc[in_ValueReg][2],RS485TX);    

//!   for(int LoopCnt = 0; LoopCnt < 8; LoopCnt++)
//!   {
//!      fputc(panelMeterPkt[in_ValueReg][LoopCnt],RS485TX);
//!   }
//!   UART_Write(0x0A);
   delay_ms(2);                            
   DOP_RX_EN(0);
   delay_ms(500);                
   if(uart_available())                             
   {                                                                               
      while(uart_available()) 
      {             
         
          ReadBuffer[Readcount++] = uart_read(); //clear buffer
      }
   }                               
   
unsigned int32 retdata = convert32(ReadBuffer[3],ReadBuffer[4],ReadBuffer[5],ReadBuffer[6]);   


lcd_clear_line(2);                            
printf(lcd_putc, "R:0x%LX",retdata);                                       

fretval =(*(Float *)&retdata);                                            
//memcpy(&fretval,&retdata,4);                                                                            
//! fretval = *(float *) &retdata;                                 
lcd_clear_line(1);                          
printf(lcd_putc, "R:%f",fretval);                                                 
return    fretval ;                                                                                

}
 
Last edited by a moderator:

Can you show the received int32 hex value? Is it a valid IEEE float number? Float isn't a standard MODBUS data type as far as I'm aware of, are you sure about the used data format?
 

Can you show the received int32 hex value? Is it a valid IEEE float number? Float isn't a standard MODBUS data type as far as I'm aware of, are you sure about the used data format?


the hex value for the modbus data is 0x 43 5d 85 73
the data received in modbus is bytes of data only we have to type cast the the four byte of data to float 32
im communicating with the l&t multifun meter WL4400 for the parameter like voltage current pf energy for the display purpose
 

Hello,
The hex value for the modbus data is 0x 43 5d 85 73

do you know what floating point value you must get ?


with a software tools like "Floatconv.exe" , i get :
43 5D 85 73
IEE754 32 bits => 221.521286011
MicroChip 32 bits => 1.50108662215E-018
MicroChip 24 bits & Hitech 24 bits :
24 Bits => only 3 bytes !

another way of issue :
Big Indian format ?
or reverse ...
...the war between Motorola & Intel
 

CCS C uses IEEE float Format. The corresponding float value for 0x435d8573 is not 0.00. Debug with hardware debugger or MPLAB simulator.
 

Hello,


do you know what floating point value you must get ?


with a software tools like "Floatconv.exe" , i get :
43 5D 85 73
IEE754 32 bits => 221.521286011
MicroChip 32 bits => 1.50108662215E-018
MicroChip 24 bits & Hitech 24 bits :
24 Bits => only 3 bytes !

another way of issue :
Big Indian format ?
or reverse ...
...the war between Motorola & Intel


i think the big and little indian format is not the issue because
i'm setting the int32 value in the code it self for the testing for that also im getting the same
 

I didn't read the original post thoroughly, thought you are compiling with PCD (PIC24). As stated, it uses IEEE float format. PCH in contrast uses Microchip 32 Bit Float format, which is similar to IEEE, but different in byte order and sign bit position, need to be converted.

- - - Updated - - -


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
union {
   float32 f;
   unsigned char c[4];
}
retval; 
   
union 
{
   unsigned int32 u;
   unsigned char c[4];
}
retdata;
 
// IEEE float format
retdata.u = make32(ReadBuffer[3],ReadBuffer[4],ReadBuffer[5],ReadBuffer[6]);
// Convert to MHCP float32  
retval.c[3] = retdata.c[0];
retval.c[2] = retdata.c[1];
retval.c[1] = retdata.c[2] & 0x7f | retdata.c[3] & 0x80;
retval.c[0] = (retdata.c[3] & 0x7f) << 1  | (retdata.c[2] & 0x80) >> 7;
return  retval.f;

 

Re: ccs c complier int32 to float problem "solved "

I didn't read the original post thoroughly, thought you are compiling with PCD (PIC24). As stated, it uses IEEE float format. PCH in contrast uses Microchip 32 Bit Float format, which is similar to IEEE, but different in byte order and sign bit position, need to be converted.

- - - Updated - - -


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
union {
   float32 f;
   unsigned char c[4];
}
retval; 
   
union 
{
   unsigned int32 u;
   unsigned char c[4];
}
retdata;
 
// IEEE float format
retdata.u = make32(ReadBuffer[3],ReadBuffer[4],ReadBuffer[5],ReadBuffer[6]);
// Convert to MHCP float32  
retval.c[3] = retdata.c[0];
retval.c[2] = retdata.c[1];
retval.c[1] = retdata.c[2] & 0x7f | retdata.c[3] & 0x80;
retval.c[0] = (retdata.c[3] & 0x7f) << 1  | (retdata.c[2] & 0x80) >> 7;
return  retval.f;



Dear Team

Thanks for the support
it is working fine
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top