1 Introduction
Today is an era in which new technologies are emerging one after another. In the electronic field, especially in the field of automated intelligent control, traditional control systems composed of discrete components or digital logic circuits are being replaced by single-chip intelligent control systems at an unprecedented speed. The single-chip microcomputer has the advantages of small size, strong functions, low cost, and wide range of applications. It can be said that the core of intelligent control and automatic control is the single-chip microcomputer. At present, a climax of learning and applying microcontrollers is emerging on a large scale in factories, schools, enterprises and institutions. The most effective way to learn microcontrollers is to pay equal attention to theory and practice. In this article, the author used AT89C51 microcontroller to make a simple running water lamp, focusing on the software programming method, in order to inspire microcontroller beginners and quickly become outstanding in the field of microcontrollers. Talent.
2.Hardware composition
According to the expansion and system configuration of the microcontroller system, the microcontroller application system can be divided into the smallest system, the smallest power consumption system and the typical system. AT89C51 microcontroller is a low-voltage, high-performance CMOS 8-bit microcontroller produced by ATMEL Corporation of the United States. It has rich internal resources: 4kB flash memory, 128BRAM, 32 I/O lines, 2 16-bit timer/counters, 5 vector two-level Interrupt structure, 2 full-duplex serial ports, with a voltage operating range of 4.25~5.50V and an operating frequency of 0~24MHz. There is no need to expand external memory when using the AT89C51 microcontroller. Therefore, this running water lamp is actually a minimum application system of a single-chip microcomputer with eight light-emitting diodes, that is, a single single-chip computer composed of light-emitting diodes, crystal oscillator, reset, power supply and other circuits and necessary software. Its specific hardware composition is shown in Figure 1.
Figure 1 Water lamp hardware schematic diagram
It can be seen from the schematic diagram that if you want LED1 connected to P1.0 port to light up, then you only need to change the level of P1.0 port to low level; on the contrary, if you want to connect it to P1.0 port If LED1 of the port goes out, the level of the P1.0 port must be changed to high level; similarly, the other seven LEDs connected to the P1.1~P1.7 ports are lit and extinguished in the same way as LED1. Therefore, to realize the running water lamp function, we only need to light up and extinguish the light-emitting diodes LED1~LED8 in sequence, and the 8 LED lights will turn on and off as a running water lamp. We should also note here that due to the visual persistence effect of the human eye and the short time it takes for the microcontroller to execute each instruction, we should delay for a period of time when controlling the diode to turn on and off, otherwise we will not see the "flowing water" It worked.
3.Software programming
The application system of the microcontroller is composed of hardware and software. After the above hardware schematic is built and powered on, we cannot see the phenomenon of the water lamp lighting up in cycles. We also need to tell the microcontroller how to work, that is, write a program to control the pins of the microcontroller. The level changes to realize the light-emitting diode turning on and off. Software programming is an important part of the microcontroller application system and is the focus and difficulty of learning microcontrollers. Next, we use the simplest running water lamp control function, which is to realize the cyclic lighting of 8 LED lights, to introduce several software programming methods to realize running water lamp control.
3.1 Position control method
This is a relatively stupid but easiest to understand method. It uses a sequential program structure and uses bit instructions to control the high and low levels of each bit of the P1 port to control the corresponding LED lights. The procedure is as follows:
ORG 0000H; after the microcontroller is powered on, it will be executed from address 0000H
AJMP START; jump to the main program storage address
ORG 0030H; Set the main program start address
START: MOV SP, #60H; set the stack starting address to 60H
CLR P1.0; P1.0 outputs low level to light up LED1
ACALL DELAY; call delay subroutine
SETB P1.0; P1.0 outputs high level to turn off LED1
CLR P1.1; P1.1 outputs low level to light up LED2
ACALL DELAY; call delay subroutine
SETB P1.1; P1.1 outputs high level to turn off LED2
CLR P1.2; P1.2 outputs low level to light up LED3
ACALL DELAY; call delay subroutine
SETB P1.2; P1.2 outputs high level to turn off LED3
CLR P1.3; P1.3 outputs low level, causing LED4 to light up
ACALL DELAY; call delay subroutine
SETB P1.3; P1.3 outputs high level to turn off LED4
CLR P1.4; P1.4 outputs low level to light up LED5
ACALL DELAY; call delay subroutine
SETB P1.4; P1.4 outputs high level to turn off LED5
CLR P1.5; P1.5 outputs low level, causing LED6 to light up
ACALL DELAY; call delay subroutine
SETB P1.5; P1.5 outputs high level to turn off LED6
CLR P1.6; P1.6 outputs low level, causing LED7 to light up
ACALL DELAY; call delay subroutine
SETB P1.6; P1.6 outputs high level to turn off LED7
CLR P1.7; P1.7 outputs low level, causing LED8 to light up
ACALL DELAY; call delay subroutine
SETB P1.7; P1.7 outputs high level to turn off LED8
ACALL DELAY; call delay subroutine
AJMP START; 8 LEDs flow once and then return to the label START for recycling
DELAY: ; Delay subroutine
MOV R0, #255; delay for a period of time
D1: MOV R1, #255
DJNZ R1, $
DJNZ R0,D1
RET; Subroutine returns
END; program ends
3.2 Circular shift method
In the previous program, we controlled each bit of the P1 port one by one, so the program seemed a bit complicated. Next, we used the cycle shift instruction and the cycle program structure for programming. We send a number to the P1 port at the beginning of the program. This number itself makes P1.0 low first and other bits high, and then delays for a period of time, then moves the data to the high bit, and then outputs it to the P1 port. This achieves the "flowing water" effect. Since the instructions of the 8051 series microcontroller only move the data in the accumulator ACC left or right, in actual programming, we should first put the data to be moved into the ACC, let it move, and then move the data in the ACC Then transfer it to the P1 port, so that the "flowing water" effect can also be achieved. The specific programming is as follows. The program structure is indeed much simpler.
ORG 0000H; after the microcontroller is powered on, it will be executed from address 0000H
AJMP START; jump to the main program storage address
ORG 0030H; Set the main program start address
START: MOV SP, #60H; set the stack starting address to 60H
MOV A, #0FEH; ACC is first loaded with the data that LED1 is on (binary 11111110)
MOV P1,A; send ACC data to P1 port
MOV R0, #7; move the data 7 more times to complete an 8-bit pipeline process
LOOP: RL A; Shift the data in ACC to the left by one bit
MOV P1, A; send the data moved by ACC to port p1 for display
ACALL DELAY; call delay subroutine
DJNZ R0, LOOP; not moving enough for 7 times to continue moving
AJMP START; after moving 7 times, jump to the beginning again to achieve a circular flow effect
DELAY: ; Delay subroutine
MOV R0, #255; delay for a period of time
D1: MOV R1, #255
DJNZ R1, $
DJNZ R0,D1
RET; Subroutine returns
END; program ends
3.3 Look-up table method
The above two programs are relatively simple water flow lamp programs. The "water flow" pattern can only achieve a single "left to right" flow method. The water flow lamp program written by using the lookup table method can realize water flow in any way, and the water flow patterns are unlimited. As long as the flow data of the water flow pattern data table is changed, the water flow patterns can be added or changed at will, and the water flow lamp effect can be truly realized as desired. We first build the data to be displayed in the pipeline pattern in a data table labeled TAB, and then fetch the data into the accumulator A through the table lookup instruction "MOVC A, @A+DPTR", and then send it to the P1 port for processing show. The specific source program is as follows. The data table at the TAB number can be modified arbitrarily according to the requirements to achieve the effect.
ORG 0000H; after the microcontroller is powered on, it will be executed from address 0000H
AJMP START; jump to the main program storage address
ORG 0030H; Set the main program start address
START: MOV SP, #60H; set the stack starting address to 60H
MOV DPTR, # TAB; send DPTR to the first address of the running pattern table
LOOP: CLR A; Accumulator cleared
MOVC A, @A+DPTR; take the value in the data table
CJNE A, #0FFH, SHOW; check the end of pipeline flag
AJMP START; After all patterns have flowed, the flow will be repeated from the beginning.
SHOW: MOV P1, A; send data to port P1
ACALL DELAY; call delay subroutine
INC DPTR; Get the data table pointer to point to the next data
AJMP LOOP; continue to look up the table to retrieve data
DELAY: ; Delay subroutine
MOV R0, #255; delay for a period of time
D1: MOV R1, #255
DJNZ R1, $
DJNZ R0,D1
RET; Subroutine returns
TAB: ;The following is the flow pattern data table. Users can write it as required.
DB 11111110B; Binary representation of running pattern data, left shift from low to high
DB 11111101B
DB 11111011B
DB 11110111B
DB 11101111B
DB 11011111B
DB 10111111B
DB 01111111B
DB 01111111B; Binary representation of running pattern data, shifted right from high to low
DB 10111111B
DB 11011111B
DB 11101111B
DB 11110111B
DB 11111011B
DB 11111101B
DB 11111110B
DB 0FEH, 0FDH, 0FBH, 0F7H; Pipeline pattern data expressed in hexadecimal
DB 0EFH, 0DFH, 0BFH, 7FH
DB 7FH, 0BFH, 0DFH, 0EFH
DB 0F7H, 0FBH, 0FDH, 0FEH
…
DB 0FFH; Streaming pattern end flag 0FFH
Previous article:DS18B20 digital thermometer C language source program
Next article:LED flexible LED tube assembly source code
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Problems encountered when burning Flash to DSP28335
- Having trouble with impedance matching? Here’s the secret (Part 1)
- HuaDa MCU Design Considerations Flash and Serial Programming Application Notes
- Playing with Zynq Serial 35——Online board debugging based on Vivado Virtual IO
- Urgently looking for mid-level surveying/mapping, electrical, mechanical engineer technicians
- Does anyone have a simulation circuit diagram of the LMD18200 chip in proteus?
- Does turning on Bluetooth and NFC for 24 hours consume power?
- 【FAQ】Implementing MultiZone Security in RISC-V Applications | Microchip Security Solutions Seminar Series
- Overview of IoT Gateway and Its Role
- TI - MCU - MSP430 User Guide 8 -> WDT Watchdog Module