Calculator With Keypad and LCD

Embed Size (px)

Citation preview

  • 8/12/2019 Calculator With Keypad and LCD

    1/30

    Calculator with keypad and LCD

    -2011-

    - 1 -

    Calculator with

    keypad and LCD

    Authors:

    Palote Nicu Cristian

    Coniac Vasilica

  • 8/12/2019 Calculator With Keypad and LCD

    2/30

    Calculator with keypad and LCD

    -2011-

    - 2 -

    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

  • 8/12/2019 Calculator With Keypad and LCD

    3/30

    Calculator with keypad and LCD

    -2011-

    - 3 -

    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 toread, 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.

  • 8/12/2019 Calculator With Keypad and LCD

    4/30

    Calculator with keypad and LCD

    -2011-

    - 4 -

    2. OBJECTIVES

    In this project we wanted to make a calculator using: a 4x4 keypad a microcontroller from Fujitsu MB9530 an LCD resistors 10 kx 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.

  • 8/12/2019 Calculator With Keypad and LCD

    5/30

    Calculator with keypad and LCD

    -2011-

    - 5 -

    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 x4 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)-capabileto 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 1is 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 is1, 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.

  • 8/12/2019 Calculator With Keypad and LCD

    6/30

    Calculator with keypad and LCD

    -2011-

    - 6 -

    The operation of the calculator is as follows:

    When power is applied to the system, the LCD displays textFirt 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 typethe first number and then press the ENTER key.

    Then text No2: is displayed in the second row of the LCD, where the user enters thesecond 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 followsshows how numbers 12 and 20 can be added:

    No1: 12 +SONo2: 20If SO are pressed twiceOperation resultRes=32

    In this project the keyboard is labeled as follows:

    1 2 3 F4 5 6 E7 8 9 DA 0 B C

    Figure 3.4 x 4 keypad structure

  • 8/12/2019 Calculator With Keypad and LCD

    7/30

    Calculator with keypad and LCD

    -2011-

    - 7 -

    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 encapsulatedand 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 DICE-

    KIT.

    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:

    ClockBuilt-in PLL clock multiplier circuit

  • 8/12/2019 Calculator With Keypad and LCD

    8/30

    Calculator with keypad and LCD

    -2011-

    - 8 -

    Machine clock (PLL clock) selectable from divide-by-two oscillation clock or multiply-

    by-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 productwithout 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 spaceInternal 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-basetimer, 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 portsGeneral-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

  • 8/12/2019 Calculator With Keypad and LCD

    9/30

    Calculator with keypad and LCD

    -2011-

    - 9 -

    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 channelStarts 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 functionDetects address match for 6 address pointers

    Low-voltage/CPU operation detection reset function (products with T-suffix in the partnumber)

    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

  • 8/12/2019 Calculator With Keypad and LCD

    10/30

    Calculator with keypad and LCD

    -2011-

    - 10 -

    This 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 thekeypad to detect when a switch is presed.

    The LCD is connected to PDR0 and an external reset button is also provided to reset themicrocontroller should it be necessary.

    Figure 5.Circuit diagram of the project

  • 8/12/2019 Calculator With Keypad and LCD

    11/30

    Calculator with keypad and LCD

    -2011-

    - 11 -

    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

  • 8/12/2019 Calculator With Keypad and LCD

    12/30

    Calculator with keypad and LCD

    -2011-

    - 12 -

    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 1and 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 themainprogram

    Variable Key stores the key value (0 to 15) pressed, variables no1 and no2 store respectivelythe first and second numbers entered by the user. All thesevariables are cleared to zero at thebeginning of the program. A while loop is thenformed to read the first number and store invariable no1.

    If key SO is pressed the second number is read from the keyboard. Function get_keyreceives a key from the keypad. We start by sending a 1 to column 1, and then we check all therows. 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.

  • 8/12/2019 Calculator With Keypad and LCD

    13/30

    Calculator with keypad and LCD

    -2011-

    - 13 -

    Figure 8.Software diagram

  • 8/12/2019 Calculator With Keypad and LCD

    14/30

    Calculator with keypad and LCD

    -2011-

    - 14 -

    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 kx 3-3 RON

    Totally 53 RON.

  • 8/12/2019 Calculator With Keypad and LCD

    15/30

    Calculator with keypad and LCD

    -2011-

    - 15 -

    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.

  • 8/12/2019 Calculator With Keypad and LCD

    16/30

    Calculator with keypad and LCD

    -2011-

    - 16 -

    6.ANNEXES

    Pieces used

    Circuit made by us

  • 8/12/2019 Calculator With Keypad and LCD

    17/30

    Calculator with keypad and LCD

    -2011-

    - 17 -

    Software codein 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 2 3 F

    4 5 6 E7 8 9 D

    A 0 B C

    1 2 3 164 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)

  • 8/12/2019 Calculator With Keypad and LCD

    18/30

    Calculator with keypad and LCD

    -2011-

    - 18 -

    {

    DDR3=0x0F; // Set P30:33 output/ P34:37 inputPDR3=0xFF;

    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>i); //Make rows low one by oneDelay_Ms(20); // 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;}

  • 8/12/2019 Calculator With Keypad and LCD

    19/30

    Calculator with keypad and LCD

    -2011-

    - 19 -

    case keyE:{key=15;while(PDR3==keyE);return key;break;}

    case keyF:{key=16;while(PDR3==keyF);return key;break;}

    default:break;} //end switch

    PDR3|= 0x08>>i; //make read row high again

    } // end for

    return 0;

    }

    //********************* 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");

  • 8/12/2019 Calculator With Keypad and LCD

    20/30

    Calculator with keypad and LCD

    -2011-

    - 20 -

    Delay_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++;

  • 8/12/2019 Calculator With Keypad and LCD

    21/30

    Calculator with keypad and LCD

    -2011-

    - 21 -

    LCDgoto(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

  • 8/12/2019 Calculator With Keypad and LCD

    22/30

    Calculator with keypad and LCD

    -2011-

    - 22 -

    {

    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); // second line ...

    LCDprintdate(3, 02, 23, 11);

    k=0;res=0;

  • 8/12/2019 Calculator With Keypad and LCD

    23/30

    Calculator with keypad and LCD

    -2011-

    - 23 -

    no2=0;

    no1=0;}

    }//********************* Set_op ****************************

    void set_op( int z, int x, int y)

    {

    switch(z)

    {

    case 2:{

    res=x+y;if(res=y)

    {res=x-y;

    if(res

  • 8/12/2019 Calculator With Keypad and LCD

    24/30

    Calculator with keypad and LCD

    -2011-

    - 24 -

    LCDprint("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(res0){

    LCDprint(",");

    LCDprintnum(res2);

    }

    else break;

    }

    default:z=0;

  • 8/12/2019 Calculator With Keypad and LCD

    25/30

    Calculator with keypad and LCD

    -2011-

    - 25 -

    }

    }

    //********************* Sqrt function *********************

    int sqrt(float m)

    {

    float i=0;

    float x1,x2;

    int j;

    while( (i*i)

  • 8/12/2019 Calculator With Keypad and LCD

    26/30

    Calculator with keypad and LCD

    -2011-

    - 26 -

    else {

    double z=1;for (i=0; i=1) && (key

  • 8/12/2019 Calculator With Keypad and LCD

    27/30

    Calculator with keypad and LCD

    -2011-

    - 27 -

    LCDgoto(LCD_1st_line);

    LCDprint("Operation Result"); // Display "OperationResult"

    LCDgoto(LCD_2nd_line);if(no1

  • 8/12/2019 Calculator With Keypad and LCD

    28/30

    Calculator with keypad and LCD

    -2011-

    - 28 -

    inti_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 no2if((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)

  • 8/12/2019 Calculator With Keypad and LCD

    29/30

    Calculator with keypad and LCD

    -2011-

    - 29 -

    {

    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{

  • 8/12/2019 Calculator With Keypad and LCD

    30/30