Digital Logic Calculator Providing Calculations for Use with Microcontrollers Collin Butler John Reece

Digital Logic Calculator
Providing Calculations for Use with Microcontrollers
Collin Butler
John Reece
Objective: create a calculator to perform
common calculations for typical microcontroller
Design Goals:
– Calculations for 16-bit systems
– Simple arithmetic operations
– Conversions to binary and hexadecimal
MC9S12C32 Microcontroller
HD44780U LCD
Sparkfun COM-08653 Keypad
Pushbuttons (x4)
Arithmetic Operations
– Enter the first number
– Press desired operator button
– Enter the second number
– Press any operator button to display result
Base Conversions
– Enter the number
– Press desired operator (binary, hex)
– Result displayed automatically
CodeWarrior IDE
– Initialization of LCD
– Input of numbers read from the keypad
– Arithmetic calculations and base conversions
– Display of input and output numbers on LCD
• I/O Ports
• Port T
DDRT = 0x3f for LCD use
• Port AD
DDRAD = 0x15
Defines direction of digital I/O on port AD
ATDDIEN = 0x6a
Additional definition needed for inputs on Port AD
• Port M
All high impedance inputs
Number Input
• Waits for a user input
• Keypad or operation
• Uses getkey() function for keypad input
• Replaced PTT with
PTAD Digital I/O
register for Port AD
• Key strokes are
placed in an array a[i]
Number input (continued)
• However, numbers input need to be placed in
a variable with the correct value
• Example:
If the user was to input a 1, 2, and 7:
a[] = [1, 2, 7, 0 . . . 0], i=3
We would desire the value of our variable to be 127.
• Need code to implement:
“one” = a[i-1]*(10^0) + a[i-2]*(10^1) +….+a[0]*(10^i)
Number input (continued)
• Accomplished by using:
• Note:
• Loop counter i is incremented during the number
input loop, thus if 3 numbers were entered, i=3
• Addition (0x2b, ‘+’), subtraction (0x2d, ‘-’),
multiplication (0x2a, ‘*’), and division (0x2f, ‘/’)
• Push buttons read from PTM pins going high
• To binary (0x62, ‘b’), To hex (0x78, ‘x’)
• On keypad, buttons * and #
• This skips the entry of a second number
• ASCII value is assigned to char variable “op”
Operations (continued)
• Once variables “one” and “two” have been
entered, they are operated on accordingly
• Operations done according to value assigned to
the op variable
• Results are placed in the integer variable “result”
Binary and Hex Conversions
For Binary:
log[ ] =
{32768, 16384,
8192, 4096, 2048,
1024, 512, 256, 128,
64, 32, 16, 8, 4, 2, 1}
For Hex:
log[ ] =
{4096, 256, 16, 1}
Possible Improvements
Provide option for logic operations (AND, OR,
Implement keypad with a larger quantity of
buttons for additional operations
Equals button
Memory of previous answers