51 Interrupt Function Study Notes

Publisher:Amy啊111111Latest update time:2016-09-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Explanation: void Timer0() interrupt 1 using 1

Timer0 is a function name, just randomly chosen 

interrupt xx using y 

  The xx value after interrupt is the interrupt number, which means the interrupt port number to which this function corresponds. It is usually in 51. 

  0 External interrupt 0   

  1 Timer 0 

  2 External interrupt 1 

  3 Timer 1 

  4 Serial interrupt 

In fact, when compiling, the entry address of your function is set to the jump address of the corresponding interrupt. 

  Using y The y here refers to the register group used by this interrupt function. There are generally 4 groups of registers r0-r7 in 51, a total of 32. If your terminal function and other programs do not use the same register group, the register group will not be pushed into the stack when entering the interrupt, and it will not be recalled when returning, saving code and time.

Note the following about the MCS-51 interrupt function:
(1) Interrupt functions cannot pass parameters. If an interrupt function contains any parameter declaration, a compilation error will occur.
(2) Interrupt functions do not have a return value. If you attempt to define a return value, you will not get the correct result. It is recommended to define the interrupt function as a void type to clearly indicate that there is no return value.
(3) Under no circumstances can an interrupt function be called directly, otherwise a compilation error will occur. This is because the return of the interrupt function is completed by the RETI instruction of the 8051 microcontroller, which affects the hardware interrupt system of the 8051 microcontroller. If the interrupt function is called directly without an actual interrupt, the operation result of the RETI instruction will result in a fatal error.
(4) If other functions are called in the interrupt function, the registers used by the called function must be the same as those of the interrupt function. Otherwise, incorrect results will occur.
(5) When the C51 compiler compiles the interrupt function, it will automatically add corresponding content at the beginning and end of the program, as follows: ACC, B, DPH, DPL, and PSW are pushed onto the stack at the beginning of the program and popped off the stack at the end. If the interrupt function does not have the using n modifier, R0~R1 must be pushed onto the stack at the beginning and popped off at the end. If the interrupt function has the using n modifier, the working register bank select bit in PSW must be modified after PSW is pushed onto the stack.
(6) The C51 compiler generates an interrupt vector from the absolute address 8m+3, where m is the interrupt number, which is the number after interrupt. The vector contains an absolute jump to the entry address of the interrupt function.
(7) The interrupt function is best written at the end of the file, and the extern storage type declaration is prohibited. Prevent other programs from calling it.

Reference address:51 Interrupt Function Study Notes

Previous article:The code keyword in C51
Next article:About the data types of data, idata, xdata, pdata, and code in Keil microcontroller programming

Latest Microcontroller Articles
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号