Decimal conversion to Hex value to load a PLL

For general Flowcode discussion that does not belong in the other sections.
Post Reply
chezkrimles
Posts: 9
http://meble-kuchenne.info.pl
Joined: Thu Jan 14, 2021 7:12 pm
Has thanked: 2 times

Decimal conversion to Hex value to load a PLL

Post by chezkrimles »

Hi,

As an example, I am trying to convert decimal 1075 to hex to load the N counter of a PLL (MC145170).
I know that 0x433 or 0x0433 loaded in manually works.
How do I convert 1075 decimal to 0x433? Can't figure out what I am doing wrong.
Would appreciate if someone could help. Thanks.

chipfryer27
Valued Contributor
Posts: 1149
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 285 times
Been thanked: 412 times

Re: Decimal conversion to Hex value to load a PLL

Post by chipfryer27 »

Hi

I haven't used that chip so I may be completely wrong.

Page 17 of the datasheet explains the 16-bit number structure and as far as I can see for convenience and ease of reading it is "broken" into 4 x 4-bit Hex values for convenience.

16-bits = 1111111111111111 in binary, FFFF in Hex and 65535 in Decimal. Writing in Hex is easier to read and at the side of the chart it shows the decimal equivalent of the 16-bit number(s)

From the chart you can have the value set at any value between 0x28 to 0xFFFF which = 40 to 65535 in decimal.

So, you can simply send 1075 in decimal, 0x433 in Hex or 0b0000010000110011 in Binary and I'm guessing this is the issue as the SPI component accepts Byte values which are 8-bits, not the 16-bits required. Therefore you need to convert the 16-bit number to 2 x 8-bit numbers and send.

You could for example convert your value to 2 x 8-bit values and send by the following (assuming value = 1075 / 0b0000010000110011)

Byte1 = value >> 8 this shifts value 8-bits to the right giving 0x4 / 00000100 / 4 in Hex/ Binary / Decimal
Byte2 = value & 255 which results in 0x33 / 00110011 / 51 in Hex / Binary / Decimal

Enable CS
Send Byte1
Send Byte2
Disable CS

Below is a capture when simulating
SPI.JPG
SPI.JPG (85.06 KiB) Viewed 562 times

Hope this is what you are after.

Regards

Steve-Matrix
Matrix Staff
Posts: 1253
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 167 times
Been thanked: 277 times

Re: Decimal conversion to Hex value to load a PLL

Post by Steve-Matrix »

chezkrimles wrote:
Thu Jan 25, 2024 12:40 am
How do I convert 1075 decimal to 0x433?
As chipfryer27 says, you can send 1075 or 0x433 as these are the same value. No need for conversion. The chip and compiler treat these values exactly the same. Writing them in decimal or hexadecimal (or binary) format is purely for human benefit.

So writing "x = 1075" and "x = 0x433" achieve the same result and are equivalent as far as the device is concerned.

As an aside, sometimes you will want to present a value in a format that is readable by a human. This requires converting the number to text. There are a number of options to do this and then you can choose to display the value as a decimal (using the ToString$ function) or hex (using the NumberToHex$ function).

chezkrimles
Posts: 9
Joined: Thu Jan 14, 2021 7:12 pm
Has thanked: 2 times

Re: Decimal conversion to Hex value to load a PLL

Post by chezkrimles »

It works now - thank you so much. I was overthinking things.

Post Reply