Calcuino One

An idea for playing with Arduino.

It's an open secret that I'm planning an electronics book that will combine examples of electronic designs with the Arduino and an explanation of electronics principles and some concepts (like „what is SPI and how do I connect it to my computer?“) We'll go from blinking an LED to wiring sensors and connecting the whole bazmek to the Internet to simple computers. And what is the stage between the flasher and the computer? Well, a calculator!

Today there are two main ways of building a calculator…

Or else: There are two ways to come to a calculator. You can use the one you have in your computer, in your cell phone, in your fridge, wherever, or you buy one of the 572 models they have in stores, starting with 9-11. Or, the second way: you build one.

Why build expensively something that I can buy, or even get as a promotional gift, and it will look better than the one I built at home? A question as old as knowledge itself. And the answer is just as old: either you enjoy creating something and learning new things in the process, or you don't. (Some people you can tell: „And why are you re-writing e-shop, quicksort, banking system…?)

So the decision is: build it. Today, there are two main ways to build a calculator. One is really retro, where you use old calculator circuits (MHB7001, MC14007), the other is modernist. I like the latter way too. In short: If I can put it together from ready-made modules from eBay for a dollar to five (incl. shipping), I'll put it together from modules!

This is how I found a module for a single-board remake. It's got eight 8-segments and sixteen buttons, and it connects via SPI, so there are three wires (+2 for power). I ordered, it arrived, including the cables, and I wondered how to connect it. Of course, the easiest thing would be to use cables where there are holes on one side (for the module) and pins on the other (to tuck into the Arduino), but in the end, I was looking at the UNO that way and my eyes kept circling around that ICSP pin connector. So I took a closer look – and what do you think?

There is +5V and ground, there are three data pins (11, 12 and 13) and RESET. If you're not currently programming a MCU, they're free and hardwired to those data outputs. So if I stick it in there…

The second step to the solution was that TM1638 circuit. I do have a datasheet that is understandable as - well, as a datasheet for a Chinese circuit, but I'd better see if anyone has a library. Fortunately, he did: TM1638-library. There was even a modification in current version (2.2.0) for the module I have, since the module has seven-segment diodes with a common anode.

I connected everything, took the library demo (tm1638qyf), changed the pin settings, ran – and things happened! It lit up, it flashed, it ran furious messages across the display, it didn't make logical sense, until I was scared that it was broken, and it wasn't, that's how the demo was programmed!

So I sat down and made my own demo that displayed numbers. It worked great, but it had one wrinkle: It displayed numbers with either leading zeros or trailing zeros. So I made a minor library modification – I added a function to display numbers with right alignment, as calculators are wont to do.

I also tested the keyboard. Working with it is simple: the module returns a hexadecimal number, where every bit is a key. The lowest bit corresponds to button 1, which is „top left&#8220 …

The calculator needs 10 buttons for the digits. I'm down to six. One button takes „=“, leaving five. So I have just enough for four basic math operations, and the last one will be „C“ – that is, the button to delete an error.

I mapped them like this:

calcuino1

(„Calcuino? WTF?“ Well, it may have been an Ardulator too!)

The first attempt showed me that the circuit should do a debouncing, but somehow it didn't feel like it, so I treated them myself. And then it was step by step.

I added a simple function: when I press the number button, it appears on the display and is added to the number I've already typed. If you don't believe it, try it: I have 123 on the display, I press 4, the result should be 1234, and that's the original 123 * 10 + (pressed) 4! In the same function I will add a limitation of the number of digits (to seven, leaving the eighth position free for the minus).

The C button simply resets the working register (and 0 appears on the display).

This took me about five minutes, and then I tried to implement the first operation, namely plus.

It's not quite as trivial as it might look at first glance. Remember that calculators work with infix notation, meaning that 2+3 is entered as a series of presses of the 2, +, 3 and = buttons. So the algorithm goes like this:

  1. If the user has pressed a digit, add it as the lowest order to what is on the display.
  2. If the user pressed C, set the display to 0.
  3. If the user has pressed an operation, make a note of it. Before doing the previous stored operation, store the intermediate result in the X register and display it. At the same time, the next time a digit is pressed, first reset the display.
  4. If the user has pressed „=“, perform the last stored operation, save the result in the X register and display it.

Just a note on this: the calculator doesn't take into account (in this version) the priority of operators, I'll deal with that next time (and you can do your homework).

So the calculator needs several working registers: first the X register to store the intermediate result, then the „op“ register to store the selected operation, and finally the Z register, where the display contents are.

From the description, it might look like this, that when you press a button with an operation, the previous one is actually executed, and if you think about it, it really is. The current one is stored „for next time“ and the previous one is executed. The „=“ button is basically the same as any other operation, except that „for next time“ the „do nothing, just copy the display to the intermediate result&#8220 operation is stored;

So, do you dare to give it to yourself?

IMG_20150822_102252
IMG_20150822_102301
IMG_20150822_102335

Suggestions for next time: modify the calculator to handle operator precedence, modify it to allow negative numbers, modify it to RPN… I have all these modifications ready and will add them gradually. But I don't want to deprive you of the joy of your own experiments.

Comments powered by Talkyard.

Martin Maly

Martin Maly

Programmer, journalist, writer and electronic hobbyist. Vintage CPU lover. Creating new computers with the spirit of 80's.
Czechia