During the interrupt service subroutine, only the return address PC is automatically pushed into the stack, and the contents of other registers must be figured out by ourselves according to the needs of the program. The PIC microcontroller assembly language does not have the PUSH POP instruction like the 51 series microcontroller, so we need to use a program to implement a similar function.
Because a program is used to implement the scene protection, it may affect the W and STATUS registers, so we have to protect these two registers first when protecting the scene. These scene protections do not save the data in the chip stack. Instead, they are stored in RAM. I personally think that these data should be saved in RAM that is independent of the body address. That is to say, PIC generally has four bodies. No matter which body is currently in which the RAM data is changed, it can be mapped to other bodies. I think this is more convenient in terms of programming and avoids some disadvantages and troubles. I am also new to PIC. If there is anything wrong with what I said, please let me know and we can discuss it together. Thank you in advance:)
The following is a sample program for interrupt context protection provided by the manufacturer: Save the contents of the W, STATUS, and PCLATH registers to the temporary backup register
(1) MOVWF W_TEMP copies W to its temporary register W_TEMP
(2) SWAPF STATUS,W Swap the high and low four bits in STATUS and put them into W
(3) CLRF STATUS Regardless of the current state, set state 0 as the current state
(4) MOVWF STATUS_TEMP saves STATUS to a temporary register on bank 0
(5) MOVF PCLATH,W copies the contents of register PCLATH to W
(6) MOVWF PCLATH_TEMP saves PCLATH to a temporary register
(7) CLRF PCLATH Set PCLATH to 0 regardless of the current page.
............(The core part of the interrupt service subroutine)
(8) MOVF PCLATH_TEMP,W transfers through W
(9) MOVWF PCLATH restores PCLATH
(10)SWAPF STATUS_TEMP,W put back after swapping
(11)MOVWF STATUS
(12)SWAPF W_TEMP,1
(13)SWAPF W_TEMP,0[page]
Let's look at the previous program. One thing we need to point out is that some of the PIC assembly statements will change the data in the STATUS register during execution, so it is forbidden to use statements that can change STATUS before protecting STATUS.
Statement 1 is used to protect the W register. Since the MOVWF statement does not change STATUS, the PIC can be used directly. Data transfer is basically transferred through W, so the data in W is first protected to a temporary backup and then restored when the interrupt returns.
Statement 2: used to protect STATUS. Since the MOVF instruction changes STATUS, it cannot be used directly here.
MOVF STATUS,W statement, but used SWAPF instruction. Here we did not apply its high and low nibble swap function, but applied that the execution of this statement has no effect on the STATUS register, so when the interrupt is restored, we still need to use SWAPF to reverse the high and low nibbles:)
Statements 3 and 4: At this time, we have reversed the low and high bits of the STATUS content and put them into the W register, so we can safely change the content of STATUS at this time. These two sentences put the content of STATUS into a temporary register. Another point is that before restoring STATUS, we don't mind using instructions to change the content of STATUS. Once restored, we still can't use instructions to change STATUS until the interrupt returns to the main program.
Statements 5, 6 and 7 are to protect PCLATH. Actually, for some small programs, this does not need to be protected. But for the sake of program standardization and to prevent PCLATH from becoming an unpredictable data due to some factors, we still protect it here. Because our interrupt service program is in body 0, PCLATH is cleared to 0 after protection.
Statements 8 and 9 are instructions for restoring PCLATH, so I won't go into details here.
Statements 10 and 11, as we have said before, to restore the value of STATUS when the interrupt returns, the high and low bits need to be reversed and then placed in the STATUS register. These two statements do not change the status bit, so you can use them with confidence.
Statements 12 and 13: Since STATUS has been restored before, we must be very careful not to affect STATUS when restoring W. Therefore, two SWAPF statements are used here. The negative is positive in the byte swap, and the status bit has not been affected.
That’s all for today. Friends are welcome to give some opinions and suggestions.
Previous article:Introduction to PIC Microcontroller Instruction Set
Next article:Control system of brushless DC motor based on dsPIC30F6010
- Popular Resources
- Popular amplifiers
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
- Do all voltage rails need to use low quiescent current? Here's the answer!
- How Carrier Networks Will Achieve 5G 《Vintage Multi-Carrier Technology》
- Mentor Use
- Analysis of the working principles of seven triode collector DC circuits 2
- Circuit
- TMS320F28035 call jump instruction problem
- Sliding resistor for LM358 comparator
- TE pressure sensor: underwater robots from 0 to 1000, how to avoid mass production pitfalls
- OHM Graphical Introduction to Electronics
- Detailed explanation of the design of RFID-based smart hotel system