Multi-function calculator simulation system

: The STC89C52RC microcontroller serves as the core component, while a 4x5 matrix keyboard is utilized for data input. An LCD1602 display is employed for data visualization, and time processing is managed by the DS1302 chip. Additionally, the DS18B20 chip is responsible for measuring the ambient temperature. The multi-functional calculator is a basic arithmetic tool for addition, subtraction, multiplication and division, and it can also be used to time the year, month, day, hour, minute and second, as well as measure the current ambient temperature through its time and temperature measurement chips, bringing great convenience to users.


Introduction
With the advancement and innovation of science and technology, electronic products are being updated at a rapid pace. This multi-functional calculator simulation system is based on the STC89C52RC microcontroller as the core, through the time chip and temperature chip, it can time and read the current ambient temperature.

Task Objectives and Requirements
According to the requirements of the microcontroller-based multifunctional calculator system, design a simple calculator system for microcontrollers to realize simple addition, subtraction, multiplication and division functions. The specific functions required for the microcontroller-based multifunctional calculator system are as follows: (1) theoretical analysis of this design topic and propose a design plan; (2) design a hardware circuit schematic according to the requirements of the topic; (3) the system can realize the input using a 4x4 matrix keyboard, which can perform addition, subtraction, multiplication, and division numerical operations and make the process in LCD1602; (4) when not calculating, the system can display current time and temperature; (5) design the system to switch mode by key when the time can be adjusted [1].

Design ideas
The hardware circuit design mainly includes microcontroller core module, time module, display module and temperature measurement module, and its circuit diagram is shown in the figure. Each of these modules is described as follows.

Selection of microcontroller
The microcontroller uses STC89C52RC microcontroller produced by STC Hongjing Company, which has the advantages of high operating speed, low energy consumption, strong anti-interference ability, etc. It is an excellent new generation of 8051 enhanced microcontroller, and the code and instructions can be compatible with the traditional microcontroller and run 8 to 12 times faster than its speed. The operating voltage is only about 5V with low risk factor, and the frequency can be up to 48MHz when working. In addition, the microcontroller also has a watchdog function, on-chip integration of 512 bytes of RAM, the user application space of 8K bytes. After analysis and consideration of the final selection of STC89C52RC microcontroller as the main control chip.

System modules design options
The multi-function calculator simulation system consists of six primary components: a microcontroller control circuit, a 4*5 matrix keyboard circuit, an LCD1602 display circuit, a temperature display circuit, a time display and modification circuit, and a buzzer alarm circuit. In addition to the microcontroller control circuit, the specific design of each module circuit of the rest of the system is as follows.
(1) Keyboard module: Because of the design of the entire calculator, the calculator keying in values and clearing the zero part as well as mode switching and time modification with a number of keys to be used, so the number of keys required for the design is high. If a stand-alone keyboard is used, its software structure will be relatively simple but it needs to occupy a large number of input port lines, that is, IO port resources. The number of IO ports of the microcontroller is limited, so combined with the needs of this design, so take the 4 * 5 matrix keyboard approach. The matrix keyboard contains ten numeric keys (0~9), four mathematical operation symbol keys (, -, *, /), as well as reset keys and zero keys. In addition, there is a need to switch the mode to time and temperature display, so a mode change button and a time change button are needed. Therefore, a total of 20 keys are needed to form the matrix keyboard input module.
(2) Display module: The LCD1602 is used for calculation and time and temperature display, which has a very low working voltage of only 2-3V, and a small button battery can supply power for 1-2 years, which is incomparable to any other display devices. At the same time, its flat structure makes the display more intuitive and easy to accept. The information provided is large and easy to colorize, and also has a longer service life.
(3) Temperature acquisition module: In the temperature acquisition selected DS18B20 as the temperature acquisition chip, its wide range of adaptable voltage, with a special single-wire interface, and microprocessor connected to the microprocessor only a port line can achieve two-way communication between the microprocessor and DS18B20 temperature acquisition chip.
(4) Clock control module: for the processing of the clock is used DS1032 chip to the clock control, the clock chip output has been included in the leap year year, month, day, week, hour, minute and second, that is, the calendar time, no need to use software calculations or look up the table. Microcontroller timer is the work of the microcontroller beat counting, high precision, but not suitable for long time timing, because the middle of the microcontroller may reset. The use of clock chip timing is the calculation of the calendar time difference, the accuracy is lower, but not affected by the microcontroller reset, so the DS1032 chip is selected for clock-related control [2].
(6) Buzzer error alert: When a functional calculator is used to simulate the system, if an arithmetic error occurs, the corresponding alarm will be displayed through the buzzer alarm circuit, for example, when the number typed or the final answer obtained through calculation is higher than the range of the multifunction calculator, or when the divisor is 0, the calculator will also display the error, and the alarm circuit will start working, and the diode will light up red. A buzzer will sound to alert the calculator of the error.

