Design a calculator with multi functions, Electrical Engineering

Assignment Help:

Aims

  • More practice using data movement and control flow statements.
  • Understand techniques to interface microprocessors with external switch-based hardware.
  • Write PIC assembly code to implement a simple 1-digit 4-function calculator Equipment
  • PIC Trainer and PICkit3 in-circuit debugger PIC trainer numeric keypad
  • MPLAB Integrated Development Environment software Background documents (available on FLO)
  • MPASM Assembler Users Guide
  • PIC18F452 Instruction Set Summary
  • PIC Trainer board schematic

1718_Design a Calculator with Multi Functions 1.png

Checkpoint 1: Getting Started

1. Obtain a numeric keypad and carefully plug it into Port C of the PIC trainer board, making sure that the board is oriented correctly and the pins are aligned properly with the jacks. Set the DIP switches so that all switches are off except LCD, LEDS C and LEDS D.

2. Create a new MPASM project named prac3, making sure that the processor and toolsuite are correct for the PIC Trainer, the configuration bits are set to the appropriate values, and the correct debugger selected. Create a new project source file named calculator.asm.

3. The numeric keypad is wired as 4 rows of 4 columns, as shown in the schematic. To operate the device, bits 0-3 of the control port (PORTC in this prac) must be configured as inputs and bits 4-7 must be configured as outputs by writing the appropriate value to TRISC. To read the status of the keypad, a single row output is taken low, and the status of the column inputs is read. If any of the switches on the activated row are pressed, the corresponding column inputs will be pulled low. For example, to see if the 4 key is pressed, set bit 5 low and test bit 0.

4. Write code to initialise the PIC trainer, setting the port D IO bits to output and taking care to set the status of the port C IO bits as appropriate.

5. Write a subroutine named scan_keypad that tests each of the 16 keys on the keypad and returns with a scan code in WREG. The scan code should be the code of the first key found to be pressed, ranging from h'0' for the top-left key (the 1 key) to h'F' for the bottom-right key (the D key). If no key is pressed, the subroutine should return the special value h'FF'. The simplest way to test for a particular key is to write a value to PORTC that has zeroes for the bits corresponding to the row and column you want to test and ones for all other bits. Then you can test if the key is pressed by comparing the value read from the port with the value you wrote to it. (The trick works because reading a value back from a bit configured as output will simply read the value written, whereas writing a value to an input bit is effectively ignored.) For example, to test the B key, write the value b'11010111' (zeros for bits 5 and 3), then read the value back. If the values are the same, then you know that column 3 must be being pulled low by the B key. If not, either key B is not pressed, or some other key is also pressed. An easy way to test all keys is to write a lookup routine that maps a scan code into the appropriate bit pattern for that key, then loop over all of the possible scan codes looking for one that matches. If you get to the end of the loop without finding a match, return the "no key" code instead. 6. Write a main loop that repeatedly calls your scan_keyboard routine and writes the return value to PORTD. When you press a key, the corresponding code should show on the port D LEDs (in binary, of course). When no key is pressed, all of the LEDs should be lit.

Checkpoint 2: ASCII Codes

7. Write a subroutine named get_char that reads a single key press from the keypad and returns the key's ASCII code in WREG. To check for a key press event, the routine will first need to wait for any previously pressed keys to be released, then wait for a new key to be pressed. The code will look something like this:

2022_Design a Calculator with Multi Functions 2.png

The code to check which keys are pressed should take into account the possibility of switch bounce in the keypad. Otherwise, a "noisy" switch press will appear to the code as several rapid keystrokes. A suitable de-bounce technique is to read the keypad three times in succession with a short delay (10 ms is typical) between each read. If all of the values agree, you can assume that the switch is stable and use the value. But if any value differs, the switch must be "bouncing" so you'll need to go back and begin the sequence again. The simplest way to translate the scan code to ASCII is via yet another lookup table. Since the keypad will be used later in this prac to provide input for a calculator, the appropriate value to return for the number keys is the ASCII code for the digits. The other keys should be mapped as follows:

A ⇒ '+', B ⇒ '-', C ⇒ '*', D ⇒ '/', # ⇒ '=', * ⇒ 'C'.

198_Design a Calculator with Multi Functions 3.png

