AVR-Based Calculator Code
AVR-Based Calculator Code
The code configures the microcontroller's ports by setting PORTA's lower nibble to output and the upper nibble to input. This is achieved by initializing PORTA with 0x00 and setting the Data Direction Register (DDRA) to 0x0F. This configuration allows the lower bits to send signals to the keypad rows while receiving signals from the columns through the upper bits, enabling accurate scanning of the keypad .
One limitation of the keypad scanning function is its reliance on sequential if-statements, which could lead to longer processing times for detecting keys pressed later in the sequence. Additionally, the code only handles single keypress events per scan cycle. Improvements could include optimizing the scan method using state machines, which might handle debounce issues more effectively, and constructing a priority check system that registers a keypress as soon as it's detected, terminating the loop early .
The code differentiates numeric key presses (0-9) from operators (>9 and <16) by checking the value returned from 'bacaKeyPad'. If the returned code is less than 10, it is treated as a numeric entry and updated into operand1 or operand2. If the code is between 10 and 15, it is treated as an operator, and the specific operator is assigned based on its value: 10 for '/', 11 for 'X', 12 for '-', 13 for '+'. An '=' key press (value 14) triggers the calculation .
The 'bacaKeyPad' function employs a 'while' loop with the condition using 'StatusBaca'. The loop keeps scanning each row of the keypad repeatedly until a key press is detected, indicated by checking bits PINA.4 to PINA.7. This continuous loop ensures that all input from the keypad is captured immediately upon pressing a key by users. The checking and resetting of 'StatusBaca' act as a control to maintain the scanning process .
The arithmetic results are formatted for display using the 'ftoa' function, which converts the floating-point result into a string format suitable for LCD output. After conversion, the result string is displayed using 'lcd_puts'. This process allows decimal results to be presented clearly on the LCD, ensuring readability and accuracy in the displayed information .
The code efficiently detects and handles user inputs by continuously scanning the keypad rows and checking the column inputs via a loop within 'bacaKeyPad'. However, the use of multiple 'if' statements for each key may reduce processing efficiency slightly, as all conditions are checked even if a key press is detected early. The logic effectively captures input but could be optimized further with switch cases or a more abstracted detection method .
The 'bacaKeyPad' function is responsible for reading the pressed key on a keypad connected to the microcontroller. It scans the keypad by enabling each row sequentially and checks which column is activated. When a key is pressed, it returns the value corresponding to the key's position. This continued scanning stops once a key is detected .
To accumulate multi-digit numbers, the code multiplies the current operand by ten and adds the new key press value each time a digit button (0-9) is pressed. This effectively shifts the digits to the left, building up the number progressively. This method enables users to input numbers greater than a single digit as the code concatenates the digits to form operand1 or operand2 .
The `lcd_puts` function is utilized to display strings on the LCD. In the context of the calculator program, it is used to show the numeric buttons pressed, the selected operator, and the result of arithmetic operations on the LCD screen. This function helps the user interactively follow the input and output process .
Upon receiving key inputs, the code starts by collecting numeric operands. It then checks if an operator button (10-15) is pressed, displaying the operator symbol and storing the operator type. When the '=' operator (code 14) is pressed, the code checks the stored operator and performs the corresponding arithmetic operation: division, multiplication, subtraction, or addition, using operand1 and operand2. The result is then converted to a string, displayed on the LCD, and the calculation sequence resets .