Myths and Facts on Climate Change

CPEG  Project 3

Keypad Calculator

For the third project, our goal is to implement a 4-digit decimal calculator. By pressing the number keys on the keypad, the user enters the first number for calculation, which is displayed on two SSDs (4-digits). By pressing the operation button (button A-F), user can insert the second number which is displayed on the SSDs as well. After finishing the input of the operands, the results will be displayed on the SSDs. While numbers are entered only as decimal values, the values can be displayed on the SSDs in either decimal or hexadecimal form.  The display and stored numbers can be cleared by pressing on-board btn1.

 

Goals of this project:

  • Learn how to use Pmodkeypad
  • Learn how to use pollingto capture input

Minimal Hardware:

One chipKIT Pro MX7 Board

One Pmod keypad

Two PmodSSDs

 

Inputs:

One on-board button: Btn1

The following buttons on the keypad will be used. Their definitions are listed below:

 

Inputs 0-9 A B C D E F BTN1
Functions
Input digit
Addition Subtraction Multiplication Division Equal HEX/DEC Transition Clear

 

Outputs:

Two external PmodSSDs displaying the absolute value ranging from 0 to (9999)10.

On-board LEDs showing the sign of sum, errors in the calculation, and current decimal/hex display. LED4 is ON if and only if the value being displayed is negative. LED1&LED2 are ON if the value is displayed in hex format.

 

Functional description:

General requirements for all modes:

  • When one button is pressed (and not released yet), any additional button pressing will be ignored.
  • Except for the overflow mode, any unnecessary “0” characters will not be displayed. For example, “345” should not be displayed as “0345”.

 

Conceptually the calculator has three modes:

  • Mode 1 – Initial
  • All on-board LEDs are off.
  • Sum and current input number is set to 0 by default.
  • SSDs display “0” in 1-digit (rightmost digit), and other digits are off.
  • Number is displayed in decimal and sign of sum is positive, by default.
  • The controller enters mode 2 automatically.

 

  • Mode 2 – Calculation
  • By default, all displayed numbers are in decimal. LED1 is ON and LED2 is OFF.
  • Number buttons ‘0’-‘9’ – when a number button is pressed,
  • The input numbers are recognized as decimal all the time.
  • If the last pressed button is not a number button, this will be the first digit for the new operand. The entered digit will be displayed at the rightmost position.
  • If the last pressed button is a number button, the current displayed number will shift one digit to the left and the just entered number will be displayed at the rightmost position (least significant digit). For example, if ‘1’,’2’,’3’,’4’ and ‘9’ have been pressed in sequence, the displayed number (which is identical to the value recorded as the operand) would be (1→12→123→1234→2349).
  • Operation buttons ‘A’-‘F’ and Btn1 – when an operation button is pressed:
  • Addition ‘A’(+) , Subtraction ‘B’ (-) , Multiplication ‘C’ (*) and Division ‘D’ (/):
  1. Whatever value on the SSD (e.g., the value entered by the user or the result left from previous calculation) will be used as the first operand. The display will be set to “0” to accept the second operand.
  2. If the last calculation hasn’t been finished yet (e.g., you entered “a-b*c”), the calculator will first finish the last calculation (a-b), store the result to sum, and then set the display to “0” to accept the next operand “c”.
  3. If one presses different operation buttons for multiple times, only the most recently pressed button will be considered (e.g., pressing “a + – b” will be considered as “a – b”).
  • Equal ‘E’ (=): calculate the sum and display it on SSDs.
  1. “E” will be ignored if the two operands and the operation have not been entered properly.
  2. Pressing “E” multiple times in a row will only trigger one calculation (the first time).
  • Decimal/Hex ‘F’: convert decimal to hex and vice versa.
  1. When the number is displayed in hex, LED1 and LED2 are ON. If Display in Hex mode, pressing all the other keys will be ignored, only ‘Btn1’ and ‘F’ works.
  • Clear ‘btn1’: Reset the controller to Mode 1 immediately.

 

  • If user performs a division by zero, enters mode 3 immediately.
  • If the result overflows (> 9999) or underflows (<-9999), which may happen when either A, B, C or E is pressed, the controller enters mode 3 immediately.

 

  • Mode 3 – Overflow
  • ALL on-board LEDs are OFF.
  • If overflow occurs, “HHHH” will flash on the SSDs at a frequency of approximately 2Hz.
  • If underflow occurs, “LLLL” will flash on the SDDs in a frequency of approximately 2Hz.
  • If division error (divided by o) occurs, “EEEE” will flash on the SSDs at a frequency of approximately 2Hz.
  • At this mode, all keys are invalid expect for ‘Btn1’. Pressing ‘Btn1’ will reset he calculator back to Mode 1 immediately.

 

