PDA

View Full Version : Need help converting a 4 bit binary number to 4 separate values



Heckler
- 22nd June 2015, 03:21
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

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

Heckler
- 22nd June 2015, 04:46
This is working, not very elegant but, hey, its working.

Now to figure out how to do it for 8 bits by dividing the "Counter" down to get the second upper 4 bits.


'================================================= ========================
'======= SubRoutine to write to the 7 Seg Binary display ==========
'================================================= ========================
Binary:
for z=0 to 3 'character position
lookup z,[0,2,6,8],chrpos ' value used to tell the I2C display which digit
SELECT CASE counter
case 0 :LOOKUP z, [$3f,$3f,$3f,$3f], chrdsp '0000
Case 1 :LOOKUP z, [$3f,$3f,$3f,$06], chrdsp '0001
Case 2 :LOOKUP z, [$3f,$3f,$06,$3f], chrdsp '0010
Case 3 :LOOKUP z, [$3f,$3f,$06,$06], chrdsp '0011
Case 4 :LOOKUP z, [$3f,$06,$3f,$3f], chrdsp '0100
Case 5 :LOOKUP z, [$3f,$06,$3f,$06], chrdsp '0101
Case 6 :LOOKUP z, [$3f,$06,$06,$3f], chrdsp '0110
Case 7 :LOOKUP z, [$3f,$06,$06,$06], chrdsp '0111
Case 8 :LOOKUP z, [$06,$3f,$3f,$3f], chrdsp '1000
Case 9 :LOOKUP z, [$06,$3f,$3f,$06], chrdsp '1001
Case 10 :LOOKUP z, [$06,$3f,$06,$3f], chrdsp '1010
Case 11 :LOOKUP z, [$06,$3f,$06,$06], chrdsp '1011
Case 12 :LOOKUP z, [$06,$06,$3f,$3f], chrdsp '1100
Case 13 :LOOKUP z, [$06,$06,$3f,$06], chrdsp '1101
Case 14 :LOOKUP z, [$06,$06,$06,$3f], chrdsp '1110
Case 15 :LOOKUP z, [$06,$06,$06,$06], chrdsp '1111
end select
I2cwrite sda,scl,bin00,chrpos,[chrdsp]
next z
return

The $3F represents a "0" and $06 is for a "1"

What I am using is this 7-Segment 4-digit display from Adafruit.
https://www.adafruit.com/product/880

They have some sample code but it's for an Arduino.

"No I will not give up my PICBasic!!!"

HenrikOlsson
- 22nd June 2015, 06:21
Hi,
Here's a couple of ideas:

Value VAR BYTE
A VAR Value.0
B VAR Value.1
C VAR Value.2
D VAR Value.3

A,B,C,D will then be treated as BIT variables which may or may not work for your intended purposes. If it doesn't then perhaps:

Value VAR BYTE
A VAR BYTE
B VAR BYTE
C VAR BYTE
D VAR BYTE

Convert:
A = (Value & %00000001) ' A will be either 0 or 1
B = (Value & %00000010) >> 1
C = (Value & %00000100) >> 2
D = (Value & %00001000) >> 3
RETURN
If you're writing to a display and what it's actually looking for is the ASCII representation of the numerical digit '1' (instead of the binary value 1) then just add 48 to the value:

Convert:
A = (Value & %00000001) + 48 ' A will be either 48 ('0') or 49 ('1')
B = ((Value & %00000010) >> 1) + 48
C = ((Value & %00000100) >> 2) + 48
D = ((Value & %00001000) >> 3) + 48
RETURN

/Henrik.

Heckler
- 22nd June 2015, 16:15
Thanks Henrik,

I'll give those a try.

When I get this working it will have...

8 digit 7-segment display of 8 bit binary number in the form of 1's and 0's
8 individual LED's to represent an 8 bit binary number in the form of High and LOW (on/off)
2 digit alphanumeric display to represent Hex number
4x20 LCD using BIG digits to display Decimal number



All controlled by a 16F690.

Will count up/down from 0-255
Each of the above listed display formats will show the same number in a different way.
A POT will increase/decrease/stop counting speed
2 buttons to change counting direction or single count when pot is turned all the way to stop.

My intention is to have a way to show the same number in DEC / HEX / BIN so that students can see the relationship between the different number formats.
Oh Yeah, and to have something sort of Rube Goldberg-ish and kind of geeky to spark interest in electronics and programming.

I'll try and post the code and a video when I get it finished.

thanks