Floating point storing

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Floating point storing

3,081 Views
EM1
Contributor I
Hello,
I am using an external serial flash memory for my HCS12.
I want to store floating point numbers in this flash. can anybody please tell me if the following
is the right way to deal with floating point storage.
suppose we have "d" as a floating point number.
float d ;
float *pt ;
pt=&d;
char i;
for (i=0; i<=3;i++)
 {
  send_serial (*(pt++)); // send_serial, is a function to send a byte through the SPI0.
 }
 
Now for reading the stored floating point number and send it to the SCI0
char buffer[4];              // a temporary buffer to store the 4bytes of the stored floating point number.
float *pt=&buffer;
for (i=0;i<=3;i++)
 {
  get_serial (*(pt++)); // get_serial is a function to receive one byte from the SPI0.
  
 }
 printf ("%6.3F",pt);   // send the received floating point number to the SCI0.
\\**********************************************************************************
 
thanks a lot and I appriciate any help here.
 
EM1.
 
0 Kudos
Reply
5 Replies

946 Views
bigmac
Specialist III
Hello EM1,
 
Since you have declared pt to be a pointer to a float, when you increment the pointer, this will not give your intended result since the increment would occur over four bytes.
 
Perhaps you could form a union between a float value and a char array (of 4 bytes length).  This way you could identify the value of the individual bytes.  The following untested code is shown for the write process.
 
typedef union {
  byte byteval[4];
   float val;
} float_val;
 
char i;
float_val d;
d.val = 123.456;
 
/* Enable /SS line (slave select) to SPI slave device */
/* Additional code required here to set up serial flash write */
for (i=0; i<4; i++)
  send_serial(d.byteval[i]);
 
/* Disable /SS line */
 
The additional code requirements will depend on the serial flash device used.  For reading the stored data, the get_serial() function might simply return a char value.
 
/* Enable /SS line (slave select) to SPI slave device */
/* Additional code required here to set up serial flash read */
for (i=0; i<4; i++)
  d.byteval[i] = get_serial();
 
/* Disable /SS line */
 
Regards,
Mac
 
0 Kudos
Reply

946 Views
EM1
Contributor I
Bigmac..
it worked !!!
 
thanks a lot..
 
one more thing please..
I am trying to send 24bits to SPI0. first 13 bits then 11 bits . to form the write command and address of the flash memory to which the SPI0 is connected.
can you help me in arranging these bits from 4byte.
in other words.
I have to send 13 bits for the page address and 11 bits for the byte address.
those addresses are in 2 integers (2x2bytes=4bytes). in this case, I need the to take the last 13bits of the 2byte for the page address and then take the last 11bits of the 2byte (the byte address) and send them togther without any other bits.
 
do you have an idea about how to do so?
thanks again for the help..
 
EM1.
0 Kudos
Reply

947 Views
bigmac
Specialist III
Hello EM1,
 
The following code might be a possibility, with some similarities to my previous post -
 
#define dword unsigned long
 
typedef union {
  byte byteval[4];
  dword val;
} Address;
 
char i;
word flash_page;  /* 13-bit value, right justified */
word flash_addr;  /* 11-bit value, right justified */
Address addr;
 
addr.val = ((dword)flash_page << 11) | ((dword)flash_addr & 0x000007FF);
 
for (i=0; i<3; i++)
  send_serial(addr.byteval[i+1]);
 
Regards,
Mac
 
0 Kudos
Reply

947 Views
EM1
Contributor I
Hello Bigmac, what's up?
 
thanks for your replies..
I developed some manual sifting to get the serial bit stream for the addresses.
 
thanks a lot for your time and for the new ideas.
 
take care..
 
EM1.
0 Kudos
Reply

947 Views
pittbull
Contributor III
Hello,

Try it this way:


// These values contain the bits
unsigned short bits13;
unsigned short bits11;

// shift them together to form a 24 bit value
// the upper byte is not used
unsigned long joint_value = ((unsigned long)bits13)<<11 | (bits11 & 0x7ff);

// send three bytes
send_to_spi (((unsigned char*)&joint_value)[2]);
send_to_spi (((unsigned char*)&joint_value)[1]);
send_to_spi (((unsigned char*)&joint_value)[0]);

Don't know if that code works, but it shows the principle
:smileywink:
0 Kudos
Reply