<<

Implementation and Design of

Submitted To: Sir. Muhammad Abdullah

Submitted By: Muhammad Huzaifa Bashir 2013-EE-125

Department of Electrical University of Engineering and Technology, Lahore

Contents

Abstract ...... 2 Introduction ...... 3 Input Unit: ...... 3 Processing Unit: ...... 3 Output Unit: ...... 3 Interfacing and Implementation ...... 4 LCD: ...... 4 Hardware Interfacing: ...... 4 Problem and Solution of hardware interfacing: ...... 4 Software Interfacing: ...... 5 Problem and Solution for Software Interfacing: ...... 5 Keypad Interfacing: ...... 6 Hardware interfacing: ...... 6 Problem and Solution for Hardware Interfacing:...... 6 Software Interfacing: ...... 6 Data Transmission from Keypad to LCD: ...... 7 Problem and Solution:...... 7 Data Assembling: ...... 8 Data Retrieving and Processing: ...... 8 Result Display on LCD: ...... 8 Arithmetic Calculator: ...... 8 Scientific Calculator: ...... 8 Trigonometric Calculator: ...... 9 Limitation:...... 9 Future Aspects ...... 10 Incorporation of Floating Data: ...... 10 Toggle Input: ...... 10 Graph Plotter: ...... 10 Bibliography ...... 11

1

Abstract

The report describes the design and implementation of software and hardware of a simple Calculator. The basic purpose of this project is to enhance our learning about the use of through practical demonstration. The basic task of this calculator, as described in proposal, is to get inputs from user through a 4x4 keypad, process that data for arithmetic operations through Tiva TM4C123G Microcontroller and show the input data and results on an LCD. However we had to deviate from interrupt based data acquisition from keypad because we learnt that it would not be a good technique. After successfully completing the proposed task, two other forms of have also been introduced. They are Scientific and Trigonometric Calculators. The report focuses on the successful interference of LCD and Keypad with the microcontroller. Once the interference is done, remaining part is not much difficult. The report also describes the proper coding to implement a simple calculator. Problems faced while making this project have been discussed. Finally the future aspects of this project are also mentioned.

2

Introduction

We start with absolutely basic steps. To make any calculation, we need operands, operators and then these are processed to give us the desired results. Now we have to implement all this through a simple calculator. This calculator will have three parts: 1. Input Unit 2. Processing Unit 3. Output Unit

Input Unit: In calculator, input unit will be a simple 4x4 keypad. The keypad comprises of 16 switches basically. As soon as one of the switch is closed when the corresponding key is pressed, that key is detected as an input by the processing unit.

Processing Unit: The brain of the system will be Tiva 4C123G Microcontroller. Input and output units will be interfaced with this microprocessor and data will be processed by it. The user code will be burnt to it.

Output Unit: 20x4 LCD will be used as an output unit. It will represent all our data and results. Various commands are sent to it for its proper functioning and display.

3

Interfacing and Implementation

Interfacing of all components with each other is the most important and basic requirement. As we have only LCD and Keypad, there interfacing is discussed one by one.

LCD: The interfacing will be discussed in two parts.

Hardware Interfacing: LCD Display used has 20x4 display size. It has 16 pins for interfacing. Their names are shown in figure. Hardware interfacing is quite easy. We just have to connect the microcontroller pins to the corresponding terminals of LCD. We can leave the VE pin connectionless because there is no contrast functionality being used. Register Select, Read/Write and Enable are collectively named as Control pins. These pins are responsible for controlling the functionalities of LCD. Then there are seven Data pins. Commands and data is sent through these pins. Then second last pin is for brightness control. It can be set in range from zero to five volts by a resistor. Problem and Solution of hardware interfacing: A problem may arise, is that data is not displayed on the LCD. Bright boxes appear in such case. This is because the ground of Microprocessor and LCD will be different. Common ground is essential for correct display of data.

4

Software Interfacing: Correct pin configuration is essential for interfacing. Three pins are allocated for Control purposes. RS pin is set high when data is to be sent. To send any command, it is set to low. Since we are using LCD as output unit so data will always be written to it and is not read from it. Then there is enable pin. Whenever data or command is to be sent, LCD is enabled through this pin. 8 bit data format is followed. Actually one character is sent at a time through its ASCII value. The character is then displayed on the LCD. There are various commands that we can send to perform various tasks such as clear, display on, move cursor to left and many others. They are shown in table as follows.

Using these set of commands we can display data properly. The C code for LCD interfacing with TIVA microcontroller is discussed in detail in Course Book. Problem and Solution for Software Interfacing: If proper delays are not set in between the commands, data may not be sent properly. So, some delay is necessary so that data is sent properly.

5

Keypad Interfacing: Keypad interfacing is discussed as follows.

Hardware interfacing: There are eight terminals for a 4x4 keypad. First four correspond to rows and next four are columns from left to right. The 16 buttons on keypad are basically 16 switches. When a button is pressed, switch is closed and a high signal is received by the micro controller. We set the columns high one by one. Now if a key is pressed the corresponding row becomes high also as the path gets completed. The intersection of this row and column tells microcontroller which key has been pressed. These rows act as input for microcontroller. In our design we have configured C7-C3 as four rows and PF1, PE2, PA7 and PA6 as four columns for microcontroller.

