Menu

Binary, Hexadecimal and Decimal Calculator

mkstevo
2019-01-27
2022-04-17
  • mkstevo

    mkstevo - 2019-01-27

    For some time I've wanted a calculator that could convert between Decimal, Binary and Hexadecimal notations. I have bought a few but they are often limited in the number of digits available. I thought I might be able to do better myself, so had a go.

    The project uses an LCD display which shows values in the three bases, all at the same time. The top line of the display shows the binary value, the bottom line shows hexadecimal and decimal values. Numeric entries can be made in any of the three bases and switched between them at any point. Simple maths (+, -, x, / ) can be performed on the values entered, with the result shown in all three bases simultaneously. Simple logical operations (AND, NOT, OR, XOR) can be also be performed.

    The circuit consists of little more than a 16F1829, a two line 16 digit LCD display, 24 buttons and a 74HC165. The buttons are arranged into three sections, with the 74HC165 inputs connected to three 'columns' of eight 'rows' of buttons. The 'rows' are selected by the processor outputs in turn and then the values returned by the 74HC165 decoded to determine which button was pressed.

    The software can be switched from the default 16bit mode, into 32bit mode at the loss of the binary input/result. In 16bit mode the calculator has a maximum value of 65,535 (decimal) and in 32bit mode, 4,294,967,295 (decimal). All calculations are carried out using standard PIC/GCB maths meaning that any calculations made on the calculator should be exactly as expected when performed within a GCB program. The calculator (as with the PIC devices) performs either WORD or LONG variable maths, where results that exceed the limits of a WORD or LONG variable value simply overflow silently back to zero and beyond. I have limited only the division calculation so that it is not possible to divide a small value with a larger one, attempting to divide 5 by 10 will return 0.

    I have uploaded the source code, schematic diagram (as a PDF file), the Gerber files for building the PCB as shown in the picture and an operation / build manual.

    I've also added the missing header file that contains some of the constants and directives. As pointed out, the source won't compile without it...

    Updated 10th March 2019.
    I have added a Modulo function to the calculator. Long pressing the divide key will activate this, with the display showing a '%' symbol instead of the '/'.
    Entering 98 % 10 = will return the answer of 8.

    New Source code, header file and manual added.

     
    👍
    1

    Last edit: mkstevo 2019-03-15
  • mkstevo

    mkstevo - 2019-01-27

    If anyone would like to build one, I have a few PCBs that I would be happy to pass on, at cost + P&P.

     
  • Chris Roper

    Chris Roper - 2019-01-27

    Very Nice Project and very useful.

    Thanks for sharing it.

    And now you have my creative juices flowing again, I feel a Programmable RPN GCB-Calc trying to break free :>)

     

    Last edit: Chris Roper 2019-01-27
    • Anobium

      Anobium - 2019-01-27

      Well cool!!!

      The code looks very good - the layout is great!

       

      Last edit: Anobium 2019-01-27
  • Moto Geek

    Moto Geek - 2019-01-27

    Very impressive indeed! Nice job on the board and the code is very interesting!

     
  • bed

    bed - 2019-01-29

    ..and beside the work for PCB and Code, the Manual is really great!

     
  • mkstevo

    mkstevo - 2019-01-29

    Many thanks for all the kind comments. Most of the things I use GCB for are for work, and I'm not able to share them - though they aren't particularly interesting, or useful outside of the business area we deal in.

    The calculator I built for myself (though I also use it at work) and so I'm free to share. As soon as I find myself with a spare hour or two, I'll upload some other 'stuff'.

     
  • mkstevo

    mkstevo - 2019-04-14

    Quick update. This is version 3.16.

    A 'ScreenSaver' has been added, this shows an animated character which travels along the screen initially gobbling up the numbers on the display then leaves a trail of pills which are in turn gobbled up again.

    Fairly useless, but amusing never the less.

    The timing for the ScreenSaver uses the millis() function kindly provided by Chris Roper. A version of this is included with the source code for completeness, this should not replace any existing versions that might be present. Only if the compilation of the program fails due to not finding the millis() function should the included millis() file be unzipped and used.

    All required files are incorporated into the one .zip file. The instructions and PCB files remain as in the first post.

     
  • mkstevo

    mkstevo - 2019-04-14

    Quick update. This is version 3.16.

    A 'ScreenSaver' has been added, this shows an animated character which travels along the screen initially gobbling up the numbers on the display then leaves a trail of pills which are in turn gobbled up again.

    Fairly useless, but amusing never the less.

    The timing for the ScreenSaver uses the millis() function kindly provided by Chris Roper. If this is not present in your Great Cow Basic installation it can be downloaded from here: millis.h

    All other required files are incorporated into the one .zip file. The instructions and PCB files remain as in the first post.

     

    Last edit: mkstevo 2019-04-14
  • mkstevo

    mkstevo - 2019-04-27

    Further update. This is version 3.19.

    A shift left/shift right function has been added.

    This is performed by pressing and holding the multiplication key, continue pressing the multiplication key and the display will rotate between '<' and '>' . Releasing the multiplication key when the appropriate symbol is displayed performs either a shift left (indicated by '<') or a shift right (indicated by '>').

    This performs a 'Rotate left simple' operation on any value currently being displayed.

    There was an error in all previous code version 3.xx files which prevented the Not and Random functions from working correctly (or not at all). This has hopefully been fixed too.

    The required source code files are added to this post. I have also updated the help file. The PCB files remain as in the first post.

    As the latest version of GCB now includes the millis() function required by the screensaver timing, this should be present on your system, if not, see the above link.

    Update: 29th April.
    Tiny improvement to the display of the 'Shift' character when the result of a previous calculation is displayed.

     

    Last edit: mkstevo 2019-04-29
  • mkstevo

    mkstevo - 2019-06-26

    Further update. This is version 3.20.

    'Continuous calculations' can be performed without the need for the equals key to be pressed between operations.

    Keys pressed:
    1 + 2 + 4 - 5 = 2

    Performed by the calculator as:
    1 + 2 = 3
    3 + 4 = 7
    7 - 5 = 2

    These operations are performed strictly in the order of entry, as though the equals key was pressed in between each operation. I was trying to do a multiple addition on a number of items bought from an online retailer and this failed due to the calculator clearing previous results, I couldn't think why I wrote it that way so decided to change it!

     

    Last edit: mkstevo 2019-06-26
  • mkstevo

    mkstevo - 2022-04-16

    Updated again. Version 4.02.

    The calculator now does all maths on 32bit (unsigned) long integers. Originally, in 16bit mode all calculations were done on word variables, in 32bit mode all calculations were done on long variables.

    The 'old' method of switching between 32bit and 16bit modes has been removed.

    The calculator now has the capacity to display values in 8, 16, 24 and 32 bits. The display mode is changed by long pressing the "C", "D", "E" or "F" keys.

    A long press on the "C" key selects 8bit (octal or byte), "D" 16bit (Word), "E" 24bit (SWord) and "F" 32bit (Long).

    The display will indicate the modes by displaying a letter in the centre of the bottom half of the LCD display. The letters are: "o" (Octal or 8bit {byte} mode) "w" (Word, 16bit) "s" (SWord, 24bit) "l" (Long 32bit).

    The last used display mode should be stored in EeProm and selected when the calculator is next powered on.

    Where relevant, parts of the "values" display will be blanked - for example, in 8bit mode, only three characters are shown for the decimal value, two for hex value and eight for the binary.

    On changing the mode, all previous calculation values are cleared and set to zero.

    As values are entered or calculated, when the display needs to show values that exceed the digits which can be shown in the space allocated for the binary, hex and decimal values the display will alternate between showing the most significant values and the least significant ones. To indicate whether the MSB or LSB value is being displayed the "h" (for Hex) and the "d" for decimal are shown in uppercase when the MSB is being displayed and in lowercase when the LSB is being indicated.

    The decimal value has a maximum display capacity of five digits, equal to a maximum of 99,999 It is possible that when showing the MSB for a binary value and a hex value, the decimal value will not change if it can be displayed within the five digits available, although the "D" will be displayed in uppercase. Decimal values greater than 65,5535 and less than 99,999 will be shown in this way, while the binary and hex values would need to be shown as separate MSB and LSB values.

     
  • mkstevo

    mkstevo - 2022-04-16

    I actually use the calculator rather a lot. I find it useful for checking what results I can expect when performing calculations on bytes, words and longs.

    To try and illustrate the convoluted explanation in my earlier post, here is a screenshot of the calculator showing the hex value of:
    FFFF0FFF
    Binary:
    11111111111111110000111111111111
    Decimal:
    4294905855

    (assuming I've pressed the keys correctly...)

    One picture shows the MSB values of:
    1111111111111111
    HFFFF hl= D42949

    The other picture shows the LSB values of:
    0000111111111111
    h0FFF hl= d05855

    As said, the capital "H" and "D" before the hex and decimal values indicate that the display for the binary, hex and decimal values are the MSB, a lowercase "h" and "d" before the hex and decimal values indicate that the values are the LSB.

    The "hl=" in the middle of the display show that the entry mode for values was in hex ("h"), the calculator was in 32bit (Long) mode ("l") and the last key pressed was the equals button ("=").

     
  • mkstevo

    mkstevo - 2022-04-17

    Tiny change in the way that the view is changed when switching between the 8, 16, 24 and 32bit display modes.

    Originally I thought that as all calculations were done using 32bit (long) wide variables, values that were outside the range of the current view might appear to change the result displayed when switching display modes. To avoid this, when the display mode was changed, all values were cleared and set to zero.

    I've since changed my mind on this point so when changing display modes, values are no longer cleared, they remain unchanged.

    Calculations performed in smaller display modes still overflow into the upper bytes of the calculator's internal memory and can now be hidden or made visible by selecting different display modes without affecting the overall value of the calculations, only affecting what is shown on the display.

     

Log in to post a comment.