8. Modify your main loop so that it calls get_char to read ASCII codes from the keypad and displays them on the LCD. Successive values can be displayed by calling the LCD subroutine (from LCD.asm) with the value in WREG. However if the value read is 'C' (for "clear"), the LCD display should be cleared instead. You'll need to include the file LCD.asm and call init_LCD to initialise the LCD module before sending it any output. Checkpoint 3: Single-Digit, Single-Function Counter

9. Modify your main loop so that it implements a single-digit calculator with only one function: addition. Your code should read and display a sequence of 4 key presses, which will consist of a digit key, one of the operator keys (the only one that need work for now is the A or '+' key), another digit key, and the # or '=' key. It should then calculate and display the answer as a 2-digit decimal number. To calculate the sum, you'll first need to convert the ASCII digit codes to the corresponding digit values by subtracting h'30'. Then you can add the digits and adjust for any decimal overflow using the DAW instruction. Finally, you'll need to convert the high and low nibbles of the result back into displayable ASCII codes by adding h'30'. Checkpoint 4: Multi-Function Calculator

10. Extend your calculator to implement the other 3 arithmetic functions. To implement the other functions, you may wish to make use of the following strategies

  • Subtraction is best implemented by adding the negative of the second number. To represent a negative number in BCD, use the 10's complement (formed by taking the 9's complement and adding 1).
  • Multiplication is best implemented as repeated addition. Initialise a result variable to zero, then use the second number as a loop counter and repeatedly add the first number to the result.
  • Division (not that it's particular useful for single-digit operands!) is best implemented as repeated subtraction. Initialise a result variable to zero, then count the number of times you can subtract the second number from the first before the result becomes negative. You'll need to check for the special case where the second number is zero.

To Explore Further

11. (Difficult) Extend the calculator to 2 digits. You can use a 1-byte value (encoded as 2 BCD digits) for the operands and a 2-byte value for the result. You'll probably find it convenient to implement a 2-byte BCD addition routine.

12. (Very challenging) Extend the calculator to multiple digits. The simplest strategy is probably to use one byte per digit, with subroutines to perform the various arithmetic operations.


Related Discussions:- Design a calculator with multi functions

Explain segment assembler directive, Explain SEGMENT assembler direct...

Explain SEGMENT assembler directive with example. SEGMENT: This directive described to the assembler the start of a segment along with name segment-name. The seg

Excess3 to bcd code converter using nand gates, design a excess3 to bcd cod...

design a excess3 to bcd code converter using only nand gates

Enhancement type mosfet, Enhancement type MOSFET In these types of de...

Enhancement type MOSFET In these types of devices operate by comprising a channel enhanced in the semiconductor material in which no channel was constructed, they are termed

Use thevenin''s theorem find the current flow by resistor, Use Thevenin's T...

Use Thevenin's Theorem, find the current flow by resistor R=10Ω.

Rim read interrupt mask instruction, RIM Read Interrupt Mask Instruction ...

RIM Read Interrupt Mask Instruction This  instruction is used  to read the  status  of interrupts  and  read the serial  input  data. The status  byte is loaded  into  the accu

Electromechanical energy conversion, derived expression for reluctant torqu...

derived expression for reluctant torque in rotating electrical machine

Show jkff connected as a t flip-flop, Q. For a JKFFwith JK = 11, the output...

Q. For a JKFFwith JK = 11, the output changes on every clock pulse. The change will be coincident with the clock pulse trailing edge and the flip-flop is said to toggle, when T = 1

Compute the worst voltage regulation, Q. The following data were obtained o...

Q. The following data were obtained on a 25- kVA, 2400:240-V, 60-Hz, single-phase distribution transformer: • Open-circuit test with meters on LV side: 240 V, 3.2 A, 165 W •

Determine the motor current and speed, A dc series motor is connected to a ...

A dc series motor is connected to a load. The torque varies as the square of the speed.With the diverter-circuit open, the motor takes 20 A and runs at 500 r/min. Determine the mot

Bifurcation using simulink, i am implementing a paper "on the chaotic behav...

i am implementing a paper "on the chaotic behaviour of buck converter" a want to plot bifurcation diagram by using simulink blocks without using coding is it possible??

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd