You are on page 1of 30

Calculator with keypad and LCD -2011-

Calculator with keypad and LCD

Authors:
Palote Nicu Cristian Coniac Vasilica

Page - 1 -

Calculator with keypad and LCD -2011-

SUMMARY

1.Argument................................2 2. Objectives...3 3.Project Organization ...4 3.1 Hardware part.4 3.1.1 Keypad......................4 3.1.2 Microcontroller..6 3.1.3. LCD10 3.2 Software part..11 4.Conclusions13 5.Perspectives ...............14 6.Annexes..15

Page - 2 -

Calculator with keypad and LCD -2011-

1.ARGUMENT

Why we did this project? A pocket calculator is a special and relatively flexible device used to perform calculations. Whether you are home, office or traveling these compact computers are useful. They have low weight for easy transport, while having a stylish look. Easy to use and easy to read, they offer a range of convenient functions. Even if these computers are used for a long time we wanted to achieve our own computer. To find out how it works actually and in the future to bring improvements to existing pocket calculators.

Page - 3 -

Calculator with keypad and LCD -2011-

2. OBJECTIVES

In this project we wanted to make a calculator using: a 4x4 keypad a microcontroller from Fujitsu MB9530 an LCD resistors 10 k x 3.

Figure 1. Block diagram of the project

This computer will be able to execute operations of: addition subtraction multiplication division sqrt factorial and power.

Page - 4 -

Calculator with keypad and LCD -2011-

3. PROJECT ORGANISATION
3.1 HARDWARE PART 3.1.1. Keypad
Keypads are small keyboards used to enter numeric or alphanumeric data into microcontroller systems. Keypads are available in a variety of sizes and styles, from 2 x 2 to 4 x 4 or even bigger. This project uses a 4 x 4 keypad (Figure 2) .

Figure 2. 4x4 keypad Figure 2 shows the structure of the keypad used in this project which consists of sixteen switches formed in a 4 x 4 array and named numerals 09, Enter (B),Set Operation (A)-capabile to do +, , * and / , Delete(C), Sqrt(F), Factorial (E), Power (D). Rows and columns of the keypad are connected to PDR3 of a microcontroller which scans the keypad to detect when a switch is presed. The operation of the keypad is as follows: A logic 0 is applied to the first column via P34. Port pins P33 to P00 are read. If the data is zero, a switch is pressed. If P33 is 1, key 1 is pressed, if P32 is 1, key 4 is pressed, if P31 is 1, key 9 is pressed, and so on. A logic 0 is applied to the second column via P35. Again, port pins P33 to P00 are read. If the data is zero, a switch is pressed. If P33 is 1, key 2 is pressed, if P32 is 1, key 6 is pressed, if P31 is 1, key 0 is pressed, and so on. This process is repeated for all four columns continuously.
Page - 5 -

Calculator with keypad and LCD -2011-

The operation of the calculator is as follows: When power is applied to the system, the LCD displays text Firt line- MINI CALCULATOR Second line- MO 02, 28, 11 Then text No1: is displayed in the first row of the LCD and the user is expected to type the first number and then press the ENTER key. Then text No2: is displayed in the second row of the LCD, where the user enters the second number and presses the ENTER key if the key SO is pressed. After this, the appropriate operation key should be pressed. The result is displayed on the LCD and then the LCDcan be cleared, ready for the next calculation. The example that follows shows how numbers 12 and 20 can be added:
No1: 12 + SO No2: 20 If SO are pressed twice Operation result Res=32

In this project the keyboard is labeled as follows:


1 4 7 A 2 5 8 0 3 6 9 B F E D C

Figure 3. 4 x 4 keypad structure

Page - 6 -

Calculator with keypad and LCD -2011-

3.1.2. Microcontroller
Increasing the number of electrical components of implanted raised problems and the large number of connections decrease the reliability of equipment. These problems were solved by using complex integrated circuits. With the development of this technology started to develop microcontrollers. An integrated circuit is a complete electronic circuit carried out on a small piece of semiconductor material. Chip is encapsulated and used as one component. Reduced size of the device is even suggested by the word "micro" and "controller" tells us that the device can be used to control objects, processes and events. Any device that measures, stores, orders, calculates and displays information is a potential host for a microcontroller. For my project I used a microcontroller from Fujitsu MB90350E integrated on DICEKIT.

Figure 4. DICE-KIT from FUJITSU

