MicroBit DecHexBin Converter

by samsungite in Circuits > Microsoft

1172 Views, 0 Favorites, 0 Comments

MicroBit DecHexBin Converter

20210918_214112.jpg
dhb

This is my first play with the BBC Microbit, and I've got to say, it's a tad impressive!

It is a great piece of kit for anyone who wants to learn about programming.

I programmed it to be a Decimal or Hexadecimal to Binary converter.

Knowing binary notation is a must for understanding registers and I/O ports!

With this program, a decimal or hexadecimal number is entered and the binary notaion is displayed on the led array matrix.

A '1' is displayed as a bright led & a '0' as a dim led. The dim leds are a little awkward to see on the video.

Converting from decimal to binary is just a matter of dividing a number by 2 and using the remainder -

  1. Divide the number by 2.
  2. Get the integer quotient for the next iteration.
  3. Get the remainder for the binary digit.
  4. Repeat the steps until the quotient is equal to 0.

See this link for an excellent explanation

https://www.rapidtables.com/convert/number/decimal...

Converting from decimal to hexadecimal is just a matter of dividing a number by 16 and using the remainder -

  1. Divide the number by 16.
  2. Get the integer quotient for the next iteration.
  3. Get the remainder for the hex digit.
  4. Repeat the steps until the quotient is equal to 0.

an excellent explanation can be found here.

https://www.rapidtables.com/convert/number/decimal...

Program Operation

It is possible to enter either a decimal number or a hexadecimal number for conversion.

On power up, decimal input is the default.

Because we have 8 bits to play with, we have 256 possible combinations, 0 to 255.

The number is entered in 3 parts, Hundreds, then Tens, then Units.

So, on power up an 'H' is displayed, indicating the microbit is awaiting a number for the hundreds which can be 0, 1 or 2 (as we can only display up to 255). If the count exceeds 2, it rolls back to 0.

Pressing button A increments the hundreds counter. The number is selected by pressing button B & the letter 'T' is displayed indicating the microbit is waiting for the Tens input. When the desired number is reached, pressing button B will select the 10s and display the letter 'U' indicating it's waiting for the Units input.

Pressing button B selects the Units and the binary representation is displayed on the led matrix.

If the Hundreds value is equal to 2, both Tens and Units input are limited to 5.

Pressing buttons A & B together shows the number entered and its equivalent Hexadecimal value then it returns to the binary matrix display.

Button A can now be used to increment the existing value & button B can be used to decrement the value with the result shown on the led matrix. As before, buttons A & B pressed together will show the current decimal and hex values.

Use reset to eneter a new value.

If button B is pressed during a reset, the microbit will change to Hex input mode.

The letter 'M' indicates the microbit is waiting for the Most significant nibble. Press button A until the desired value is reached (0 through to F) and press B to select it. Now the letter 'L' is displayed indicating the microbit is waiting for the Least significant nibble (0 through to F). Press button B to select the value.

The display and button operations are identical to the decimal input.

The Python Code

Note: Some variables I used were mainly for debug purposes.

Initially the code checks to see if button B is pressed and sets variable decip (decimal input) to True or False and display either an 'H' or 'M'.

In Decimal input mode

Button A increments variable hh & button B sets variable hhs to True (hhs indicates hundreds (hh) have been set).

This is repeated for the Tens (tt), (tts) & the Units (uu), (uus).

When all have been entered, variable 'allset' is set to True and the numbers added as follows :-

decin=hh*100 + tt*10 + uu

Two functions, conv & convx are used to convert decin to Hex & Binary numbers and store the values in an array (list).

The binary display is split into 2 rows of 4. The bottom line for the Least significant nibble & the top for the Most significant nibble.

Program lines 158 - 163 sets the display to all 0's (dim leds), then lines 166 - 172 sets the 1's (bright leds) according to the values in the array (list)

Variable 'allset' is used to change the function of buttons A & B to increment & decrement decin.

The Hexadecimal input mode of operation is identical to the decimal input with the exception of 'HTU' being changed to 'ML' and the input changed from decimal to a 2 digit Hexadecimal number.