How can I convert from Q3.5 format to decimal using an 8-bit microcontroller?

In summary, you can go from a number in an unsigned Q3.5 format to 8 bits given by dividing the number by 2^5. You can store the fractional bits in an int and bit mask it to get the 5 bits representing the fractional.
  • #1
PhantomPower
14
0
Hello,

I am currently trying to figure out if its possible to go from a number in (unsigned) Q3.5 format -> 8 bits given.

And print it on a display in decimal.

I only have a 8 bit microcontroller to work with.

For example 011.11010 should have 3.8125

Getting to 3 is easy but I can't see how I could get 8125 - I know I need to divide the 26 by 2^5 but this would give me 0 as an inside an 8 bit microcontroller.

Any tips - Or some C source code would be lovely.
Thanks in advance.
 
Technology news on Phys.org
  • #2
PhantomPower said:
Hello,

I am currently trying to figure out if its possible to go from a number in (unsigned) Q3.5 format -> 8 bits given.

And print it on a display in decimal.

I only have a 8 bit microcontroller to work with.

For example 011.11010 should have 3.8125

Getting to 3 is easy but I can't see how I could get 8125 - I know I need to divide the 26 by 2^5 but this would give me 0 as an inside an 8 bit microcontroller.
The part of your number to the right of the 'binary' point is a binary fraction.

.11010 means 1 * 2-1 + 1 * 22 + 0 * 2-3 + 1 * 2-4 + 0 * 2-5
= 1/2 + 1/4 + 0/8 + 1/16 + 0/32
= 13/16
= .8125

Please excuse me if you already know this. At first I thought you didn't, because of what you said about 26. I then realized that you were adding the bits, and didn't realize that you were representing the fraction as 26/32, which is the same as 13/16.

Binary fractions work exactly like decimal fractions except that instead of having decreasing negative powers of 10, you have decreasing negative powers of 2.
PhantomPower said:
Any tips - Or some C source code would be lovely.
Thanks in advance.

I recommend using a lookup table. The table (an array) should have 32 elements, one for each possible value of a 5 bit number. Each element in the table is a string of decimal digits.

Index the table by the sum of the 5 bits.
 
  • #3
Hey, thanks for the reply.

Yeah I figure out how to get the conversions - Sorry about my ambigous language.

Figured out if I store it in a int (4 bytes) then I can bit mask to get the 5 bits representing the fractional - then multiply by 100000 and divide by 2^5 giving me the fractional bits without the point.

Thanks
 

FAQ: How can I convert from Q3.5 format to decimal using an 8-bit microcontroller?

What is fixed point to decimal conversion?

Fixed point to decimal conversion is the process of converting a number represented in fixed point notation to its decimal equivalent. Fixed point notation is a method of representing numbers with a fixed number of digits after the decimal point.

Why is fixed point to decimal conversion important?

Fixed point to decimal conversion is important because it allows for easier understanding and comparison of numbers. Decimal numbers are more commonly used in everyday life and it is easier to perform calculations with them.

What is the formula for converting fixed point to decimal?

The formula for converting fixed point to decimal is to first determine the value of each digit based on its position, then add all the values together. For example, in a number represented in fixed point notation as 123.45, the value of the first digit (1) is 100, the value of the second digit (2) is 10, and the value of the third digit (3) is 1. So the decimal equivalent would be (1*100) + (2*10) + (3*1) + (4/10) + (5/100) = 123.45.

What are some common applications of fixed point to decimal conversion?

Fixed point to decimal conversion is commonly used in financial calculations, such as interest rates and currency conversions. It is also used in computer programming and data storage to represent numbers with a fixed precision.

What are some tips for performing fixed point to decimal conversion accurately?

Some tips for performing fixed point to decimal conversion accurately include understanding the concept and formula thoroughly, double-checking your calculations, and using a calculator or computer program if necessary. It is also helpful to be familiar with common fixed point notation formats and their corresponding decimal equivalents.

Similar threads

Replies
7
Views
2K
Replies
13
Views
5K
Replies
34
Views
21K
Replies
8
Views
1K
Replies
7
Views
7K
Replies
5
Views
2K
Back
Top