Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4746

Need help converting a 4 bit binary number to 4 separate values

$
0
0
Hey group,
I am trying to build a Decimal / Hex / Binary number demonstrator to teach electronics and microcontrollers to a bunch of Scouts (the Electronics merit badge is what I am teaching)

I am stuck on the following problem... (and running out of time before the upcoming class)

If I have a number x (from 0 to 15)
I need to set variables d,c,b,a to the binary representation of the 4 digit positions of x.

so if x = 8 which is %1000 in binary
then...
a should =0
b should =0
c should =0
d should =1

so if x = 9 which is %1000 in binary
then...
a should =1
b should =0
c should =0
d should =1

I have tried (with no success) many different methods including arraywrite and bin string modifiers but they are all failing.

I have tried a 4 byte array
I have tried trying to select each digit in the 4 bit binary number

I know there is a way I just can't get to it (yet)

My final purpose is to write each of the 4 digit positions to a I2C 4-digit 7-segment display, I need to be able to write each digit to its position 0-3 separately.

Any Ideas or guidance would be greatly appreciated.

My code is just a mess right now
here is a NON WORKING partial
Code:

        i2cwrite sda,scl,$e0,0,[bin1 x]
        i2cwrite sda,scl,$e0,1,[b]
        i2cwrite sda,scl,$e0,2,[c]
        i2cwrite sda,scl,$e0,3,[d]

How do I select a given digit from within a 4 bit number and send just that digit to the I2C display??
I guess I may have to resort to some sort of look up table for each of the 4 digit positions.

thanks

Viewing all articles
Browse latest Browse all 4746

Trending Articles