For complex calculations, the speed of the microcontroller is too slow. The best way is to manually calculate all the results in advance, store them in ROM in sequence, and then directly check the results from ROM when the program is executed.
The 51 MCU has two table lookup instructions:
MOVC A, @A + PC
MOVC A, @A + DPTR
The first one can avoid DPTR, but the "offset" is quite difficult to calculate because it involves the basic concept of instruction byte number and table lookup, which is a headache for many people.
In response to this problem, Zuoerlundao has developed a method to automatically calculate the offset, which can avoid the tedious manual calculation process.
The following is an online question that was answered by Doerlundao:
The data pre-stored in 30H ~ 39H are 1, 3, 5, 7, 9, 2, 4, 6, 8, 10.
Use MOVC A, @A + PC instruction to write a square table lookup program to calculate the square of the number in 30H ~ 39H on the chip and send it to 40H ~ 49H unit.
The program written by erlundao is as follows:
;-------------------------------------------
org 0000h
mov 30h, #1 ; store some data first
mov 31h, #3
mov 32h, #5
mov 33h, #7
mov 34h, #9
mov 35h, #2
mov 36h, #4
mov 37h, #6
mov 38h, #8
mov 39h, #10
call SQRT ;Call subroutine to find the square of the above 10 numbers
;The unit part starting at 40H is omitted
sjmp $
;-------------------------------------------
;Subroutine to find the square using table lookup method
SQRT:
MOV R0, #30H ;Source value starting address
MOV R1, #40H ;Destination starting address
MOV R2, #10 ;Total 10 numbers
LOOP:
MOV A, @R0
ADD A, #S_TAB - ($ + 3) ;Automatically calculate the offset
MOVC A, @A + PC ;Look up the table and find the square
MOV @R1, A
INC R0
INC R1
DJNZ R2, LOOP
RET
;-------------------
S_TAB:
DB 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196
;-------------------------------------------
;End
The questioner's comment on the answer: Yes, that's correct, but what does #S_TAB - ($ + 3) mean?
========================================
Algorithm description:
At the beginning of S_TAB, a series of "Square Numbers" is stored using the DB pseudo-instruction. - Baidu's layout is very poor, the quotation marks are displayed too small, and the book title marks are used instead.
So, counting from S_TAB, the 0th byte is 0 squared, the 1st byte is 1 squared, and so on, the 14th byte is 14 squared.
If the PC in the table lookup instruction MOVC A, @A + PC is equal to S_TAB, then we can find the square of A from the table.
Haha, this is a bit like "Standing Long Jump". You stand on the springboard and jump as far as your strength allows.
In fact, when executing the table lookup instruction MOVC A, @A + PC, PC is not equal to S_TAB.
What to do? Just think of long jump, add the run-up and you'll be done. Everyone should be able to calculate the number of steps or meters for the run-up.
Before executing the table lookup instruction, calculate the difference between S_TAB and the PC when executing the table lookup instruction, and add it to A.
This number, called the offset, is added to A, which is equivalent to the "run-up".
To calculate this offset, we need to count the number of bytes between S_TAB and the PC when the table lookup instruction is executed.
If you are not familiar with machine language, it is quite difficult to calculate the offset manually.
Moreover, if the instructions are modified, they have to be recalculated, which is very troublesome.
Fortunately, Zuoerlundao has invented a method to automatically calculate the offset.
In the program, the following two lines:
ADD A, #S_TAB - ($ + 3) ; 2 bytes
MOVC A, @A + PC ; 1 byte
That is, the work of automatically calculating the offset and looking up the table is completed.
The calculation formula in the program is: #S_TAB - ($ + 3), which can also be written as: #S_TAB - $ - 3.
The following is a description of each part:
$: represents the current address where the ADD instruction is located;
$ + 3: the PC value when executing the table lookup instruction, plus 3, which means these two instructions occupy a total of three bytes;
S_TAB - ($ + 3): Calculate the difference between the table head address and the address when looking up the table;
According to the method introduced in the microcontroller textbook, using the MOVC A, @A + PC instruction requires manual calculation of the "difference between the address of the table lookup instruction and the beginning of the table."
Anyone who has used this method knows how difficult it is.
Therefore, most people would rather waste a DPTR than calculate the difference.
The formula developed by Zuo Er Lun Dao has both theory and practical methods, which greatly facilitates microcontroller programmers and is worthy of wide promotion.
Previous article:Use the timer interrupt of the microcontroller to periodically output a rectangular wave
Next article:52 MCU timer/counter programming summary
Recommended ReadingLatest update time:2024-11-16 21:26
- 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
- EEWORLD University Hall ---- Computer Vision Course Tsinghua University Wang Mingzhe
- Download to get a gift! Lots of exquisite gifts are waiting for you!
-
EEWORLD University Hall----Live Replay:
takes you to learn about ADI's digital health biosensor series - Switching power supply using microcontroller for loop control
- Carrier synchronization
- Popular Science Sharing - Detailed Explanation of the Working Principles of Starlink Satellites (Episode 3)
- [Recruitment] Shenzhen foreign companies are urgently recruiting RF antenna engineers, looking forward to your joining
- No more confusion! A guide to choosing a cordless vacuum cleaner
- How to Go from Beginner to Expert in FPGA
- "51 MCU Easy to Get Started Based on STC15W4K Series" 429 pages HD electronic version