In program design, setting a good clock interruption can greatly facilitate and simplify program compilation and improve system efficiency and operability. The following takes the 89C51 system with a 6MHz clock as an example to illustrate the application of clock interruption.
Timer initial value and interrupt cycle. The time interval of clock interrupt is generally 20ms (50Hz). If a time base signal of one hundredth of a second is required, 10ms (100Hz) can be used. Here we take 20ms. T0 works in 16-bit timer mode (mode 1). After each machine cycle, T0 automatically adds 1. When it reaches the next machine cycle of 0FFFFh, T0 overflows and generates an interrupt. The hardware sets the corresponding flag bit for software to query. That is, after N+1 machine cycles after the interrupt is started, T0 generates an interrupt. Therefore, as long as a number N less than 0FFFFh is stored in T0 first, and then the timer is started, an interrupt will be generated after N+1 machine cycles. This number is the so-called "initial value". If the clock is 6MHz, there are 10,000 machine cycles in 20ms. (10000)10=(2710)16, then 0FFFFh-2710h+1=0D8F0h. Since it takes 7-8 machine cycles to respond to interrupt, protect the scene and reload the initial value, the initial value that should be loaded into T0 is 0D8F7h. After each interrupt is entered, the values of A and PSW are first stacked, and then 0D8F7h is loaded into T0.
Set a unit, and add 1 for each interrupt. Name a unit in RAM INCPI. In the interrupt processing program, after loading the initial value of T0, use the "INC INCPI" instruction to add 1 to it. Whether it is an interrupt program or a main program, you can read any integer multiple of 20ms between 1 and 256 from INCPI. For example, if the program for sending display to the digital tube needs to refresh the display every 0.5 seconds, you can set a waiting unit W_DISP, use the three statements "MOV A, INCPI/ADD A, #25/MOV W_DISP, A" to make it 25 greater than the current INCPI value, and then check whether W-DISP is equal to the INCPI value in each interrupt. If they are equal, it means that 25 interrupt cycles have passed, execute the display sending program, and let W_DISP add 25 and wait for the next 0.5 seconds. Multiple waiting units can be set in the program to obtain multiple time base signals.
Read keys and watch in interrupts. Usually, we read the keyboard in the main program, the steps are: scan the keyboard, if a key is pressed, delay for tens of milliseconds to debounce, confirm again that the key is indeed pressed, and then process the work corresponding to the key, and repeat the above steps again after completion. This method has two shortcomings: 1. The key input cannot be latched when processing the corresponding work, and the key may be missed. 2. The CPU cannot do other things during the delay debounce, which is not efficient. Putting the key reading in the clock interrupt can avoid the above shortcomings. The method is: if the same key is read as pressed in two adjacent interrupts, then this key is valid (the debounce purpose is achieved), latch it into the first-in-first-out (queue) keyboard buffer, and wait for the main program to process it. In this way, the main program can still respond to keyboard input while processing the key. The buffer depth can usually be set to 8 levels. If the number of latched keys has reached 8, the new key is ignored, and an alarm is issued to remind the user that the new key will be invalid. If the keyboard buffer queue stagnates for much longer than the maximum time required for the main program to process a key press, it means that the main program has made an error or run away. In this case, you can use an instruction in the interrupt to reset the system, thus fulfilling the purpose of a watchdog.
Delay in the main program. Since there is a normally open clock interrupt, when a shorter delay with higher precision is required in the main program, the clock interrupt should be temporarily turned off. When a longer delay with lower precision is required in the program, the following example can be used to avoid multi-layer nested loop delays.
Example: Output a 1-second high-level pulse from P1.1 port
MOV A,INCPI
INC A
CJNE A,INCPI,$; Wait for an interrupt to complete
SETB P1.1; Set P1.1 to 1, pulse starts
ADD A,#50; 50 20ms is 1 second
CJNE A,INCPI,$; Wait for an interrupt to increase INCPII by 50 times
CLR P1.1; Set P1.1 to 0, pulse ends
In summary, we can know that the clock interrupt should be used flexibly in the design, and the tasks should be reasonably allocated to the interrupt and the main program. The two should have clear division of labor and simple interface. And we should pay attention to shortening the execution time of the interrupt handler as much as possible, and it should not be longer than 20ms.
Pulse constant current source with variable frequency and duty cycle controlled by 89C2051
The load of this constant current source is purely resistive, with a resistance range of 100Ω~1kΩ, and an output constant current of 4mA. The constant current source provides three outputs, and the frequency of each output pulse constant current source is 12 times/min, 6 times/min, and 3 times/min, respectively, and the output pulse width is 300ms.
The constant current source is mainly composed of two parts: the constant current source output circuit and the pulse control signal generation circuit with variable frequency and duty cycle.
The above pulse control signal can also be obtained by frequency division using TTL or CMOS circuit, but the circuit is more complicated, and the frequency and duty cycle are not easy to change. Using the output port of 89C2051 as the pulse control signal, the frequency and duty cycle of the pulse control signal can be changed through the program.
The pulse constant current source circuit is shown in the attached figure. For the convenience of explanation, only one constant current source output circuit is drawn here.
Connector P1 is connected to the 9V battery, and P3 is connected to the load.
The constant current source output circuit is composed of U1, T1, T2, D1 and other peripheral circuits. T1, R3, D1, RW1 and R2 constitute a reference voltage source. According to the design requirements, the reference potential added to the in-phase terminal of the op amp is determined by the output current and the resistance value of the sampling resistor R1. According to the parameters in the figure, when T1 is turned on, RW1 is adjusted to make the reference potential of the in-phase terminal of the op amp 0.4V. Obviously, when T1 is turned off, the reference potential is equal to 0V. When T1 is turned on, if the load current is lower than 4mA, the voltage on the sampling resistor R1 drops, the potential of the inverting input terminal of the op amp drops, the output potential of the op amp rises, and the base potential and collector current (load current) of T2 rise; when the load current is greater than 4mA, the base potential and collector current (load current) of T2 drop, and finally the output current is kept constant. When T1 is turned off, since the reference potential added to the in-phase terminal of the op amp drops to 0V, the output voltage of the op amp drops, T2 is turned off, and no current passes through the load.
The pulse signal that controls the conduction and cutoff of T1 is output from the P10 port of 89C2051. The software that generates the pulse control signal mainly consists of a loop program and a delay subroutine.
If you want to expand the output current range, you only need to use a relatively high-power voltage regulator and replace T2 with a high-power composite tube.
AT89C series MCU encryption mode
The simple decryption of the microcontroller is to erase the encryption lock bit in the microcontroller. Due to the unreasonable design of the erasing operation timing of the AT89C series microcontroller, it is possible to erase the encryption lock bit before erasing the on-chip program. The timing of the erasing operation of the AT89C series microcontroller is: erase start ----> erase operation hardware initialization (10 microseconds) ----> erase the encryption lock bit (50-200 microseconds) ---> erase the data in the on-chip program memory (10 milliseconds) -----> erase end. If the program is used to monitor the erasing process, once the encryption lock bit is erased, the erasing operation is terminated, and further erasing of the on-chip program memory is stopped. The encrypted microcontroller becomes an unencrypted microcontroller. The on-chip program can be read out through the bus. There are two unbreakable encryption methods for the AT89C series microcontroller.
1. An encryption method that permanently destroys the encryption bits of the microcontroller. It is referred to as OTP encryption mode.
2. A method to permanently destroy the encryption of the microcontroller's data bus, referred to as the burn bus encryption mode.
AT89C series MCU OTP encryption mode principle
This programming encryption algorithm burns out the encryption lock bit (breaks through the silicon chip inside the chip) without damaging other parts and does not occupy any resources of the microcontroller. After the encryption lock bit is burned out, it no longer has the erase feature. The 89C51/52/55 has 3 encryption bits to further increase the reliability of encryption. Once encrypted in OTP mode, the encryption bit in the microcontroller chip and the data in the program memory cannot be erased again. The 89C51/52/55 microcontroller is like a one-time programmed OTP type microcontroller. If the user program length is greater than the capacity of the 89C51 microcontroller's on-chip memory, the OPT mode can also be used for encryption. The specific method is as follows:
1. Expand a large-capacity program memory as usual, such as 27C512 (64K).
2. Arrange the key program part in the first 4K of the program.
3. Write the entire program into 27C512, and then fill the first 4K of 27C512 with 0.
4. Fix the first 4K of the program into AT89C51 and encrypt it in OPT mode.
5. Connect the EA pin of the microcontroller to a high level.
In this way, the first 4K of the program runs inside the microcontroller, and the last 60K runs outside the chip. Pirates cannot read the first 4K of the program, and even if they know the last 60K, it will be useless.
AT89C series single chip microcomputer bus encryption mode principle
Because the program code in the microcontroller chip must eventually be read out through the data bus, if one of the lines of the data bus that guides the microcontroller is permanently damaged, the decryptor cannot read the correct code of the program in the chip even if the encryption bit is erased. The data bus of 89C1051/2051 is P1 port. The bus burning mode burns the P1.0 port of 89C2051. The original program code is 02H, 01H, 00H. The read data is 03H, 01H, 00H. The lowest bit is always 1, and the program code read out is obviously wrong. This encryption mode is used to encrypt 89C1051/2051 microcontrollers. The disadvantage is that it occupies the resources of the microcontroller. When designing the microcontroller hardware system, the development and design personnel only need to reserve the export line P1.0 for use, and then use the bus burning mode to encrypt the microcontroller.
Previous article:Using the MCU serial port to realize the display of multiple LEDs
Next article:Application of Single Chip Microcomputer in Furnace Temperature Control
Recommended ReadingLatest update time:2024-11-16 13:47
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How to choose a replacement battery for the Dyson V6 vacuum cleaner?
- CY8CKIT-149 PSoC 4100S PLUS PROTOTYPING KIT Unboxing and Power-On Run Example
- B-U585I-IOT02A Bluetooth communication problem
- Super detailed teaching you how to use HFSS to design and simulate inverted F antenna 2
- How does an intelligent fully automatic multimeter measure capacitance?
- PCB current and signal integrity design
- micropython update: 2020.6
- 【DSP】TMS320F28035 SCI routine (self-transmission and self-reception + query)
- The reverse withstand voltage peak of the secondary winding rectifier tube D10 reaches -142V. How can I adjust the RCD parameter value to reduce it?
- C6678 multi-core application cache test