In the development guide (1), we introduced the overall framework of the sample program. Starting from this chapter, we will introduce the modules used in detail. In this chapter, we mainly explain a simple usage of key interrupt, refer to chapters 6 and 7 of the application manual.
02 key interrupt
Because external interrupts and key interrupts are multiplexed on GPIO (KEY0~KEY6 corresponds to P00~P06), GPIO should be set as an input state first, and GPIO, external interrupts and key interrupts are all peripheral functions, so the first step is to First enable the corresponding peripheral clock, then initialize the state of GPIO00~06 as input pull-up, then initialize the key interrupt configuration, and finally enable the key interrupt, as shown in Figure 1 below:
Figure 1. Key interrupt configuration initialization
When the key interrupt is initialized, first write the correct vector number, enable the interrupt corresponding to the vector number, and then configure the interrupt priority and priority threshold according to the application needs. Generally, when there are multiple interrupts in the program, the interrupt priority needs to be considered. In this sample program SD93F115-JBS_TEST.zip, the interrupt priority and threshold are set to the highest (because we select the test to be executed by pressing the interrupt item, so we think that the priority and threshold of the key interrupt must be the highest), after the configuration is completed, you can enable the key interrupt and set the key KEY0~KEY6 as key input.
After the basic configuration of the key interrupt is completed, it is necessary to write the key interrupt service function. We find the interrupt function entry corresponding to the key interrupt in SD931X_it.c of lib, and write the key interrupt service function (other interrupts can be written in the corresponding interrupt function), as shown in Figure 2 below:
Figure 2. Button interrupt service function
Enter the key interrupt service function, the first step is to clear the interrupt flag bit, to avoid the system repeatedly entering the interrupt due to forgetting to clear the flag bit (no matter which key in KEY0~6 triggers the interrupt, the KEYIF flag bit will be set); the second step It is to debounce to prevent false triggering of keys (LCD display will be explained in subsequent chapters); the third step is to judge which key or keys are pressed by reading the input data register GPIO0_IDR of GPIO0 (because the IO It is pulled up, so when no button is pressed, the register result should be 0x7F, and the corresponding bit is 0 when a button is pressed).
If the result of the register is the same as the defined key value Key0-Key6 (see the key value definition in Figure 3), the flag position of the corresponding test item will be 1, and the flag position of other test items will be 0, otherwise the program will directly exit the interrupt. After the execution of the button interrupt is completed, the program will jump out of the interrupt and continue to execute the while loop in Figure 4. In the loop, we verify whether a button is pressed by continuously monitoring whether the flag bit of each test item is set to 1, so that we can pass a simple Key operation to enter different test items.
Figure 3. Key value definition
Figure 4. while loop
03 Brief analysis of development problems
The most common problem encountered in the development stage when using key interrupts or other interrupts is that the interrupt does not respond. It is necessary to focus on whether the peripheral clock is turned on, whether the interrupt enable is turned on, whether the corresponding analog function is turned on, and whether the interrupt flag bit is judged correctly.
This article introduces a simple usage of key interrupt, and briefly introduces the process of key interrupt initialization and interrupt service function writing.
Reviewing editor: Tang Zihong