Microcontroller Minimum System Module
The microcontroller includes all the basic functions required by a computer, such as RAM, ROM, CPU multifunction I/O, and other basic components integrated on a single chip. In order for the microcontroller to operate properly, it must have three basic operating conditions: normal power supply circuit, normal clock circuit and normal reset circuit.
The power supply circuit can supply energy and power for the work of the microcontroller, the corresponding wiring: the microcontroller pin 40 (VCC) for the power supply pins, in order to carry out the selection of the program memory control of the chip, the beginning of the operation of the 5V power supply, the 20th pin (GND) for the ground pin. Clock circuit for the microcontroller to issue timing pulses, the microcontroller's calculations and decisions are driven by the same timing pulse operation, the microcontroller's pin 18, pin 19 external 30pF ceramic capacitor and 11.0592MHz crystal oscillator circuit of quartz crystal, the clock circuit is the heart of the microcontroller. Microcontroller pin 9 external by 10uF electrolytic capacitor and the button together to form a button reset circuit. When the key button is not pressed, the capacitor is in charging state, which keeps the RST terminal low; when the key button is pressed, the capacitor is rapidly discharged and the RST terminal becomes high and enters the reset state.

4*5 matrix keyboard module
The matrix keyboard, respectively, consists of four I/O lines as row lines (P1.0, P1.1, P1.2, P1.3) and four I/O lines as column lines (P1.5, P1.6, P3.3, P3.4) together. The buttons are placed at the points where the row and column lines cross, and a 4*5 determinant constitutes a matrix keyboard with 20 buttons. However, for the sake of the overall aesthetics of the calculator, the actual number of keys required is only (0~9) ten numbers, plus (-, *, /, =) five mathematical symbols, a zero button, a mode switch button, a time change button, and a confirmation button, for a total of 19 keys. This type of matrix keyboard layout can significantly improve the usage of the I/O interface in a microcontroller system [3].
The key descriptions are as follows.

LCD1602 display module
In this design, the resistor is connected to the P0 port of the STC89C52RC microcontroller, and there are 9 pins in the resistor, one of which is connected to VCC. The P0 port can be used as an I/O port, or it can be used as an address or data bus. The P0 port is used as an I/O port. When the contrast of V0 reaches the weakest 5V power supply, when the contrast of V0 is the strongest ground, in order to prevent the generation of "ghost images" when the contrast is too high, so in use can access a 10kΩ potential regulator to adjust its contrast.
Screen LCD1602 with 5V voltage drive, it can show two lines of information, the screen can show 16 characters per line, with backlight, and cannot show Chinese characters, the internal ASCII set of 128 characters sub-base, only parallel interface, no serial interface. Its operation current is 2.0mA (5.0V), and the display capacity is 16×2 characters.

Clock Module
DS1302 synchronous clock can provide year, month, day, hour, minute, second, week, 31 days a month can be adjusted automatically, and has leap year compensation function. The voltage of the operation is as wide as 2.5~5.5V, and it takes double power supply for power supply (main power supply and backup power supply), and the backup power supply can be assembled for charging method, which provides the ability of trickle current charging to the backup power supply. VCC1 of the DS1302 clock chip is the backup power supply and VCC2 is the power supply, which can ensure the continuous operation of the clock if the main power supply is turned off. Therefore, it is widely used in measurement systems [4]. CE and clock control are connected to the P23 port of the microcontroller, data input and data output are connected to the P22 port of the microcontroller, and the serial clock SCLK is connected to the P21 port. It should be noted that the CE signal must be kept high when reading, the I/O pins are bi-directional data pins of the three-wire interface, and X1 and X2 are connected to an external 11.0592Hz crystal to improve stability and efficiency. The reference clock signal is generated by a crystal, which, in conjunction with the circuit in the chip, forms an oscillator. After frequency division, a high-precision time signal can be obtained.

