# AVR Calculator

Simple decimal calculator using the AVR Stk500 and a testing board for input/output using LCD and numeric keyboard.

## description

This was another project for an electronic course in Vitus Bering University (Denmark). We wanted to do a simple calculator using the AVR Stk500 and a testing board that has built an LCD and Keyboard controller.

The features of the calculator are:
• 5 Digit
• Basic arithmetic operations: addition, subtraction, multiplication, division.
• Save and load a number from memory.
• Clear everything (CE) or just the current number (C).
• Power button to switch on/off the calculator.

### building the connections

The first step we needed to do was to build the cable to connect the AVR and the testing board. As you can see in the specification papers the keyboard needs 8 connections (7 data lines and 1 GND). The LCD needs 11, but as we won't support brightness control with 10 will be ok. (9 for data and 1 GND). As the AVR ports support just 8 data lines, it was necessary to split the LCD lines into two different ports.

Here you can see the final connections reusing one free line from the keyboard port to connect one from of the LCD.

### jumpers in the testing board

• Display:
• JP1: Not connected because we don't want to have brightness control. We just use the default brightness for each led.
• JP2: Connected pins 1 and 2 to allow multiplexing display, otherwise we will show just one digit instead of five.
• Keyboard:
• JP3: Connected pins 2 and 3 to allow scanned reading. In that mode we can scan throw all the rows of keys, in the other mode we just scan the first row.

### workflow

First of all we initialize the hardware setting up all the bits according with the write and read necessities for each port's pin and setup the default output values for each port (For example not show nothing in the display). After that we initialize the calculator variables that we're going to use internally to compute the calculator's operations. Also all the temporary variables such as memory, or last operation or last number. Once we did it we go to the main loop in our program:

• We check if the power button was pressed so the variable power is set to On, if not we wait until we press.
• If power is On we check if there's some key pressed.
• If there's some key pressed we get the ID of that key and we send to be processed by the calculator's main function.
• After that we get from the calculator program the number that we need to show and we send to the display function.

### modules diagram

The program is divided into blocks independent from each others:
• Hardware: Implement all the related to the communication with the hardware in our project. Including:
• Display: Functions to manage the 5 digits diplay.
• Keyboards: Functions to manage the keyboard inputs
• Calculator: The logic in our decimal calculator (Operations, number conversions, etc…)
• Utils: Some util functions such us sleep to use in our program.
• Main: Our main program where is the main workflow detailed before.

### implementation

#### setting up the hardware

In the call to HardwareInit we need to setup all the bits for start the "dialogue" with our hardware. As we saw in the connection diagrams we're going to use PortB for display, so we setup that port as use just for output:
`DDRB = 0xff;`
Then we'll use PortC for Keyboard but we use also the pin6 for display. So we need to put that bit up and also the bits corresponding to row select. These bits (row select) must be set up because we must send which row we want to select to make the scanning to know if was some key pressed in that row.
`DDRC=(1<<PC6)|(1<<PC1)|(1<<PC3)|(1<<PC5);`

#### key pressed

Key pressed To know if the user has pressed one key we need to do a scanning throw the keyboard. For that we make a loop along all the rows that we have. We set that row to be the input row (With that we read the information just about that row). For select that row we just need to set the according bits in the Row Select ABC bits. After we look around all the columns if some of these bits are set to up.
```int ReadKeys()
{
int nRow,nCol;
for (nRow=0;nRow<KEYB_NUM_ROWS;nRow++)
{
for (nCol=0;nCol<KEYB_NUM_COLUMNS;nCol++)
{
if (IsColumnPressed(nCol))
return nRow*10+nCol;
}
}
return -1;
}```
The code that returns is a two digits identificator for that key. The first digit represent the row and the second the col. Following we have the table of contants for each key value:

#### show number

The display supports to show one digit in one position at time. It means that is impossible to send one command or set the bits to show more than one in the same time. So what we make is to make a loop throw all the digits that our number has and display in the corresponding position, make a small sleep, and show the next one. With that we make a fast refresh of all the digits so the human's eye can't notice the change and you'll see like is just one number with different digits.
```void ShowNumber(int nNumber)
{
int nNumDigits,i;
nNumDigits=GetNumDigits(nNumber);
for (i=0;i<nNumDigits;i++)
{
ShowDigitInPosition(nNumber%10, i);
nNumber/=10;
Delay(1);
}
}```

#### insert new digit in the current number

To insert a new digit to the current number that we're typing in the display we displace one position left the and add the new number. If the number of digits of the current number if five (the maximum allowed) we just change the last digit of the current number instead of add (Otherwise it will give overflow).
```void InsertNewDigit(int key)
{
if (GetNumDigits(nCurrentNumber)==5)
nCurrentNumber=10*(nCurrentNumber/10)+GetNumberFromKey(key);
else
nCurrentNumber=10*nCurrentNumber+GetNumberFromKey(key);
nShowNumber=nCurrentNumber;
}```

#### processing key

When we read a key we send the key ID to the function ProcessKey inside it we make a loop waiting for reading while the ID of the read key is the same that we're going to process:
`while (key==ReadKeys()) {}`
We need to make it because if not it will enter several times in the function if we leave the key pressed.

#### calculate result of the operation

In the calculator we have some global variables:
• nLastAcum: Last accumulator value (Resulting from previous operations).
• nLastOp: Last operation entered by keyboard.
• nShowNumber: Number to show in the screen.
• nCurrentNumber: Current number being typing in the keyboard.
Everytime that we press an operation key or the result key we compute the result with the following function:
```void CalculateResult(int key)
{
nLastAcum=ComputeOperation(nLastAcum,nLastOp,nCurrentNumber);
nLastOp=key;
nShowNumber=nLastAcum;
nCurrentNumber=0;
}```

## images

Complete source code
Testing board diagram (Keyboard & LCD)

#1 posted by julius at 2009-05-16 21:39:14

please send an article to my email including making a calculator with avr microcontroller and program it with C

#2 posted by sayeh at 2009-10-29 01:49:07

please send an article to my email including making a calculator with avr microcontroller and program it with C

#3 posted by RainMan at 2010-05-15 23:50:20

first of all,thanks for the great project,and then i need to know if this calculator calks phrases containing decimal numbers or not!

#4 posted by pavi at 2011-02-05 05:19:46

pls send me the code and designing options with AVR 8515 kit..as soon as possible...

Message posting is currently unavailable :(
Feel free to contact me using mail or contact form