Monday, April 8, 2013

New Project: RPN Scientific Calculator

My interest in electronics started because I would like to build calculators like the ones I used to collect. Now I plan to build a scientific RPN calculator. A three inch graphical LCD would work well for this but most cell phone screens I can easily find are a little bit too small. For the time being I will continue using the 16x2 LCD I found.

For this project I intend to use an MSP430. The challenge of this will be that the MSP430G2553s I have only have 512 bytes of RAM. To begin with I decided to start on the programming since this will probably be much more work than the hardware. Although I want this calculator to use RPN, it would also be good if it could evaluate algebraically as well like the HP-49G. It will hopefully also be useful to have a small parser that works on a microcontroller for some other projects I want to do. Here is the parser running on my LaunchPad and evaluating the equation 13*(4+25*13)/((67-33)*2^3):

At first I wanted to use recursion because this can really simplify parsing but that's not really an option since it would use a lot of stack space. Instead, it evaluates any expressions it can and stores the result in memory then writes a pointer to that memory in the place where the expression was. Doing it like this instead of tokenizing the whole string before evaluating saves memory. The results of operations are stored starting at the end of the string that holds the expression so that as the expression is evaluated and becomes smaller, memory is freed which can be used to store more results. So far this parser fits in about 2k of Flash.

The second part I worked on is a Binary Coded Decimal format. This will be more accurate than floating point since it will be able to store any number that will fit in available memory. Each byte of data contains two numbers from 0 to 9. Now numbers are limited to 512 digits in length but this can easily be increased, although it probably wouldn't be useful on such a small screen. I have code for adding and subtracting, so multiplication and division should be easy to implement using these. I was able to fit this into less than 2k of  Flash so with the parser I should still have about 12k of flash for other functions. Here is an example using numbers small enough to fit on the screen:

Saturday, April 6, 2013

16x2 LCD and MSP430

Recently, I found a local electronics shop that carries 16x2 LCDs that are HD44780 compatible. This type of LCD is really common and it wasn't too hard to get it working with an MSP430.

The hard part was that the LCD expects 5 volts, although the LaunchPad supplies only 3.6 volts. I did some reading and it turns out that the logic part of the LCD can run on as little as 2.7 volts. The contrast, on the other hand, expects 5 volts. Even with the pin that controls contrast connected to ground, nothing can be seen on the LCD. One solution is to feed this pin some negative voltage.

Negative voltage can be generated with a charge pump made of only two capacitors and two diodes. In the picture above you can see them behind the LCD. They need a pulse to work which I generated with my MSP430. This worked well but I began to think that it might be a bad idea to charge capacitors through a microcontroller pin. My first try was to switch the voltage with a transistor. This didn't work because I could only source current. Then I found out about push-pull stages. This is a handy way to sink and source current with only two transistors.

My next plan was to create the pulses for the charge pump externally so the MSP430 wouldn't have to worry about doing it. The 555 timer I bought for the job turned out to be 5 volts also so that didn't help. I tried modifying a circuit I found to flash LEDs using capacitors and transistors but it wasn't very reliable. For now I will stick with doing it with the MSP430.