Problem and Solution for Hardware Interfacing: One of the problems is that the keypad is not of good quality therefore sometimes, it is to be pressed quite hard and sometimes only one key is pressed and more than one row get high. Therefore keys are to be pressed carefully. Other problem is that due to lose connections different rows get high randomly producing garbage. Therefore fine connections are essential. Another problem is that if whole assembly of project is being powered through and laptop is being charged at that time, then current flows through microcontroller and makes one of the rows high continuously. So, if assembly is to be powered through laptop then laptop must not be charged at that time.

Software Interfacing: Coding of keypad interface appears to be challenging. It was proposed that interrupt based scanning will be done, but later it was realized that for keypad it is not a good technique as discussed in course book. Therefore we had to revert to normal scanning technique. Four columns are set high one by one at a very high rate continuously. If any key is pressed then column number and row number is noted and there intersection gives us the pressed key. If no key is pressed zero is returned. The code for keypad is also available in course book.

6

Data Transmission from Keypad to LCD: This is the main part of interference where user input is to be displayed on the screen. Successful transmission of this data depends on the C code written. First of all we read the keypad. The key pressed takes its value from the 4x4 Array defined. Then this character is sent to LCD using LCD’s data sending data command. Problem and Solution: Actually this data transmission is not easy if it is being done for the first time. First of all a key is pressed and nothing is sent to the screen. It seems that the keypad or LCD is malfunctioning, but actually it is not. Put a never-ending while loop around the scan and data transmission. Now the statements will be executed. Just like: While(1) { Input = Scan_Keypad(); LCD_Send_Data(Input); } Another problem that will arise now is that continuous garbage will be displayed on the screen and if a key is pressed that character will be displayed in between that garbage. To overcome this write the code like this: While(1) { Input = Scan_Keypad(); if (input !=0) LCD_Send_Data(Input); } That garbage appears because zero is continuously returned by Scan_Keypad function. Another problem is that if a key is pressed it appears more than once. To correct this problem, put a small delay. Now everything will work fine.

7

Calculator

Once all the interfacing is done successfully, the final task is to write functions for the calculator tasks. A model is first designed, then following that model different forms of calculators are made making slight changes.

Data Assembling: When a key is pressed it gives a character type data. Character is sent to LCD. An array is defined of limited size. That array keeps storing the input as long as ‘=’ key is pressed. If an operand key is pressed, for example ‘+’ its position is saved. Data Retrieving and Processing: When the equal key is pressed it is the time to process the inputs and give the results. Since input was in character form. It was stored in array by subtracting 48 from each. This gives the integer value. Now to get the first operand a loop is used from starting position of the array till the value before the sign position. Following equation is written in that loop:

푛푢푚0 = 푎푟푟푎푦[푖] + (푛푢푚0 × 10) On similar pattern we can get second operand. After getting both operands, simply the operation is performed and stored in variable. Now this result is to be sent to LCD. Result Display on LCD: To display results on LCD data is to be converted back to character type. Earlier a function was made to convert numbers into integers and then send those integers in the form of characters. Later “sprintf” statement was used which converts integer type numbers to string. This string is then sent to LCD.

This is how a basic calculator works. Based on this pattern, three different types of calculators were made. They are Arithmetic, Scientific and Trigonometric Calculators. Arithmetic Calculator: Arithmetic calculator performs addition, subtraction, multiplication and division. Scientific Calculator: This calculator performs power of a number, factorial, nCr and of a number.

8

Trigonometric Calculator: This calculator performs sin, cos, tan and cot of angles taken in radians.

Limitation: Right now this calculator cannot take decimal value. Only integer type of data can be input. However floating results can be gained.

This is the basic design and implementation of a simple calculator.

9

Future Aspects

This calculator can be enhanced to perform more tasks and can improved in functionality.

Incorporation of Floating Data: In future we can take floating data from it as well. Just by increasing the number of intermediate points. Other than sign position we can also introduce first decimal and second decimal positions. They will describe the floating values easily.

Toggle Input: Since the number of keys are limited so to enhance the functions we can set toggled inputs. For example on first press of a button, ‘+’ if it is pressed after a small interval ‘-’ appears and so on. In this way we can increase our functions.

Graph Plotter: Currently graph plotting task was under process to increase the functionality but it was incomplete. User will enter his desired equation and he will see resulting waveform on the screen. For example on entering sin(2x) , a sine wave form would appear with corresponding values.

10

Bibliography

[1] "Innovative Logics," [Online]. Available: http://innovatelogics.com/interfacing-lcd-with- pic18f4550/.

[2] M. Tahir, ARM Microprocessor Systems , Cortex-M Architecture, Programming and Interfacing. Lahore: UET Lahore..

[3] Systronix, "HD44780U Data sheet".

[4] "," [Online]. Available: www.ti.com.

11