Page 1 of 2

[NXC][API][SE] Use of bcd2dec

Posted: 26 Feb 2011, 05:48
by muntoo

Re: Use of NXC function bcd2dec

Posted: 26 Feb 2011, 06:33
by mattallen37
According to the help files, it looks like it would return the number 5 if you were to pass the number 101 into it. Or return 42 52 (thanks Andy) if you pass 101010 into it. It looks like it converts a binary appearing number into the decimal equivalent to it. I'll test to confirm what the help file appears to say.

Re: Use of NXC function bcd2dec

Posted: 26 Feb 2011, 06:57
by mattallen37
Ok, I did some tests, and it always returns 0. I don't know if it doesn't work, or if I am doing something wrong (not likely with only one line of relevant code).

Re: Use of NXC function bcd2dec

Posted: 26 Feb 2011, 08:20
by gloomyandy
Well I've no idea what the bcd2dec NXC function actually does but the decimal equiv of bcd 101010 is 52 (or possibly 25 depending on your nibble order!) not 42... You treat the two nibbles as a binary codec decimal digit so 010 is 2 and 101 is 5.... as to 0x3a well 0xa is not a valid bcd value...

Andy (who is old enough to remember when microprocessors had instructions to manipulate bcd *and* programmers actually used them!)

Re: Use of NXC function bcd2dec

Posted: 26 Feb 2011, 08:53
by mattallen37
gloomyandy wrote:...the decimal equiv of bcd 101010 is 52 (or possibly 25 depending on your nibble order!) not 42...
Yes, you are right. Duh me, I don't know what I was thinking. Sorry.

Re: Use of NXC function bcd2dec

Posted: 26 Feb 2011, 09:10
by mightor
I remember using CMOS BCD converter chips for controlling 7 segment displays :) Oh, that's a long time ago.

- Xander

Re: Use of NXC function bcd2dec

Posted: 26 Feb 2011, 09:29
by mattallen37
mightor wrote:I remember using CMOS BCD converter chips for controlling 7 segment displays :) Oh, that's a long time ago.

- Xander
I remember that as well, but it wasn't all that long ago for me (maybe 4-6 years). Actually, a project I did not too long ago (maybe a year ago) for the NXT used some of them. It was a 4 x 7-segment display for the NXT.

Re: Use of NXC function bcd2dec

Posted: 28 Feb 2011, 09:04
by afanofosc
So is the API function in NXC working or not? It is used by the Mindsensors RTClock API function so it was working once upon a time when I tested that API function. Here's the code that is generated when you use bcd2dec:

Code: Select all

  acquire __bcd2DecMutex 
  div __bcd2DecTens, _bcd, 16 
  mod __bcd2DecOnes, _bcd, 16 
  mul _result, __bcd2DecTens, 10 
  add _result, _result, __bcd2DecOnes 
  release __bcd2DecMutex
I guess this would return "invalid" decimal values if you input invalid bcd values. I don't think I will bother adding a check that the result of the mod operation is less than 10. I will update the docs with a proper example, though.

John Hansen

Re: Use of NXC function bcd2dec

Posted: 28 Feb 2011, 10:47
by mattallen37
afanofosc wrote:So is the API function in NXC working or not?...
Well, it seems like "not" to me.

Re: Use of NXC function bcd2dec

Posted: 28 Feb 2011, 16:56
by afanofosc
Could you post the NXC and NBC code for a simple program that always produces 0 when you call bcd2dec?

John Hansen