<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Reading a 32-bit floating point value from register</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/19299/reading-a-32-bit-floating-point-value-from-register</link><description>Hello, 
 I am using SEGGER Embedded Studio together with a nRF52832 and I am trying to read an EEprom register containing a 32-bit floating point value in four bytes (register address 0x10, 0x11, 0x12, 0x13). 
 I have seen examples of someone using</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sun, 29 Jan 2017 07:32:34 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/19299/reading-a-32-bit-floating-point-value-from-register" /><item><title>RE: Reading a 32-bit floating point value from register</title><link>https://devzone.nordicsemi.com/thread/74809?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2017 07:32:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5df6cd8-0afe-4303-8ae2-cbb1d0d2618f</guid><dc:creator>Jan Lundskov</dc:creator><description>&lt;p&gt;Thanks for your help, it is appreciated. I am not quite sure how to cast pointers, since my experience with pointers is limited. I will be able to see if the union code works once I am able to print float values using RTT. I have posted a separate question on this topic.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reading a 32-bit floating point value from register</title><link>https://devzone.nordicsemi.com/thread/74808?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2017 03:23:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e8d8e27b-071c-49a8-b910-95c41af549e1</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;I presume you have code that is reading the data into floatArray[], so you could read directly into floatConverter.bytes&lt;/p&gt;
&lt;p&gt;Alternatively, you could achieve the same results by casting pointers&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reading a 32-bit floating point value from register</title><link>https://devzone.nordicsemi.com/thread/74807?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2017 00:52:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:97aed8b2-196f-43bc-b2d8-16a5ccb3bd64</guid><dc:creator>Jan Lundskov</dc:creator><description>&lt;p&gt;Just one more question to make sure that I understand the union code. I guess it is the 8-bit value stored in each register that you are referring to in the above example and not the address of the register. Is that correctly understood?
So in my case it would loook something like this:&lt;/p&gt;
&lt;p&gt;uint8_t floatArray[4];
floatArray[0] = registervalue_0;  // 8 bit value read from register address 0x10
floatArray[1] = registervalue_1;    // 8 bit value read from register address 0x11
floatArray[2] = registervalue_2;    // 8 bit value read from register address 0x12
floatArray[3] = registervalue_3;    // 8 bit value read from register address 0x13&lt;/p&gt;
&lt;p&gt;Where registerValue_0 - registervalue_3 is the value of the 8 bits stored in each of the four registers. I have used the address to retrieve the value from the register.&lt;/p&gt;
&lt;p&gt;So the union code would look like this:&lt;/p&gt;
&lt;p&gt;floatConverter.bytes[0]=floatArray[0];
floatConverter.bytes[1]=floatArray[1];
floatConverter.bytes[2]=floatArray[2];
floatConverter.bytes[3]=floatArray[3];&lt;/p&gt;
&lt;p&gt;Or simply just assigning the register values directly.&lt;/p&gt;
&lt;p&gt;Or am I getting this wrong?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reading a 32-bit floating point value from register</title><link>https://devzone.nordicsemi.com/thread/74806?ContentTypeID=1</link><pubDate>Sun, 29 Jan 2017 00:34:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:636a06e0-ef40-442f-b3c6-f035f49c6a54</guid><dc:creator>Jan Lundskov</dc:creator><description>&lt;p&gt;Hi Roger,
Thank you very much for your reply. I have added the code and it compiles and runs without any errors. The next issue I see is that I am not able to print a float value using RTT viewer (JLinkRTTViewer). It works fine when printing integer values. This is the code:&lt;/p&gt;
&lt;p&gt;char float_str[80];
sprintf(float_str, &amp;quot;The 32-bit float value is %f\n&amp;quot;, eepromFloat);&lt;br /&gt;
SEGGER_RTT_WriteString(0, float_str);
I have tried with &amp;#39;%d&amp;#39;, &amp;#39;%0.1f&amp;#39; etc. but it makes no difference.&lt;/p&gt;
&lt;p&gt;Do you know if it is possible to print float values using RTT, so that it is possible to debug the code?&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reading a 32-bit floating point value from register</title><link>https://devzone.nordicsemi.com/thread/74805?ContentTypeID=1</link><pubDate>Sat, 28 Jan 2017 22:50:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1b8ca720-6045-446a-b5a9-4cdd26ddf29c</guid><dc:creator>Roger Clark</dc:creator><description>&lt;p&gt;There are a few things to consider with this.&lt;/p&gt;
&lt;p&gt;Firstly whether the data in EEPROM register is a floating point value that is in the same format as the compiler you are using expects.&lt;/p&gt;
&lt;p&gt;If you wrote the EEPROM using the nRF52 it will be in the correct format, but if the value was written by a different processor, potentially running code generated by a different compiler etc the way the number is stored could be completely different.&lt;/p&gt;
&lt;p&gt;Assuming its the same format, then yes you can use a union to do this, e.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;union
{
float floatVal;
uint8_t bytes[4];
} floatConverter;


	floatConverter.bytes[0]=0x10;
	floatConverter.bytes[1]=0x11;
	floatConverter.bytes[2]=0x12;
	floatConverter.bytes[3]=0x13;

float eepromFloat = floatConverter.floatVal;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You could also just cast the byte array as a pointer to float and get its contents.&lt;/p&gt;
&lt;p&gt;Note. Byte order may be not 0,1,2,3 it could be 3,2,1,0 or even 2,3,0,1 etc&lt;/p&gt;
&lt;p&gt;It really depends on what stored the float in the EEPROM in the first place&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>