On the Problem of Interrupt Context Protection of PIC Microcontroller

Publisher:创新思绪Latest update time:2013-03-04 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:On the Problem of Interrupt Context Protection of PIC Microcontroller

Previous article:Introduction to PIC Microcontroller Instruction Set
Next article:Control system of brushless DC motor based on dsPIC30F6010

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号