Long press the QR code below to enter the public account

Abstract
The signal generator is a commonly used signal source, widely applied in electronic circuits, automatic control systems, and teaching experiments.This design uses the AT89C51 microcontroller as the control core, with external digital/analog conversion circuits (DAC0832), operational amplifier circuits (LM324), buttons, and a 1602 liquid crystal display circuit. The circuit adopts a microcontroller and a digital-to-analog converter DAC0832 to form a digital low-frequency signal generator, capable of producing four waveforms: sine wave, square wave, sawtooth wave, and triangle wave.The system generates digital signals through the microcontroller, converts them into analog signals via DAC0832, and then amplifies them using LM324 to obtain various bipolar waveforms, which are finally displayed on an oscilloscope. The keyboard controls the selection of the four types of waveforms and frequency changes, and the LCD1602 displays their respective waveform types and frequency values.
This design features a simple hardware circuit, complete software functions, a reliable control system, and a high cost-performance ratio, providing practical and reference value.
Keywords:AT89C51 microcontroller;function signal generator;DAC0832;LCDliquid crystal display
1 System Overall Design
1.1 Basic Module Design and Verification
1.1.1 Signal Generation Module
Scheme 1: Use the MAX038 chip to form a circuit that outputs waveforms. The MAX038 is a precision high-frequency waveform generation chip capable of producing accurate triangular, square, and sine waveforms. However, this scheme has a high cost and complexity.
Scheme 2: Use discrete components to implement an unstable multivibrator oscillator, then add integration circuits as needed to form sine, square, triangle, and other waveform generators. This type of signal generator has a narrow output frequency range, and the circuit parameter settings are relatively complicated, often requiring hardware circuit switching for frequency measurement, which is inconvenient to operate.
Scheme 3: Use a microcontroller and DAC0832 digital-to-analog converter to generate waveforms. By programming, control the digital values input to the digital-to-analog conversion circuit, thus controlling the output signal’s waveform and frequency. Its characteristics include low cost, simple design, good stability in the low-frequency range, ease of operation, small size, and low power consumption.
After comparison, Scheme 3 meets the basic requirements of the graduation design while fully utilizing its advantages, with a simple circuit, easy control, and high cost-performance ratio, so this scheme is adopted.
1.1.2 Output Display Module
Scheme 1: Use a digital tube for display. Digital tubes are widely used due to their fast display speed, simple use, and clear display effect. However, since we plan to display the output signal waveform type and frequency, which contains a lot of content, digital tubes cannot display such rich content, so we abandoned this scheme.
Scheme 2: Use an LCD for display. LCDs are widely used due to their clear display, rich content, large information volume, ease of use, and fast display.
For this system, we selected the LCD 1602, which can display English letters and numbers, thus meeting the display requirements, so we chose this scheme.
1.1.3 Keyboard Module
Scheme 1: Use an independent key circuit, where each key occupies a separate I/O interface line, and the working status of each I/O port does not affect each other. This type of keyboard uses direct port scanning. The disadvantage is that when there are many keys, it occupies a large number of I/O ports of the microcontroller, while the advantage is that the circuit design is simple, and programming is relatively easy.
Scheme 2: Use a matrix keyboard, which is a 4*4 matrix row-column scanning. The advantage is that when there are many keys, it can reduce the number of I/O ports occupied by the microcontroller.
For this system, since the number of keys is not large, Scheme 1 is adopted.
1.1.4 Final Circuit Design Scheme
This system includes the main control module, display module, signal generation module, keyboard module, and operational amplifier module. Based on the above schemes, the final design scheme is determined as follows: use the AT89C51 as the main control system, LCD liquid crystal display as the display part, independent keys as the keyboard module, DAC0832 as the signal generation module, and LM324 as the current/voltage conversion amplification module.
1.2 System Principles and Overall Structure Diagram
1.2.1 Overall Structure Diagram of the System
According to the final circuit design scheme, the overall design structure of the system is shown in Figure 1.
Figure 1: Overall Structure Diagram of the System
1.2.2 System Working Principle
Digital signals can be converted into analog signals through a digital-to-analog converter, thus obtaining the required waveforms by generating digital signals and converting them into analog signals. In this design, the AT89C51 is the core part of the waveform generator, and the peripheral circuits include digital/analog conversion circuits, current/voltage conversion amplification circuits, key circuits, LCD liquid crystal display circuits, and crystal oscillator circuits. The external 12MHz crystal oscillator circuit provides working pulses for the microcontroller. The microcontroller’s P1 port connects to the LCD liquid crystal display circuit to display waveform types and frequency sizes; the P0 port connects to the digital-to-analog conversion chip DAC0832’s DI0~DI7 to provide digital signals for the DAC; the P2.0~P2.4 connects to the independent keyboard to control the signal types, frequencies, etc.; and the P3 part of the port serves as the control signal for the LCD.
By writing programs for the microcontroller, control the type of digital signal output from the P0 port. Pressing different keys can change this output signal, thus achieving transitions between sine wave, square wave, sawtooth wave, and triangle wave, and can change the frequency of the waveform output. During this process, the microcontroller processes the signal types and frequencies, which are then sent to the LCD display via the P1 port for display, with the first line showing the signal type and the second line showing the waveform frequency.
2 System Hardware Design
2.1 System Hardware Simulation Schematic
Hardware design is one of the key points of system design, and selecting suitable components is particularly important. In this design, based on the overall scheme, the AT89C51 is selected as the main controller, DAC0832 as the digital-to-analog conversion chip, LM324 as the current/voltage conversion component, LM016L as the LCD liquid crystal display component, and 5 independent keys as the keyboard module components. The hardware circuit diagram drawn in Proteus software is shown in Figure 2.
Figure 2: Hardware Simulation Schematic
2.2 Main Unit Circuit Design of the System
2.2.1 Main Controller AT89C51
AT89C51 is a low-voltage, high-performance CMOS 8-bit microcontroller produced by Atmel Corporation, which contains 4KB of reprogrammable read-only program memory (PEROM) and 128B of random access data memory (RAM). The device is produced using Atmel’s high-density, non-volatile storage technology compatible with the standard MCS-51 instruction set, and it has a built-in general-purpose 8-bit central processing unit (CPU) and FLASH storage unit. The powerful AT89C51 microcontroller can provide many high-cost performance applications.
Main performance parameters:
① Fully compatible with the MCS-51 product instruction set
② 4K bytes of reprogrammable FLASH memory
③ 1000 erase cycles
④ Fully static operation: 0HZ-24MHZ
⑤ Three-level encrypted program memory
⑥ 128 bytes of internal RAM
⑦ 32 programmable I/O port lines
⑧ 2 16-bit timers/counters
⑨ 6 interrupt sources
⑩ Programmable serial UART channel
Figure 3: AT89C51 Pin Package Diagram
AT89C51 pin package is shown in Figure 3, and the pin introduction is as follows:
(1) Main power pin
VCC: +5 V power supply VSS: ground.
(2) Clock circuit pins
XTAL1 and XTAL2: External crystal lead terminals. When using the chip’s internal clock, these two lead terminals are used for connecting quartz crystals and fine-tuning capacitors; when using an external clock, they are used for connecting external clock pulse signals.
(3) Control signal pins
RST/VPD: Reset signal. When the input reset signal lasts for more than two machine cycles at a high level, it is valid for completing the microcontroller’s reset initialization operation; when the microcontroller loses power, this pin can connect to a backup power supply, providing backup power to the internal RAM from VPD to keep the data in the internal RAM from being lost.
ALE/PROG: Address latch control signal. In system expansion, ALE is used to control the low 8 bits of the address output from the P0 port to be latched, achieving isolation of low address and data. In addition, since ALE outputs a positive pulse at a fixed frequency of the crystal oscillator 1/6, it can also be used as an external clock or external timing pulse.
PSEN: External program memory read selection signal output.
EA/VPP: Program storage access control signal. When the EA signal is low, the read operation of the ROM is limited to the external program memory; when the EA signal is high, the read operation of the ROM starts from the internal program memory and can extend to the external program memory.
(4) I/O pins
P0.0 ~ P0.7: P0 port 8-bit bidirectional port lines; P1.0 ~ P1.7: P1 port 8-bit bidirectional port lines; P2.0 ~ P2.7: P2 port 8-bit bidirectional port lines; P3.0 ~ P3.7: P3 port 8-bit bidirectional port lines.
The second function of the P3 port lines. All 8 lines of P3 have defined second functions, as detailed in Table 1.
|
Pin |
Second Function |
Signal Name |
|
P3.0 |
RXD |
Serial Data Reception |
|
P3.1 |
TXD |
Serial Data Transmission |
|
P3.2 |
INT0 |
External Interrupt 0 Request |
|
P3.3 |
INT1 |
External Interrupt 1 Request |
|
P3.4 |
T0 |
External Input of Timer/Counter 0 |
|
P3.5 |
T1 |
External Input of Timer/Counter 1 |
|
P3.6 |
WR |
External RAM Write Selection |
|
P3.7 |
RD |
External RAM Read Selection |
The above lists all signal pins of the 8051 microcontroller in terms of first and second functions. For various models of chips, the first function signal of the pins is the same, differing only in the second function signal. For pins 9, 30, and 31, since the first function signal and the second function signal are signals of the microcontroller under different working modes, there will be no conflict in usage. However, the situation of the P3 port is different; its second function signals are all important control signals of the microcontroller. Therefore, in practical use, the second function signals are selected first as needed, and the remaining ones are used as data bits for input/output.
As shown in Figure 4, the resource allocation of the microcontroller in this design is as follows:
(1) The microcontroller uses external circuits to provide clock pulses. A 12MHz quartz crystal is connected between the pins XTAL1 and XTAL2, making one machine cycle of the microcontroller equal to 1us.
(2) The P0 port is connected to the data input ports DI0-DI7 of the DAC0832; the P1 port is connected to the data input ports of the LCD liquid crystal display; the P2 port’s P2^0-P2^4 are connected to 5 independent keys; and the P3 part of the port is connected to the control signal interface of the LCD liquid crystal display.
Figure 4: Microcontroller Control Module
2.2.2 Digital-to-Analog Conversion Circuit
The digital-to-analog conversion circuit is the key circuit of this design, determining the conversion of digital signals into analog signals, thus obtaining various required waveforms.
1. Working Principle of the Digital-to-Analog Converter
The digital-to-analog converter can convert digital signals into analog signals proportional to the value. The digital quantity to be converted is represented by a binary number, with each binary digit having a certain weight. During conversion, each bit of the binary number is converted into the corresponding analog output component according to its weight, and then all output components are summed up, resulting in an analog value proportional to the digital quantity.
The above D/A conversion is commonly implemented using an inverted T-shaped decoding network as shown in Figure 5.
Figure 5: Inverted T-shaped Resistor Network D/A Converter
The circuit design shown in Figure 5 includes a resistor network composed of R-2R, analog electronic switches, and integrated operational amplifiers.
For the resistor network, regardless of which R-2R node is viewed, the equivalent resistance is R, thus the current input from the reference voltage end is:
IR=VREF/R
According to the shunt formula, the branch currents are:
I7=I/21I6=I/22I5=I/23I4=I/24I3=I/25I2=I/26I1=I/27I0=I/28
Thus, the output analog voltage from the operational amplifier is:
VO= – RF∑Ii= -RfbVREF/R×28(D7×27 +D6×26 +D5×25 +D4×24 +D3×23 +D2×22 +D1×21 +D0×20)
Where VREF is the reference voltage provided by the system for D/A conversion, Rfb is the feedback resistor of the integrated operational amplifier, and D7 ~ D0 take values of 0 or 1, where 0 indicates the switch is connected to ground, and 1 indicates it is connected to the reference voltage. Through the above circuit, the conversion from binary digital signals to analog voltage signals is completed.
2. Digital-to-Analog Converter Main Technical Indicators
(1) Resolution
Resolution refers to the change in analog output caused by a unit digital quantity input to the D/A converter, usually defined as the ratio of the output full-scale value to 2n (where n is the binary bit number of the D/A converter). Obviously, the more binary bits, the higher the resolution, meaning the D/A converter is more sensitive to input quantity changes. For example, for an 8-bit D/A converter, if the full-scale output is 10V, according to the resolution definition, the resolution is 10V/2n, which means the change in the lowest binary bit can cause a change of 39.1mV in the output analog voltage, which accounts for 0.391% of the full-scale output, commonly represented as 1LSB. When using, the bit number of the D/A converter should be selected according to the required resolution.
(2) Relative Accuracy
Relative accuracy refers to the difference between the analog output corresponding to any digital value and its theoretical value, under the premise that the full scale has been calibrated. It is usually expressed in terms of deviation in several LSBs.
(3) Settling Time
Settling time is a parameter that describes the speed of the D/A converter’s conversion, indicating the time required for the output to reach a total value error of ±(1/2)LSB (least significant bit). The conversion time for current output is shorter, while for voltage output converters, it takes longer due to the delay time of the operational amplifier needed to complete the I-V conversion. Fast D/A converters can control the conversion time to be less than 1us.
(4) Linearity
Linearity is usually expressed by the size of the non-linearity error of the D/A converter, with the deviation of the input-output characteristics expressed as a percentage of the full-scale output. The maximum non-linearity error at a certain temperature is generally between 0.01% and 0.03%.
3. DAC0832 Digital-to-Analog Conversion Chip Main Characteristics and Structure
The DAC0832 series is produced by National Semiconductor and features 8-bit resolution with 2 data registers. This chip is fully compatible with microprocessors, is low-cost, has a simple interface, and easy conversion control, making it widely used in microcontroller application systems.
1. Main Characteristics of DAC0832
l Resolution is 8 bits;
l Conversion time is 1us;
l Can be single-buffered, double-buffered, or direct digital conversion;
l Only requires linearity adjustment at full scale;
l Logic level input is TTL compatible;
l Powered by a single supply (+5V to +15V);
l Reference voltage range is ±10V.
2. Internal Structure of DAC0832
The internal structure block diagram of DAC0832 is shown in Figure 6. It consists of an 8-bit input latch, an 8-bit DAC register, an 8-bit D/A converter, and conversion control circuits, forming a two-level data input latch through two input registers.
Figure 6: Internal Logic Block Diagram of DAC0832
When in use, data input can be in the form of two-level latching (dual latching), single-level latching (single latching, one-level direct mode), or direct input (two-level direct). In the above figure, two AND gate circuits form the register output control logic circuit, which is responsible for data latching control.
3. Pin Functions of DAC0832
DI0~DI7: 8-bit data input lines.
CS: Chip select signal input, active low.
ILE: Data latch enable control signal, active high.
WR1: Input register write enable signal, active low.
WR2: DAC register write enable signal, active low.
XFER: Data transfer control signal, active low. The above two signals control whether the DAC register is in data direct mode. When WR2=0 and XFER=0, it is in DAC register direct mode; when WR2=1 or XFER=1, it is in DAC register latch mode.
IOUT1, IOUT2: Current output, IOUT1 + IOUT2 = constant.
Rfb: Feedback resistor input. Internally connected to the feedback resistor, externally connected to the output of the operational amplifier. To obtain voltage output, an operational amplifier must be connected at the output, and Rfb is the feedback resistor terminal of the operational amplifier.
Vref: Reference voltage, ranging from -10V to +10V.
AGND: Analog signal ground.
DGND: Digital signal ground, which is the working power ground and digital logic ground, can be grounded at a single point at the reference power supply.
VCC: Power supply input, ranging from +5V to +15V.
4. DAC0832 Chip Interface with Microcontroller
The DAC0832 can operate in three modes based on the connection of control signals: direct mode, single-buffered mode, and double-buffered mode.
1. Single-buffered mode
In this mode, the input latch and DAC register corresponding control signal pins are connected together, allowing data to be written directly into the DAC register, immediately performing D/A conversion (in this case, the input latch does not latch). This mode is suitable for systems with only one analog output or several analog outputs that do not require synchronization.
2. Double-buffered mode
For multiple D/A conversion outputs that require synchronization, the double-buffered synchronous mode can be used. When the DAC0832 operates in double-buffered mode, the input latch and D/A conversion are completed in two steps. First, the CPU’s data bus time-shares to input the digital quantities to be converted into each D/A converter’s input, latching them in their respective input latches. Then, the CPU issues control signals to all DACs, causing the data in the input latches to be transferred to the DAC registers, achieving synchronized conversion output.
3. Direct working mode
When the chip select signal CS, write signals WR1, WR2, and transfer control signal XFER pins are all grounded, allowing the input latch signal ILE pin to be connected to +5V, the DAC0832 chip operates in direct working mode. Once the digital quantity is input, it directly enters the DAC register for D/A conversion, resulting in the output of the converted analog quantity.
In this design, to reduce the settling time of the digital-to-analog conversion, the DAC0832 chip adopts direct working mode.
2.2.3 Current/Voltage Conversion Amplification Circuit
Since the analog signal converted from the DAC0832 is a current signal, to facilitate the observation of the output signal characteristics, the output signal from the DAC0832 is passed through a current/voltage conversion amplification circuit to convert it into a voltage signal. The operational amplifier used in this design is LM324.
LM324 operational amplifier is a low-cost operational amplifier with differential input functionality. Its pin diagram is shown in Figure 8.
LM324 features:
1. Short-circuit protected output
2. True differential input stage
3. Can operate with a single power supply: 3V-32V
4. Low bias current: maximum 100nA (LM324A)
5. Each package contains four operational amplifiers.
6. Features internal compensation.
7. Common-mode range extends to the negative power supply
8. Industry-standard pin arrangement
9. Input has electrostatic protection
Figure 8: LM324 Pin Diagram
The output signal from the DAC0832 is transformed into voltage after passing through the operational amplifier circuit in two output modes. One is the unipolar output mode. In this mode, the DAC0832 converts the digital quantity input from the microcontroller into voltage output through one LM324 operational amplifier, with the output voltage given by Uout=Din/28×(-VREF). When VREF=5V, the output range of Uout is -5V~0V. The other is the bipolar output mode. This design adopts the bipolar output mode, as shown in Figure 9. The principle of its bipolar output is: since U1 is -5V~0V, according to the current loop theorem, we have:
Uout=-VREF-2U1
Thus, when VREF=5V, and U1 is -5V~0V, the output of Uout is -5~+5V, achieving bipolar output.
Figure 9: DAC0832 and Two LM324s Achieving Bipolar Output
2.2.4 LCD Display Circuit
The display module is mainly used to show the waveform type and frequency of the output signal. In this design, LM016L is used as the display device. The LM016L intelligent display module is a low-power, low-cost, low-loss display device that can display various characters, Chinese characters, and graphics, while also having programmable capabilities and easy interfacing with microcontrollers.
1. Pin Function Description
The LCD LM016L uses a standard 14-pin interface, and the pin interface descriptions are as follows:
Pin 1: VSS is the ground power supply.
Pin 2: VDD connects to +5V positive power supply.
Pin 3: VEE is the contrast adjustment terminal of the liquid crystal display. When connected to the positive power supply, the contrast is weakest; when grounded, the contrast is highest. If the contrast is too high, it will produce “ghosting”. A 10K potentiometer can be used to adjust the contrast.
Pin 4: RS is the register select pin. When high, it selects the data register; when low, it selects the instruction register.
Pin 5: R/W is the read/write signal line. When high, it performs read operations; when low, it performs write operations. When both RS and R/W are low, instructions or display addresses can be written; when RS is low and R/W is high, the busy signal can be read; when RS is high and R/W is low, data can be written.
Pin 6: The E pin is the enable pin. When the E pin transitions from high to low, the liquid crystal module executes the command.
Pin 7~14: D0~D7 are 8-bit bidirectional data lines.
2. Basic Operation Timing Table
The read/write operation timing is shown in Figures 10 and 11.
Figure 10: Read Operation Timing
Figure 11: Write Operation Timing
3. RAM Address Mapping and Standard Character Library Table of LCD LM016L
The liquid crystal display module is a slow display device, so before executing each instruction, it is essential to confirm that the module’s busy flag is low, indicating it is not busy; otherwise, the instruction will be ineffective. To display characters, the display character address must be input first, which tells the module where to display the character. Figure 12 shows the internal display addresses of 016.
Figure 12: Internal Display Addresses of 1602LCD
For example, the address of the first character in the second row is 40H. However, can we directly write 40H to position the cursor at the first character of the second row? This is not possible because when writing the display address, the highest bit D7 must be kept high. Therefore, the actual data to be written should be 01000000B (40H) + 10000000B (80H) = 11000000B (C0H).
During the initialization of the liquid crystal module, its display mode must be set first. When the liquid crystal module displays characters, the cursor automatically moves to the right without manual intervention. Before each instruction input, the busy state of the liquid crystal module must be checked.
The internal character occurrence memory (CGROM) of the 016 liquid crystal module has stored 160 different dot matrix character patterns, as shown in Figure 13. These characters include Arabic numerals, uppercase and lowercase English letters, commonly used symbols, and Japanese kana. Each character has a fixed code, for example, the code for the uppercase English letter “A” is 01000001B (41H). When displayed, the module shows the dot matrix character pattern stored in address 41H, allowing us to see the letter “A”.
Figure 13: Character Code Corresponding to Graphics
4. LCD LM016L Display Circuit Diagram
In this design, the connection of the liquid crystal display circuit to the microcontroller is shown in Figure 14.
Figure 14: LCD Display Circuit
2.2.5 Keyboard Circuit
This subsystem consists of 5 independent keys and a 2-input AND gate, where the output of the AND gate is connected to the P3.2 port of the microcontroller to form an external interrupt. The 5 keys are connected to the P2.0~P2.4 ports of the microcontroller. One pin of the 2-input AND gate is connected to the K1 key, and the other pin is connected to a high level. When K1 is pressed, INT0 goes low, indicating that an external interrupt has occurred, and the system executes the interrupt program. When any of the K2~K5 keys are pressed, the corresponding P2.1~P2.4 port goes low, allowing the system to sense which key was pressed and execute the corresponding program. The functions of each key are shown in Table 2.
Table 2: Key Function Table
|
Key |
Function |
Attribute |
|
K1 |
Waveform Selection |
Auto Reset |
|
K2 |
Increase Frequency |
Auto Reset |
|
K3 |
Decrease Frequency |
Auto Reset |
|
K4 |
Increase Duty Cycle |
Auto Reset |
|
K5 |
Decrease Duty Cycle |
Auto Reset |
The circuit diagram of the keyboard circuit is shown in Figure 15.
Figure 15: Keyboard Circuit Diagram
3 System Software Design
The system software is mainly used to control the signal generator system to operate according to the designed logic. In software design, it is divided into multiple modules for programming, mainly including: main program module, four different waveform generation modules, key processing module, external interrupt module, LCD display module, etc.
3.1 Main Program Flowchart of the System
The main program flowchart of the system is shown in Figure 16, and the source program can be found in the attachment.
Figure 16: Main Program Flowchart of the System
3.2 Signal Generation Subroutine
This system uses programming methods to output four different waveforms: sine wave, square wave, triangle wave, and sawtooth wave. The methods for generating each waveform are as follows.
3.2.1 Sine Wave Generation Subroutine
The generation of sine waves is quite special; they cannot be directly produced by the microcontroller but can only be approximated by a staircase wave as shown in Figure 17. Clearly, the more steps there are in the staircase wave within one cycle, the closer the waveform output by the microcontroller will be to a sine wave.
Figure 17: Generation of Sine Wave Signal
Assuming the amplitude of the sine wave is 2.56 V, the valley corresponds to the minimum digital value 00H, and the peak corresponds to the maximum digital value FFH. The first cycle of the sine wave is divided into several equal parts by angle, and the corresponding voltage values at each point are calculated using the formula:
Vx=2.5·(1+sinθ)
Since 00H~FFH corresponds to the digital values of 0~255, the calculated voltage can directly yield the corresponding digital values at each point. The microcontroller stores the digital values of one cycle in a designated storage area, then sequentially retrieves these digital values and sends them to the D/A circuit to convert them into staircase waves, i.e., approximate sine wave outputs. The flowchart for sine wave generation is shown in Figure 18.
Figure 18: Flowchart for Sine Wave Generation
3.2.2 Square Wave Generation Subroutine
As shown in Figure 19, the implementation of square waves is relatively simple. First, define an unsigned character variable i=0, allowing the variable i to continuously increment by 1. If the value of i is less than squa_num, assign P0 the value of 0xFF; if the value of i is greater than squa_num, assign P0 the value of 0x00. When i increments to 256, it automatically resets to 0, repeating this process to obtain a square wave. When squa_num=128, the output is a square wave. Adjusting the size of squa_num allows for the adjustment of the duty cycle of the square wave; adjusting the delay time changes the frequency of the square wave.
Figure 19: Flowchart for Square Wave Generation
3.2.3 Triangle Wave Generation Subroutine
Triangle waves are approximated by small steps. When the step intervals are small, the waveform closely resembles a straight line. First, define an unsigned character variable i=0, allowing P0=i, and incrementing the variable i continuously by 1. If the value of i is less than 128, assign P0 the value of i; if the value of i is greater than 128, assign P0 the value of 256-i. When i increments to 256, it automatically resets to 0, repeating this process, thus achieving periodic digital quantity changes at the P0 port. After passing through the digital-to-analog conversion, it transforms into an analog signal, and after passing through the operational amplifier circuit, a periodic triangle wave is obtained. The flowchart for triangle wave generation is shown in Figure 20.
Figure 20: Flowchart for Triangle Wave Generation
3.2.4 Sawtooth Wave Generation Subroutine
The implementation process of sawtooth waves is similar to that of triangle waves. A variable i=0 is defined, and P0=i, with the variable i continuously incrementing by 1 until it reaches 255, after which i automatically resets to 0, repeating the process. During this process, the value of P0 changes in accordance with i, and after digital-to-analog conversion by DAC0832, the periodically changing digital quantity is converted into a sawtooth wave output. By adjusting the delay time between each two adjacent values of P0, the frequency of the sawtooth wave can be changed. The flowchart for sawtooth wave generation is shown in Figure 21.
Figure 21: Flowchart for Sawtooth Wave Generation
3.3 Key Processing Subroutine
In this design, the output waveform type, frequency size, and duty cycle of the square wave are adjusted through keys, using a program-controlled scanning method to constantly monitor whether any key is pressed. To accurately determine the key status, once a key is pressed, a delay is introduced to eliminate key bounce, and then it is determined which key was pressed, allowing the microcontroller to execute the corresponding program.
Figure 22: Key Processing Flowchart
3.4 LCD Display Subroutine
The display functions of the LCD LM016L are straightforward. By strictly following its timing diagram and combining it with its related instruction set, the initialization program, clear screen program, instruction writing program, data writing program, and data reading program can be written to meet all display needs of the LCD. In this design, since the content to be displayed is relatively large, and some need to be displayed repeatedly while others only once, only the basic flow of the liquid crystal display is drawn, as shown in Figure 23.
Figure 23: LCD Display Flowchart
3.5 INT0 Interrupt Subroutine
The interrupt subroutine is used to control the selection of output waveforms and display the waveform types on the LCD screen. When the waveform selection key K1 is pressed, it enters the interrupt program, at which point the waveform selection identifier WaveChoice is incremented by 1, and the corresponding waveform type is displayed on the LCD. After the interrupt ends, the main program outputs the corresponding waveform based on the value of WaveChoice. Figure 24 shows the INT0 interrupt flowchart.
Figure 24: INT0 Interrupt Flowchart
4 System Debugging
4.1 System Debugging Process
1. Compile and link the project to form the target file.
Open Keil, create a new project file, then add the source program file, saving it with the file extension changed to “.c”, and directly import it into the newly created project.
After entering all the source programs, perform a rough check to correct obvious errors, which can reduce the subsequent modification workload. Then, in the “Options for Target” under “Target 1”, click on “Output”, check the option “Create Hex” to generate an executable file. Then click on the “Project” menu and select the “Built Target” command to compile and link, forming the target file. Compilation and linking can be done using the “Project” menu’s “Built Target” command (or shortcut key F7), or by directly clicking the corresponding icon in the toolbar.
During compilation and linking, if there are syntax errors in the program, it will not pass the compilation, and the corresponding error messages will be given in the information window below. Errors must be corrected, while warnings can be ignored. Double-clicking the line showing the error message below can directly locate the line where the error is, allowing users to conveniently modify the program. After modification, compile and link again, continuing the debugging process, which may be repeated multiple times. If there are no syntax errors, the compilation and linking will be successful, and the information window will provide a prompt.
2. Run Debugging
In the Proteus software, first load the components to be used from the component library, then wire them on the schematic one by one, trying to avoid crossing wires for easier viewing and analysis, using wiring numbering methods if necessary. After completing all component wiring, right-click on the microcontroller and select “Edit Properties”, then click on the right side of the “Program file” line, and select the previously generated file with the suffix “.hex” to add the executable file for the AT89C51. Then click the relevant button in the lower left corner to start the simulation.
4.2 Problems Encountered During Debugging and Solutions
After determining the design scheme, to better debug, different modules can be debugged separately before overall debugging. During the entire debugging process, several problems were encountered, which were mostly resolved after consulting materials and asking teachers and classmates. The problems encountered during debugging and their solutions are as follows:
(1) During the debugging of the LCD display, when the waveform selection key changes, the corresponding waveform type displayed on the LCD also changes, achieving the design goal. However, due to the different character lengths of the displayed waveform types, when switching from a longer character to a shorter one, the extra characters from the longer one still display, causing errors in the display results.
Solution: Set the string lengths of the four waveform types to be the same, using spaces to fill in the insufficient letters, thus achieving the desired display effect.
(2) During the debugging of waveform generation, when the waveform selection key is pressed, there is no waveform output or only one waveform is output.
Solution: If there is no waveform output, first check whether the hardware schematic is connected properly, then check if there are issues in the program. If only one waveform is output, check if there is a dead loop in the program and whether the key control program meets logical requirements.
(3) When the functions of each module can be realized, overall debugging can be performed. During overall debugging, it was found that when the waveform selection key is pressed, the change in the waveform type displayed on the LCD and the change in the output waveform cannot be realized simultaneously.
Solution: When the waveform selection key is pressed, the display on the LCD only needs to be done once, while the output of the signal waveform is continuous. Therefore, use the external interrupt INT0; when a key is pressed, first respond to the interrupt program, display the output waveform type, and then return to the main program to output the corresponding waveform.
(4) The system uses a control delay method to change the output signal frequency, making it difficult to calculate the frequency values.
Solution: Set different delay times, calculate the corresponding frequencies from the output waveforms displayed on the oscilloscope, and record them. Then, based on the approximate execution time of each instruction, roughly estimate the function relationship between the output signal frequency and delay time. Of course, the frequency measured by this method has a significant error compared to the actual frequency, and this problem has not been perfectly solved.
4.3 System Simulation Results
After the above debugging, when confirming that the program has no issues, it is loaded into Proteus software for simulation, and the simulation results are as follows.
(1) At the beginning of running the program, without any key operations, the simulation result is shown in Figure 25.
Figure 25: Simulation Initialization Result
(2) When the K1 key is pressed to select the four types of waveforms, the simulation results for each waveform are as follows.
Figure 26: Sine Wave
Figure 27: Square Wave
Figure 28: Triangle Wave
Figure 29: Sawtooth Wave
(3) When the waveform is a sine wave, pressing the K2 key increases the frequency of the output waveform, as shown in the simulation in Figure 30.
Figure 30: Sine Wave Frequency Increase Simulation
(4) When the waveform is a square wave and the K4 key is pressed, the duty cycle of the square wave increases. The simulation result is shown in Figure 31.
Figure 31: Square Wave Duty Cycle Increase Simulation
5 Conclusion
After much effort, the design of the signal generator has been completed. Summarizing the work over the past two months, the main points are as follows:
(1) By reviewing a large amount of information, I have gained a better understanding of the current research status, principles, working methods, and technological developments of signal generators.
(2) Based on the design tasks and requirements, the design scheme of the signal generator was determined and gradually implemented.
(3) This system uses a microcontroller as the main controller, directly generating different waveform signal outputs through software, thus significant effort was put into programming, and much knowledge was gained.
(4) The simulation program was written in Keil, and the hardware simulation diagram was drawn in Proteus, achieving joint debugging simulation. From the simulation results, the design requirements were basically met.
In these tasks, I have also learned a lot. First, through this design, I have learned many software aspects related to electronics, such as Proteus, Keil51, etc. I have understood the entire work process from project selection, scheme selection, circuit design verification, component selection, to software and hardware design and implementation, and finally to overall machine testing and debugging, laying a solid foundation for future work. Secondly, this design has made me deeply realize the importance of mastering professional knowledge and understand the meaning of theory in practice, enhancing my interest in electronic design and production.
References
[1] Peng Wei. C Language Programming Training 100 Cases – Based on 8051 + Proteus Simulation[M].Electronic Industry Press,2009
[2] Wang Weiqing, Cheng Guogang. Microcontroller Keil Cx51 Application Development Technology[M].Beijing: People’s Posts and Telecommunications Press,2007
[3] Gong Yunxin. Microcontroller C Language Development Technology[M].Beijing: Tsinghua University Press,2006
[4] Yu Yong, Dai Jia. 51 Microcontroller C Language Common Modules and Comprehensive System Design Examples[M].Beijing: Electronic Industry Press,2007
[5] Cai Meiqin et al. MCS-51 Series Microcontroller Systems and Applications[M].Beijing: Higher Education Press,2004
[6] Lin Shenmao. Comprehensive Study of 8051 Microcontroller Practical Edition[M].Beijing: People’s Posts and Telecommunications Press,2004
[7] Hu Xuehai. Principles of Microcontrollers and Their Application System Design[M].Beijing: Electronic Industry Press,2005
[8] Han Zhijun. Microcontroller Application System Design[M].Beijing: Mechanical Industry Press,2005
[9] Shu Huailin. Principles of Microcontrollers and Interface Technology[M].Wuhan: Huazhong University of Science and Technology Press,2001
[10] Xu Aijun, Peng Xiuhua. 8051 Microcontroller Practical Tutorial[M].Beijing: Electronic Industry Press,2005
END
Long press the QR code below to enter the public account