For all mid-range PIC microcontrollers, when the top 4 pins (RB7~RB4) of PORTB are set to input mode, when the input level changes from high to low or from low to high, the microcontroller can generate an interrupt. This is commonly known as a pin state change interrupt.
There are three points that need special attention when designing the pin interrupt program. First, before clearing the P0RTB interrupt flag RBIF, an essential read operation instruction with the PORTB port data register PORTB as the source register must be arranged. The purpose of placing this instruction is sometimes not just to read useful data, but to cancel the hardware signal of the state change, so as to successfully clear the RBIF flag and prepare for the next interrupt. Second, since port PORTB is a pin electronic change interrupt, that is, an interrupt request will be generated regardless of the rising edge or falling edge of the pin, so unnecessary false interrupts must be handled properly. Third, the pin function of the PIC microcontroller is generally used to detect the key, so the key debounce problem must be handled properly.
Pin interrupt programming
First set the relevant registers in the main program.
◇Set the TRISB register to make the pins related to RB7~RB4 in input state;
◇If weak pull-up is required, set it via bit 7 of OPTION_REG;
◇RBIF=O;
◇RBIE=1;
◇GIF=1.
Respond to the interrupt service routine after the state change.
◇ Check if RBIF is 1, if it is 1, it is an interrupt caused by pin change;
◇Call the delay program, delay 20~30 ms, the purpose is to debounce the key;
◇Determine whether the interrupt is caused by a rising edge or a falling edge of the pin;
◇Call key processing program;
◇Read the value of PORTB and cancel the hardware signal of state change;
◇Clear RBIF flag.
The author believes that the biggest problem with the above program design is calling the delay program in the interrupt program. As we all know, the mid-range PIC microcontroller has only 8 layers of hardware stack, and it is very easy to cause stack overflow when calling the program in the interrupt. In addition, the PIC microcontroller interrupt program has only one population. When responding to the interrupt request, the PIC microcontroller will automatically clear the global interrupt enable bit (the 7th bit GIF of INTCON), so that other interrupts cannot be responded to temporarily (at this time, if other interrupts send interrupt requests, the flag bit will be kept), and will not be responded until the interrupt program exits. This requires us to design the interrupt program as short as possible, avoid calling subroutines, and do not perform complex calculations in the interrupt.
The following is the author's thinking when designing the program.
When the pin status changes and causes an interrupt, first determine in the interrupt subroutine whether the cause of the interrupt is the change we need. If so, do not delay here, but set a flag, then clear the interrupt flag and exit the interrupt. The interrupt procedure is as follows:
else if((RBIE&RBlF)==1){ //If the pin changes and causes an interrupt
if(RB4==0){ //The button on RB4 is grounded
key=1; //button flag position
}
RBIF=0; //Clear pin interrupt flag
}
Among them, the if(RB4==0) statement is equivalent to reading the PORTB port data register and canceling the hardware signal of the state change.
The following is a detailed introduction on how to debounce keys. [page]
First, set a 1ms time base flag "SYSlms" in the timer interrupt. Every 1ms, "SYSlms" is set. The procedure is as follows:
unsigned char count;
if((ToIE&TOIF)==1){ //Timer interrupt
TMRO+=0x09; //Interrupt every 250μs
if(count==4){
count=0;
SYSlms=l; //System time flag
couot++;
}
T0IF=0; //Clear clock interrupt flag
}
With this time base, you can perform key debounce processing in the main program. In order to make better use of this time base, define a message flag SYSTime, which I call time message. In order to make this message have the function of self-publishing and self-disappearing, define the following macro:
bit SYSTime;
#defincTimeEnahle()SYSTime=0, if(SYSlms){SYSTime=l;SYSlms=0;)
You can put TimeEnable() anywhere in the main program's infinite loop. Whenever the program executes this macro, SYSTime will be cleared, which is the self-disappearance of the flag. If the timer time base flag SYSlms is already set, SYSTime will be set to 1, so that other programs can use this time message, which is the self-publishing of the message. The following is to use this time message to perform key delay debounce. First, let's take a look at the key scanning subroutine;
void seaakey(){
unsigned char KeyTime, KeyTask; //Define task time parameters,
//Task parameters
switch(KeyTask){
case0:if(key){
KeyTime=30; //Prepare delay 30 ms
KeyTask++; //Get ready for the next task
kcy=0;
}
break;
case I: KeyTime--; //Delay 30 ms
if(KeyTime==0)Key+ask++;
break;
case2;if(RB4==o){
//Call the key handler
KeyTask=0;
}
else KeyTask=0;//Exit task
break;
}
}
Use it in the endless loop of the main program like this:
while(1){
TimeEnable();
If(SYSTime==1){scankey();)
// You can add other programs here
The key scanning program is executed only when there is a question message. It can be seen that when entering the scanning program for the first time, the program first determines whether the key flag is set. If it is set (that is, a key is pressed), the task time parameter (KeyTime) is assigned a value of 30, which is a delay of 30ms and debounce. Of course, you can also set it to other time values; at the same time, the task parameter (KeyTask) is increased by 1. After 1ms, the scanning program is entered again. At this time, the scanning program executes the statement of case1. In this way, after 30 times (delayed by 30ms), the task parameter (KeyTask) is increased by 1 and the value is 2. After 1ms, the scanning program is entered again and the statement of case 2 is executed. First, it is judged again here whether the key is still pressed. If it is, the key processing program is called. If not, the key scanning program is exited. Here, you can also add a judgment program to determine whether the key is lifted.
The pin change program designed in this way has low CPU overhead and high efficiency, and will not have the problem of shallow stack overflow, thereby improving the real-time performance of the system.
Previous article:PIC Microcontroller Study Notes
Next article:PIC Microcontroller Basics
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