MB90350E1 series is a general-purpose 16-bit microcontrollers designed for in vehicle applications. Consisting of CAN functions, I2C, capture, compare timer, A/D converter, and others, it is equipped with functions that are suitable for personal computer peripheral devices such as displays and audio devices, and control of devices that support in-vehicle communications. MB90350E series has the following features: Clock Built-in PLL clock multiplier circuit
1

According Datasheet Page - 7 -

Calculator with keypad and LCD -2011 Machine clock (PLL clock) selectable from divide-by-two oscillation clock or multiplyby-one to multiply-by-six oscillation clocks (4MHz to 24MHz when oscillation clock is 4MHz) Sub clock operation: Capable of maximum 50kHz (when operating with 100kHz and divide-by-two oscillation clock) of internal operation clock frequency (only for the product without S-suffix in the part number) Minimum instruction execution time: 42ns (when operating with 4MHz of oscillation clock and multiply-by-six PPL clock) Clock supervisor: Main clock or sub clock is independently supervised. Sub clock mode: The clock source can be selected from an external oscillator or built-in CR oscillator. 16 MB CPU memory space Internal 24-bit addressing Instruction system optimized for controllers Various data types (bit, byte, word, long word) Various addressing modes (23 types) Enhanced signed instructions of multiplication/division and RETI High-accuracy operations enhanced by 32-bit accumulator Instruction system for high-level language (C language)/multitask System stack pointer Enhanced pointer indirect instructions Barrel shift instructions Higher execution speed 4-byte instruction queue Powerful interrupt function Powerful interrupt function with 8 levels and 34 factors Supports maximum 16 channels of external interrupt CPU-independent automatic data transfer function Extended intelligent I/O service (EI2OS): Maximum 16 channels DMA function: Maximum 16 channels Low-power consumption (standby) mode Sleep mode (that stops CPU operating clock) Time-base timer mode (that operates only the oscillation clock, sub clock, time-base timer, and watch timer) Watch mode (that operates only sub clock and watch timer) Stop mode (that stops the oscillation clock and sub clock) CPU intermittent operating mode Process CMOS technology I/O ports General-purpose I/O ports (CMOS output): 49 ports (products without S-suffix in the part number; sub clock compatible device) 51 ports (products with S-suffix in the part number; sub clock non-compatible device) Timers Time-base timer, watch timer, watchdog timer: 1 channel 8/16-bit PPG timer: 8-bit 10 channels or 16-bit 6 channels 16-bit reload timer: 4 channels

Page - 8 -

Calculator with keypad and LCD -2011 16-bit I/O Timer - 16-bit free-run timer: 2 channels (FRT0: ICU0/ICU1, FRT1: ICU4/ICU5/ICU6/ICU7, OCU4/OCU5/ OCU6/OCU7) - 16-bit input capture (ICU): 6 channels - 16-bit output compare (OCU): 4 channels Full-CAN*1 controller: 1 channel Complies with CAN specification Ver. 2.0A and Ver. 2.0B 16 built-in message buffers CAN wake-up LIN-UART (LIN/SCI): 2 channels Full-duplex, double buffering Available for clock asynchronous/clock synchronous serial transfer I2C interface*2: 1 channel Capable of maximum 400kbit/s of communication DTP/External interrupt: 8 channels, CAN wake-up: 1 channel Starts the extended intelligent I/O service (EI2OS)/DMA and generates an external interrupt by an external input Delay Interrupt Generation Module Generates an interrupt request for task switching 8/10-bit A/D converter: 15 channels The resolution can be switched between 8 and 10 bits Can be started by an external trigger input Conversion time: 3 s (including sampling time when using a 24MHz machine clock) Program patch function Detects address match for 6 address pointers Low-voltage/CPU operation detection reset function (products with T-suffix in the part number) Resets automatically when low-voltage is detected (4.0 V0.3 V) Resets automatically when the counter is not cleared within the interval time (approximately 260ms when the external clock frequency is 4MHz) Clock supervisor

Page - 9 -

Calculator with keypad and LCD -2011This project uses a 4 x 4 keypad and an LCD to design a simple calculator. Rows and columns of the keypad are connected to PDR3 (Figure 6) of a microcontroller which scans the keypad to detect when a switch is presed. The LCD is connected to PDR0 and an external reset button is also provided to reset the microcontroller should it be necessary.

Figure 5. Circuit diagram of the project

Page - 10 -

Calculator with keypad and LCD -2011-

3.1.3. LCD
In this project we used a 2x16 LCD.