Temperature detection module
The DS18B20 temperature sensor is used to provide 9-bit temperature digital drama readings to indicate the temperature of the device. The information content is transmitted to the temperature sensor through a single-wire port, requiring only one connection line and a ground between the microcontroller and the DS1820 clock chip. This unique single-wire interface enables communication through just one interface pin. Specifically, pin 2 serves as the digital signal input/output terminal and is connected to the microcontroller's P37 port, while pin 1 is connected to the 5V power supply and pin 3 is grounded.

Design of the main function
The design of the main function is primarily used to design and write the calculation function and set the time and the keypad settings (1) void Delay10us(char c), role: error 0 microseconds (2) void showClear(), role: initialize the interface (3) void KeyDown(), role: key function, to define and assign functions to the keys (4) void jisuanqi(), role: the main calculator function first, define a global variable num and flag = 0. Assume that num is the corresponding number 0 to 9, and flag is equal to 1. If you press 3, then num=1. In case1 a=num, that is, a=3, the value of a is a single digit, thus the LCD shows the value of a. When the second number is pressed, suppose the press is 4, flag equals 2. If you want to display 34 the two-digit number, in the case statement, at this time a cannot directly equal to num, but the original value * 10, that is, the original a = 3 into a * 10 = 3 * 10 = 30, that is, 30 4 that is, 34 at the same time and then assign the value to a so a is equal to 34, if the next button 5, constitute the number 345 a three-digit number, the principle is the same as above, the That is, after the original value *10, then add the digit and the tens place can be, at this time flag equal to 3. In case3 a=34 a*10=340 a num -> 340 5 then assign the value to a=345. At this point, if you add the high digit to the number and the matrix keyboard of the above-mentioned digital input principle are the same.
if(num==16) that is, key 16 is to exit the calculation mode door into the clock display mode.
Next is the display of the input numbers on the LCD, for example, how the LCD is displaying 345 a three-digit number. LcdWriteCom (0x80); // display coordinates LcdWriteData ('0' (a00)/100); LcdWriteData ('0' (a0)/10); LcdWriteData ('0' a); At this point a is equal to 345, a00 = 345 because 345 is less than 1000 equal to the original number; 345/100 = 3.45 because a is an integer variable, take the integer that is 3, 3 is the value of the hundredth digit and then displayed on the LCD screen; 3450 = 45 at this point 345 is greater than 100, then take the modulus operation is a multiple of the remainder, can be seen as 3000 = 0, 450, 45 and Less than 100 so only 45 is left after the modulus operation; 45/10 = 4.5 to the integer that is, 4, and then displayed in the LCD; 345 encountered that is, the last number is the single digit, and then finally displayed in the LCD. And the case in the program design only to 5 so the maximum number of 5 digits can only be entered. num=10~ num=13, respectively -* /, when the button is pressed, look at the corresponding if (num==0) if (num==11), etc. will find num_flag1, so the next if statement can be established, flag set to 0, used to re-enter the corresponding case statement [5].