Display quality and keypad input requirements:

  • All 4 digits of two SSDs should be always illuminated without observable flickering, and the luminance of each digit should be the same (at least cannot see the difference with eyes).
  • Button pressing should not interfere the display.
  • The calculator is sensitive to inputs. Mode switch, if any, happens as soon as a button is pressed. However, if a button is pressed less than 0.5 second, it is only recognized as one button press.

 

Customer Satisfaction Requirements:

Any of the following may account for deduction of your score:

  • Incorrect/unexpected operation
  • Unfamiliarity with any aspect of your project
  • Late for your demo
  • Poorly presented demo
  • Sloppy, undocumented program code

 

Questions for Project 3(to be submitted with the Flow Chart Tuesday after Spring Break):

  1. For keypad input capture, both interrupts (change notice) and polling method are introduced in the course. Please (1) introduce how the two methods work respectively. (2) Describe the difference between polling and interrupts and the pros and cons of each. (3) Considering theDigilentPmod KYPD schematic, should columns be inputs or outputs, what about rows, and why?

 

  1. In this project, you are asked to flash the SSD in the pattern “HHHH–off offoffoff–HHHH- off offoffoff” when overflow appears. The flash frequency is 2Hz. Now you are asked to use a peripheral timer (timer 1-5) to implement this flash function void flash(). Please:
    • Describe how to implement this display pattern using the timer.
    • Give the pseudo code for the function, including at least :
      1. Timer set up and ISR function.
      2. The switch between “HHHH” and “off offoffoff” display. Show how this switch happens. You can add any function or structure if necessary.
  1. You are asked to implement a SSD display function void Display_value(int value, intHorD) that displays a given value either in decimal or in hex on two SSDs. When HorD == 0, value will be displayed in decimal. When HorD == 1, value will be displayed in hex. Value ranges from 0 to (9999)10 . Note that unnecessary “0” will not be displayed on SSDs. Please write your C code to implement this function.

 

In question 2 and 3, you can call a subroutine void DisplayDigit() (given in Project 2 template) to display one digit, if that is useful.

 

 

Mid-stage checkpoint (in class device demo):

As a mid-stage checkpoint, you are asked to implement HEX/DECnumber displayer

  • Initially SSDs display “0000” and program is in DEC mode (LED1 ON/LED2 OFF).
  • The keys (0~9) are pressed to input number in decimal. Every time when a key is pressed, the current displayed number will shift one digit to the left and the just entered number will be displayed at the rightmost position. For example, if ‘1’,’2’,’3’,4’ and ‘5’ are pressed in sequence, the displayed number would be (1→12→123→1234→2345).
  • Anytime on-board Btn1 is pressed the display mode will toggle from DEC to HEX or from HEX to DEC. In HEX mode the number is displayed in hexadecimal form on the SSDs and LED2 is ON and LED1 is OFF.  In DEC mode the number is displayed as a base 10 number and LED1 is ON and LED2 is OFF.
  • Pressing Btn2 clears the number and resets the display to “0000”.
  • Interrupts and an ISR are used to refresh the SSD displays so that there is not flickering caused by polling the keypad. Any timer (including the coretimer) can be used.

 

Note that when displaying “A” through “F”, “b” and “d” are in lower case while the other letters are in upper case.

 

Due Dates:

  • In class Mid Project demo:                                 3/24 in class
  • Mid Project Report with Flowchart and Questions: 4/4 at 11:55AM thru Sakai
  • Final Project C Code:                                                                 4/6 at 11:55AM thru Sakai
  • Final Project Demo:                                 4/7 afternoon
Order from us and get better grades. We are the service you have been looking for.