Figure 6. 2x16 LCD LCD connects to DICE-KIT through pin E, R \ W, RS. These pins are used for writing, erasing, resetting.

Figure 7. LCD pins

Page - 11 -

Calculator with keypad and LCD -2011-

3.2.SOFTWARE PART
The program consists of: a function called Get_key, which reads the pressed keys a function called Init uC, which initializes the microcontroller a function called Delay a function called Display, which display the number on the LCD a function called Get number 1 and Get number 2, which form the number a function called Get sign, which display de sign (^,+,-,*,/) when the SO is pressed a function called init LCD Screen, which clear the results a function called Set_op 3 functions called SQRT, POW, FACT and the main program

Variable Key stores the key value (0 to 15) pressed, variables no1 and no2 store respectively the first and second numbers entered by the user. All these variables are cleared to zero at the beginning of the program. A while loop is then formed to read the first number and store in variable no1. If key SO is pressed the second number is read from the keyboard. Function get_key receives a key from the keypad. We start by sending a 1 to column 1, and then we check all the rows. When a key is pressed, a logic 0 is detected in the corresponding row and the program jumps out of the while loop. It is important to realize that when a key is pressed or released, we get what is known as contact noise, where the key output pulses up and down momentarily, producing a number of logic 0 and 1 pulses at the output. Switch contact noise is usually removed either in hardware or by programming in a process called contact debouncing. In software the simplest way to remove the contact noise is to wait for about 20ms after a switch key is pressed or switch key is released.

Page - 12 -

Calculator with keypad and LCD -2011-

Figure 8. Software diagram

Page - 13 -

Calculator with keypad and LCD -2011-

4.CONCLUSIONS
Compared with other devices it has a low price, use a small number of components, production time is relatively short. Components: keypad 4 x 4-15 RON microcontroller MB90350 -15RON LCD-20RON Resistors 10 k x 3-3 RON Totally 53 RON.

Page - 14 -

Calculator with keypad and LCD -2011-

5.PERSPECTIVES
In the future we would like to bring a number of improvements such as making multiple functions, achieving trigonometric functions: sin cos tan Using the Braille code on each button we can make a computer for the blind. Result of operations could be using a buzzer sound.

Page - 15 -

Calculator with keypad and LCD -2011-

6.ANNEXES
Pieces used

Circuit made by us

Page - 16 -

Calculator with keypad and LCD -2011-

Software code in C using Softune program from Fujitsu. #include "mb90350.h" #include "Lcd.h" #define key1 0xEE #define key2 0xED #define key3 0xEB #define keyF 0xE7 //sqrt #define key4 0xDE #define key5 0xDD #define key6 0xDB #define keyE 0xD7 //n! #define key7 0xBE #define key8 0xBD #define key9 0xBB #define keyD 0xB7 //pow #define keyA 0x7E //set operation +,-,*,: #define key0 0x7D #define keyB 0x7B //display the result(Enter) #define keyC 0x77 //Delete /* 1 4 7 A 2 5 8 0 3 F 6 E 9 D B C

1 2 3 16 4 5 6 15 7 8 9 14 11 10 12 13 */

int key=0; int i=-1; int no1, no2, res=0,res2=0, m,n; int k=1; //************* init uC ****************************** void init_uC(void)

Page - 17 -

Calculator with keypad and LCD -2011{ DDR3=0x0F; PDR3=0xFF; // Set P30:33 output/ P34:37 input

DDR0=0xFC; // Set Port0 output PDR0=0xFF; } //********************* Delay ************************ void Delay_Ms(long int x) { while(x--) { __asm("\tNOP"); } } //****************** Get key ************************* char get_key(void) { int i,key=0; for(i=0;i<4;i++) { PDR3 &=~(0x08>>i); Delay_Ms(20); //Loop for 4 rows //Make rows low one by one // debounce

switch(PDR3) { case key1:{key=1;while(PDR3==key1);return key;break;} case key2:{key=2;while(PDR3==key2);return key;break;} case key3:{key=3;while(PDR3==key3);return key;break;} case key4:{key=4;while(PDR3==key4);return key;break;} case key5:{key=5;while(PDR3==key5);return key;break;} case key6:{key=6;while(PDR3==key6);return key;break;} case key7:{key=7;while(PDR3==key7);return key;break;} case key8:{key=8;while(PDR3==key8);return key;break;} case key9:{key=9;while(PDR3==key9);return key;break;} case key0:{key=10;while(PDR3==key0);return key;break;} case keyA:{key=11;while(PDR3==keyA);return key;break;} case keyB:{key=12;while(PDR3==keyB);return key;break;} case keyC:{key=13;while(PDR3==keyC);return key;break;} case keyD:{key=14;while(PDR3==keyD);return key;break;}

Page - 18 -

Calculator with keypad and LCD -2011case keyE:{key=15;while(PDR3==keyE);return key;break;} case keyF:{key=16;while(PDR3==keyF);return key;break;} default:break; } PDR3|= 0x08>>i; } return 0; }

//end switch //make read row high again

// end for

//********************* Display ************************** void Display(int key) { if(key==1) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("1"); Delay_Ms(20); } if(key==2) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("2"); Delay_Ms(20); } if(key==3) {

i++; LCDgoto(LCD_2nd_line+i); LCDprint("3"); Delay_Ms(20);

} if(key==4) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("4");

Page - 19 -

Calculator with keypad and LCD -2011Delay_Ms(20); } if(key==5) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("5"); Delay_Ms(20); } if(key==6) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("6"); Delay_Ms(20); } if(key==7) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("7"); Delay_Ms(20); } if(key==8) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("8"); Delay_Ms(20); } if(key==9) { i++; LCDgoto(LCD_2nd_line+i); LCDprint("9"); Delay_Ms(20); } if(key==10) { i++;

Page - 20 -

Calculator with keypad and LCD -2011LCDgoto(LCD_2nd_line+i); LCDprint("0"); Delay_Ms(20); } if(i==16) { clear2(); i=-1; Display(key); } } //******************** Get number1 ************************ int get_number1(void) { int j; for(j=1;j<11;j++) { if (key==j) { if(key!=10) { no1=no1*10+j; Display(key); } else { no1=no1*10+0; Display(key); } } } return no1; }

//****************** Get number2 ************************** int get_number2(void) { int l; for(l=1;l<11;l++)

Page - 21 -

Calculator with keypad and LCD -2011{ if (key==l) { if(key!=10) { no2=no2*10+l; Display(key); } else { no2=no2*10+0; Display(key); } } } return no2; } //************************Get sign************************** void sign(int k) { switch(k) { case 1:{ LCDgoto(LCD_1st_line+9); LCDprint(" ^");break;} case 2:{ LCDgoto(LCD_1st_line+9); LCDprint(" +");break;} case 3:{ LCDgoto(LCD_1st_line+9); LCDprint(" -");break;} case 4:{ LCDgoto(LCD_1st_line+9); LCDprint(" *");break;} case 5:{ LCDgoto(LCD_1st_line+9); LCDprint(" /");break;} default: { LCDgoto(LCD_1st_line+9); LCDprint(" ");break;} } } //*******************init LCD Screen*********************** void inti_LCD_Screen(void) { if(key==13) { i=-1; clear1(); clear2(); LCDgoto(LCD_1st_line); LCDprint("Mini Calculator"); LCDgoto(LCD_2nd_line+2); LCDprintdate(3, 02, 23, 11); k=0; res=0;

// second line ...

Page - 22 -

Calculator with keypad and LCD -2011no2=0; no1=0; } } //********************* Set_op **************************** void set_op( int z, int x, int y) { switch(z) { case 2:{ res=x+y; if(res<=9999) { LCDprint("Res="); LCDprintnum(res); } else { LCDprint("Overflow Error"); } break; } case 3:{if(x>=y) { res=x-y; if(res<=9999) { LCDprint("Res="); LCDprintnum(res); break; } else { LCDprint("Overflow Error"); break; } LCDprint("Res="); LCDprintnum(res); } else { res=y-x; if(res<=9999) {

Page - 23 -

Calculator with keypad and LCD -2011LCDprint("Res=-"); LCDprintnum(res); break; } else LCDprint("Overflow Error"); break; } break; } case 4:{if((x==0)||(y==0)) { res=0; LCDprint("Res="); LCDprintnum(res); } else { res=x*y; if(res<=9999) { LCDprint("Res="); LCDprintnum(res); } else { LCDprint("Overflow Error"); } } break; } case 5:{ res=x/y; LCDprint("Res="); LCDprintnum(res); if (res2=(x%y)>0) { LCDprint(","); LCDprintnum(res2); } else break; } default:z=0;

Page - 24 -

Calculator with keypad and LCD -2011} } //********************* Sqrt function *********************

int sqrt(float m) { float i=0; float x1,x2; int j; while( (i*i) <= m ) i+=0.1; x1=i; for(j=0;j<10;j++) { x2=m; x2/=x1; x2+=x1; x2/=2; x1=x2; } return x2; } //********************* Factorial function **************** int fact(int x) { if (x<=1) return(1); else x=x*fact(x-1); return(x); }

//********************* Pow function ********************** int pow(double x, double y) { int i; if (y<=0) return 0;

// we want a positive integer for the exponent

Page - 25 -

Calculator with keypad and LCD -2011else { double z=1; for (i=0; i<y; i++) { z*=x; } return z; } } //**************************** Main *********************** void main (void) { init_uC(); LCDinitdisp(); // Init. LCD Delay_Ms(2000); // wait a litle bit, please LCDgoto(LCD_1st_line+0.5); // seteaza prima linie LCDprint("Mini Calculator"); LCDgoto(LCD_2nd_line+2); // second line ... LCDprintdate(3, 02, 23, 11); Delay_Ms(2000); while(1) { while(key!=11) { key=get_key(); if ((key>=1) && (key<=10)) { clear1(); clear2(); i=-1; no1=get_number1(); // get no1 LCDgoto(LCD_1st_line); LCDprint("no1="); LCDprintnum(no1); clear2(); } if(key==16) { no1=sqrt(no1); clear2(); i=-1; clear1(); clear2();

Page - 26 -

Calculator with keypad and LCD -2011LCDgoto(LCD_1st_line); LCDprint("Operation Result"); // Display "Operation Result" LCDgoto(LCD_2nd_line); if(no1<=9999) { LCDprint("Res="); LCDprintnum(no1); inti_LCD_Screen(); } else { LCDprint("Overflow Error"); inti_LCD_Screen(); } Delay_Ms(20);

} if(key==15) { clear2(); i=-1; no1=fact(no1); clear1(); clear2(); LCDgoto(LCD_1st_line); LCDprint("Operation Result"); // Display "Operation Result" LCDgoto(LCD_2nd_line); if(no1<=9999) { LCDprint("Res="); LCDprintnum(no1); inti_LCD_Screen(); } else { LCDprint("Overflow Error"); inti_LCD_Screen(); } Delay_Ms(20); }

Page - 27 -

Calculator with keypad and LCD -2011inti_LCD_Screen(); }//end while(key!=11) if(key==11) { k++; clear1(); //clear LCD line 1 LCDgoto(LCD_1st_line); LCDprint("no1="); LCDprintnum(no1); i=-1; //reset i clear2(); LCDgoto(LCD_2nd_line); LCDprint("no2="); } inti_LCD_Screen();

i=i+4; while((key!=12)&(key!=13)) {

key=get_key(); no2=get_number2(); // get no2 if((key==14)&&(no2==0)&&(k==1)) { LCDgoto(LCD_1st_line); LCDprint("Operation Error"); // Display "Operation Result" LCDgoto(LCD_2nd_line); LCDprint("Please get No2"); } if(key==14) { i=-1; clear1(); clear2(); LCDgoto(LCD_1st_line); LCDprint("Operation Result"); // Display "Operation Result" LCDgoto(LCD_2nd_line); res=pow(no1,no2); if(res>=9999)

Page - 28 -

Calculator with keypad and LCD -2011{ LCDprint("Overflow Error"); inti_LCD_Screen(); } else{ LCDprint("Res="); LCDprintnum(res); inti_LCD_Screen(); } Delay_Ms(20);

} if(key==11) { k++; if(k==6) k=0; sign(k); } }

if(key==12) { if(k==1) { clear1(); clear2(); i=-1; if(no2==0) { LCDgoto(LCD_1st_line); LCDprint("Operation Result"); // Display "Operation Result" LCDgoto(LCD_2nd_line); LCDprint("No2 Missing"); Delay_Ms(20); } else {

Page - 29 -

Calculator with keypad and LCD -2011LCDgoto(LCD_1st_line); LCDprint("Operation Result"); // Display "Operation Result" LCDgoto(LCD_2nd_line); LCDprint("Operation Error"); Delay_Ms(20); } } else { clear1(); clear2(); i=-1; LCDgoto(LCD_1st_line); LCDprint("Operation Result"); // Display "Operation Result" LCDgoto(LCD_2nd_line); set_op(k,no1,no2); Delay_Ms(20); } }

Page - 30 -