BCD Conversion

Recommended Posts


I have read the post on BCD conversion and the formula to use

sum((bcd & {0xf000,0xf00,0xf0,0xf}) / {4096,256,16,1} * {1000,100,10,1})

I'm not sure how to use it in conjunction with the Channel data returned

The manual says for the piece of equipment I am using that the hour and day are BCD with the hour lower byte

I am using I/O read holding U16 (3) which is returning 5409 for 15th day and 2100 hour - is this the correct I/O to use?

(for Month and Year (9th mth 2017 year) using the same I/O on a different Channel I am returning 5897)

I assume by substituting 5409 for bcd in the above formula I would get an integer and then by using floor and modulus functions I would get the correct values - I'm not...

(I have used the floor and Modulus functions on other equipment / data conversions and have got it to work)

Your help in the correct method to convert the BCD values most appreciated











Link to comment
Share on other sites

Actually, its probably easier to split the word into the two bytes before you feed it into the BCD function.  To get the low byte of a word, just do:

value & 0xff

To get the high byte when you know its a word (and not a long) do:

value >> 8

Then feed the result into the bcd function in place of the "bcd" and you'll get what you want (I checked!)

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.