dspic float pointer error

Beta Testing discussion on mikroC PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
alcidesramos
Posts: 272
Joined: 17 Feb 2009 02:39
Location: Colombia
Contact:

dspic float pointer error

#1 Post by alcidesramos » 07 Mar 2014 21:00

The compiler dont charge the value of a float pointer.
i show a example basic the problem in this code
i charge a char vector with values.
then i want to find the 4 bytes of float number from posicion 3

i use the number 34.5 float that is 42 0A 00 00 in ieee format

when simulete the variable valor= 0A 00 00 01 this incorret i hope (42 0A 00 00)
but the value pointer (pun_flota) if show corret 42 0A 00 00 please fix that. the corret float charge value
i attachamment the picture of register value(dspic variables.jpg)

this the code:

Code: Select all

char trama[20];

float *pun_flota,valor;

//float 34.5  = float 32 microchip  42 0A 00 00

void main()

{

 trama[0]=0x01;
 trama[1]=0x01;
 trama[2]=0x01;
 trama[3]=0x00;
 trama[4]=0x00;
 trama[5]=0x0A;
 trama[6]=0x42;
 trama[7]=0x00;
 trama[8]=0x01;

 pun_flota=&TRAMA[3];
 valor=*pun_flota;


}
Ok make the same exaple in mikroc for pic

Code: Select all

char trama[20];

float *pun_flota,valor;

//float 34.5  = float 32 microchip  84 0A 00 00

void main() 

{

 trama[0]=0x01;
 trama[1]=0x01;
 trama[2]=0x01;
 trama[3]=0x00;
 trama[4]=0x00;
 trama[5]=0x0A;
 trama[6]=0x84;
 trama[7]=0x00;
 trama[8]=0x01;

 pun_flota=&TRAMA[3];
 valor=*pun_flota;
 

}
in this example the float hax value is the microchip float format = 84 0A 00 00 for 34.5 float value.
ok when simulet wrk ok the variable valor=84 0A 00 00 that it the correct float and the pointer pul_float= 84 0A 00 00 too
i shoe this in the pic ok.jpg file send to you
the problem with float pointer is in mikroc for dspic.

please fix this.

kind regards
Alcides Ramos
Attachments
Pointer dspic problem.rar
(90.98 KiB) Downloaded 272 times

alcidesramos
Posts: 272
Joined: 17 Feb 2009 02:39
Location: Colombia
Contact:

Re: dspic float pointer error

#2 Post by alcidesramos » 14 Mar 2014 04:57

:( Please mikroe some answer about?

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: dspic float pointer error

#3 Post by dejan.odabasic » 19 Mar 2014 13:45

Hello,

PIC(8bit) and dsPIC(16bit) differ in memory organization.
This is the cause of difference in behaviour.

If you wish to extract the float value from byte array you need to make sure that first byte is aligned to even address.
Please take a look at following example (test it with Software debugger option):

Code: Select all

char fbytes[20];
char fb_even[4] absolute 0x100C; // Even address
char fb_odd[4] absolute 0x1101;  // Odd address
float *pf, val;
//float 34.5  = float 32 microchip  42 0A 00 00
void main()
{
  fbytes[0]=0x00;
  fbytes[1]=0x00;
  fbytes[2]=0x0A;
  fbytes[3]=0x42;
 
  fbytes[4]=0x00;
 
  fbytes[5]=0x00;
  fbytes[6]=0x00;
  fbytes[7]=0x0A;
  fbytes[8]=0x42;
 
 pf = &fbytes[0];
 val = *pf;       // OK, variables aligned
 
 pf = &fbytes[5];
 val = *pf;       // ERROR misalignment

 // Manual alignment
 memcpy(fb_even, fbytes, 4);
 memcpy(fb_odd, fbytes, 4);
 
 pf = &fb_even;
 val = *pf;      // OK
 
 pf = &fb_odd;
 val = *pf;     // ERROR
 
 while(1)
  ;
}
Best regards.

alcidesramos
Posts: 272
Joined: 17 Feb 2009 02:39
Location: Colombia
Contact:

Re: dspic float pointer error

#4 Post by alcidesramos » 24 Mar 2014 07:09

Ok thank you.

then if i have a vector of char for example modbus trame. but the float number begin in odd address i can use float pointer for read more easy the float numbers.

ok, in this moment i use char pointer for this case.


char *pu_cha,*pu_cha1; //char pointer
float *pf, val;
//float 23.4556 = float 32 microchip 41 BB A5 12
void main()

{

fbytes[0]=0x12;
fbytes[1]=0xA5;
fbytes[2]=0xBB;
fbytes[3]=0x41;

fbytes[4]=0x00;

fbytes[5]=0x12;
fbytes[6]=0xA5;
fbytes[7]=0xBB;
fbytes[8]=0x41;



pu_cha1=&val;
pu_cha=&fbytes[5];
*pu_cha1=*pu_cha;
pu_cha++;
pu_cha1++;
*pu_cha1=*pu_cha;
pu_cha++;
pu_cha1++;
*pu_cha1=*pu_cha;
pu_cha++;
pu_cha1++;
*pu_cha1=*pu_cha;

//the foat "val" is ok value

}


thank you.

kind regards

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: dspic float pointer error

#5 Post by dejan.odabasic » 31 Mar 2014 13:13

Hello,

You can use Manual alignment method which is show in code snippet.

Best regards.

alcidesramos
Posts: 272
Joined: 17 Feb 2009 02:39
Location: Colombia
Contact:

Re: dspic float pointer error

#6 Post by alcidesramos » 30 Jun 2014 05:22

the explanation was very useful, every day one learns something new


kind regards


Alcides Ramos

Post Reply

Return to “mikroC PRO for dsPIC30/33 and PIC24 Beta Testing”