Extracting decimal values from binary

edmunds

Senior Member
Hi all,

Mathematical question. I have a PCA5554A which is read by PICAXE 40X2 through I2C. There are two 10-position BCD switches connected to 4 pins of PCA5554A each. So setting the switches to something gives me a binary value of, say %00111000. This is actually 0011 or decimal 3 for the first switch and 1000 or decimal 8 for the second one. Is there a neat way to extract this without crazy length of select case or if statements? bcdtoascii or bintoascii commands seem not to achieve this.


Regards,

Edmunds
 

hippy

Technical Support
Staff member
For an X2 ...

b0 = %00111000
b1 = BcdToBin b0
SerTxd( "b1=", #b1 )

There's a bug in simulation in PE6 which we will be fixing in due course so that won't give the correct result if simulated but it should be correct with a real PICAXE chip
 
Last edited:

AllyCat

Senior Member
Hi,

Note that BCD is not the same as the "binary" in your title! To convert binary, you simply take the remainders when repeatedly dividing by 10.

If the two switches are BCD then the "high" nybble is simply the "tens" digit, so you can divide the byte by 16. The low nybble is the "units" digit directly, so you can use the AND function, or take the remainder when dividing by 16. Thus use:
Code:
bcdbyte = %0011 1000
tens = bcdbyte / 16
units = bcdbyte AND 15    ; Note 15 = %0000 1111
;     OR use:
units = bcdbyte  // 16      ; Remainder operator
decimalvalue = tens * 10 + units
Or hippy did it in a "one-liner" in this code snippet

Cheers, Alan.
 
Last edited:

edmunds

Senior Member
There's a bug in simulation in PE6 which we will be fixing in due course so that won't give the correct result if simulated but it should be correct with a real PICAXE chip
Thank you. I'm on Mac, so no problem with that :)


Regards,

Edmunds
 

edmunds

Senior Member
Thank you, guys for your assistance. My old code of:

Code:
  bcdtoascii b0, CustomAddress1, CustomAddress2               'tens into CA1, units into CA2
  CustomAddress1 = CustomAddress1 - $30                         'clean up hexadecimal
  CustomAddress2 = CustomAddress2 - $30                         'clean up hexadecimal
started working again after I realised I had to use this to invert the inputs of PCA9554A:

Code:
  Symbol PCA9554A_1 = %01111000    'Lane 1 DIP switch inputs
  ...
  Symbol PCA9554A_5 = %01111110    '10x2 rotary DIP switch inputs (2x8421)
  Symbol AllInputs = %11111111         'All port I/O pins set as inputs or All inputs inverted
  Symbol PolarInvReg = 2
  Symbol InputReg = 0
  ...
  hi2csetup i2cmaster, PCA9554A_1, I2CSpeed, i2cbyte      'Announce there will be I2C
  ...
  hi2cout PolarInvReg, (AllInputs)                                      'Invert all inputs to read correct 1248 BCD switch values
  hi2cin InputReg, (b0)
Regards,

Edmunds
 
Last edited:

westaust55

Moderator
Code:
  bcdtoascii b0, CustomAddress1, CustomAddress2               'tens into CA1, units into CA2
  CustomAddress1 = CustomAddress1 - $30                         'clean up hexadecimal
  CustomAddress2 = CustomAddress2 - $30                         'clean up hexadecimal
From your code example it appears that you in fact want to extract the val;ues as two separate digits, one for each BCD switch.

If that is the case, then:
Code:
  CustomAddress1 = b0 / 16                       'extract value 1 from BCD switch on upper nybble (=4 bits)
  CustomAddress2 = b0 AND $0F                'extract value 2 from BCD switch on lower nybble
 
Top