Design Skills of PIC Microcontroller Pin Interrupt Program
[Copy link]
1 Briefly describe all mid-range PIC microcontrollers. When the highest 4 pins (RB7~RB4) of the PORTB port 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 the 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 indispensable 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. 2 Pin interrupt program design First set the relevant registers in the main program. ◇ Set the TRISB register to make the pins related to RB7~RB4 in the input state; ◇ If weak pull-up is required, set it through the 7th bit of OPTION_REG; ◇ RBIF=O; ◇ RBIE=1; ◇ GIF=1.
Respond to the interrupt service program after the state change. ◇ Check whether RBIF is 1. If it is 1, the interrupt is caused by the pin change; ◇ Call the delay program, delay 20~30 ms, the purpose is to debounce the key; ◇ Determine whether the interrupt is caused by the rising edge or the falling edge of the pin; ◇ Call the key processing program; ◇ Read the value of the PORTB port and cancel the hardware signal of the state change; ◇ Clear the 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 an 8-layer hardware stack, and it is very easy to overflow the stack when calling the program in the interrupt. In addition, the PIC microcontroller has only one interrupt program. When responding to an interrupt request, the PIC microcontroller will automatically clear the global interrupt enable bit (GIF, bit 7 of INTCON), so that other interrupts cannot be responded to temporarily (at this time, if an interrupt request is issued by another interrupt, the flag bit will be retained), and it will not be responded to until this 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 interrupts.
The following is the author's idea 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 interrupt caused by the change we need. If so, do not delay here, but set a flag bit, then clear the interrupt flag and exit the interrupt. The interrupt program 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; //The key flag is set } RBIF=0; //Clear the 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 the key.
First, set a lms time base flag "SYSlms" in the timer interrupt. Every lms, "SYSlms" is set. The program 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 the 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-release and self-disappearance. Define the following macro: bit SYSTime; #defincTimeEnahle()SYSTime=0, if(SYSlms){SYSTime=1; SYSlms=0;)
You can put TimeEnable() anywhere in the main program dead loop. Whenever the program executes this macro, SYSTime will be cleared to zero, which is the self-disappearance of the flag. If the time base flag SYSlms of the timer is already set, SYSTime will be set to 1, so that other programs can use this time message, which is the self-release of the message. The following is to use this time message to perform key delay and 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 for a delay of 30 ms KeyTask++; //Prepare for the next task kcy=0; } break; case I: KeyTime--; //Delay 30 ms if(KeyTime==0)Key+ask++; break; case2;if(RB4==o){ //Call key processing program KeyTask=0; } else KeyTask=0; //Exit task break; } }
Use it like this in the endless loop of the main program: while(1){ TimeEnable(); If(SYSTime==1){scankey();) //Other programs can be added here
to execute the key scanning program only when there is a time message. It can be seen that when the scanning program is executed 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. 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 determined 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 heap overflow, which improves the real-time performance of the system.
|