LCD1602 display subroutine
Firstly, the pin functions are defined as follows: The VSS and LEDX pins are grounded, while the VCC and LEDA pins are connected to a 5V voltage source. The LCD display contrast adjustment section, V0, is connected to a 10kΩ resistor. The read/write selection terminal (H/L), R/W, is connected to the microcontroller's P2.6 pin. The data/command selection terminal (H/L), RS, is connected to the P2.5 pin, and the enable signal, E, is connected to the P2.7 pin. The data bus, D0 to D7, is connected to the microcontroller's P0.0 to P0.7 pins, respectively.
(2) function void LcdWriteCom(uchar com), role: write a byte to the LCD command.
(3) function void LcdWriteData(uchar dat), role: write a byte of data to the LCD; (4) function void LcdInCom(uchar com), role: write a byte of data to the LCD.
(4) function void LcdInit(), the role: initialize the LCD screen. The function of the command to reset the display is to clear the screen, cursor homing, and cursor moving to the upper left. The function of cursor homing is to clear the address counter (AC) to zero, the data of DDRAM does not move, and the cursor moves to the upper left corner. The function of character entry mode in the command is to set the screen shift mode when the character is entered. I/D=1 means that after writing or reading new data, the value of AC will automatically add 1 and the cursor will move right; I/D=0 means that after writing or reading new data, the value of AC will automatically subtract 1 and the cursor will shift left. The functions of the display buttons in the command are to assemble the display switch, cursor switch and blink switch. Among them, D=1 means LCD1602 display is on, D=0 means LCD1602 display is off; C=1 means there is a cursor, C=0 means there is no cursor; when B is high level, it means the cursor starts to blink, when B is low level, it means the cursor does not blink. The function of function setting is to set DL, display line number and font. Among them, the data length is 8 bits when DL=1; DL=0 means D4~D7 are 4 bits in total, and 1 byte is sent twice. N=0 is single line display, N=1 is double line display. F=1 is 5×10 dot matrix characters, F=0 is 5×7 dot matrix characters.

DS1302 subroutine
First of all, define the use of the 1302 port, data line DSIO connected to P2.2, reset RST connected to P2.3, serial clock SCLK connected to P2.1, DS1302 clock initialization Wednesday, January 1, 2013 12:00 minutes 00 seconds, storage order is seconds, minutes, hours, days, months, weeks, years, storage method is to use BCD code for storage.
(1) void Ds1302Write(uchar addr, uchar dat), role: to DS1302 command (address data) (2) uchar Ds1302Read(uchar addr), role: read the data of an address (3) void Ds1302Init(), role: initialize the DS1302 (4) void get_ds1302(), role: read the clock information (5) void showTime(), role: display the time (6) char ds1302_time_s = 0, char ds1302_time_g = 0, role: set the ds1032 time parameters, respectively, the ten-bit and single-bit time scalar (7) set the time function: void set_ds1302 (), modify the year decimal value, call the subroutine of the key, modify the time each modified value range is 0 ~ 9, more than the equivalent of invalid & & with the operation to be all true to hold. Display the range, if the tens digit is greater than 9 or display 9. After pressing button 18 to confirm, the time will be stored in the DS1302 chip, and then the next time after the power is turned off, the modified time will be read. For modifying the position of the year, the principle is the same as the previous one, but only the parameters are different. To modify the month, date, hour, minute, and second is similar to the above, all of them are taken out of the decimal and single digit, and then the keyboard function is called to modify the parameters. After all modifications Ds1302Init(); that is, save the time, the above first need to save the data into the DS1302_Time array, Ds1302Init() function inside the array of data into the DS1302 chip in turn because the array will be lost after power failure, and thus external power supply to prevent accidents.

DS18B20 subroutine
First define the DS18B20 use port, digital signal input/output terminal DQ connected to the microcontroller P3.7, GND for power ground, VDD for 5V voltage. The DS18B20 temperature sensor takes a one-wire bus communication, and it needs to reset the temperature sensor before each acquisition of temperature digital drama, i.e. initialization, in which it detects whether the temperature sensor exists or not, and returns to 0 if it is not detected. If the sensor is detected, a skip ROM matching command (unique to the sensor as a "skip finger") is issued, followed by a temperature conversion command to the DS18B20 to obtain the temperature data. The acquired temperature data must undergo corresponding data conversion and compensation. Finally, the processed temperature value is returned.

Conclusion
This design uses STC89C52RC microcontroller as the core, LCD1602 display and DS1302 chip as the hardware base for time processing, and DS18B20 chip as the ambient temperature acquisition module, which constitutes a complete timing device.
The entire design can be well realized through the setting of hardware circuits and the reasonable application and debugging of its internal function functions.
This system not only realizes the basic computing functions of a calculator, but also is an analog system that realizes time display and temperature measurement under the control of a microcontroller. The system has good scalability as well as strong practicality and other characteristics.