PIC microcontroller interrupt program design skills

Publisher:诚信与爱Latest update time:2012-12-18 Source: 21IC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:PIC microcontroller interrupt program design skills

Previous article:PIC Microcontroller Study Notes
Next article:PIC Microcontroller Basics

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
Guess you like

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号