Catalog
I. Preface
In the last blog, we talked about initializing GPIO as an output function to drive LED s or other peripherals. In this blog, we will continue to talk about the other remaining function of GPIO - input function
Since it is a smart device, it needs to interact with the outside world. There are many ways to get outside information.
Access through the Internet is a way to obtain information.
It is also a way to obtain information through its own sensor or button switch. The output signal is the switching value
Access to information through the Internet, our blog after the talk, first not urgent.
Today, we will talk about how to use the input function of GPIO to obtain external switch signals.
2, Programming
There are two ways to obtain switch signal by GPIO input function, one is query method, the other is interrupt method. We will explain them in turn
1. Inquiry method
As the name implies, the query method is to constantly query the status of GPIO. No matter whether the switch is pressed or not, I will query what I find. When I don't query, you press it and it's useless. The real-time performance of this method is relatively low.
The specific use method is as follows
local function GPIO_Input_init() local KEY1 = pins.setup(27, nil, pio.PULLUP)--take GPIO27 Initialize as input pull-up local LED1 = pins.setup(64, 0)--take GPIO64 Initialize as output while true do if KEY1() == 0 then--If KEY1 Grounded, then on LED1 LED1(1) else LED1(0)--Otherwise extinguish LED1 end sys.wait(1000)--Query once a second end end
The query method is relatively simple to use, and it needs to be queried constantly
If the delay is too short, there will be frequent task switching, low execution efficiency, and the cpu has been busy querying.
If the delay time is too long, the real-time performance will be low. For example, if my code key1 is connected and I'm unlucky, it will take a second for me to respond. It's not recommended to use it when I'm sensitive to events
2. Interruption method
Interrupt my understanding of it is that its priority is higher than ordinary functions, which can interrupt low priority interrupts and ordinary tasks being executed.
Interrupt means that as long as the priority of the task being executed is lower than it, when an interrupt comes, it can be interrupted and execute the interrupt function instead.
The principle of interruption is fast in and fast out. Complex tasks cannot be performed in it, or other tasks will be affected. It is more real-time than ordinary tasks and is suitable for event sensitive situations
The use of GPIO input interrupt in Air720SL is as follows
local LED2 function GPIO_Exti_cb(msg)--Interrupt callback function if msg == cpu.INT_GPIO_POSEDGE then--If the current interrupt is triggered by rising edge LED2(0)--Extinguish LED2 else LED2(1)--Otherwise, light up. LED2 end end local function GPIO_Exti_init() local KEY2 = pins.setup(28, GPIO_Exti_cb, pio.PULLUP)--take GPIO28 Initialize as input pull-up and enable interrupt callback LED2 = pins.setup(65, 0)--take GPIO65 Initialize as output end
The use of interrupt is more troublesome than query. It needs to write an extra function. There is a lot of code, but the advantage is high real-time.
3, Download the complete code to the Air720Sl development board
Here we post all the codes directly
--Must be defined at this location PROJECT and VERSION variable --PROJECT: ascii string Type, which can be defined freely as long as it is not used,Just go --VERSION: ascii string Type, if used Luat The firmware upgrade function of IOT cloud platform must be in accordance with the"X.X.X"Definition, X Express1Digit number; otherwise, it can be defined freely PROJECT = "LED" VERSION = "0.0.1" require "sys" --Load the log function module and set the log output level --If the call is closed log The log output by the module interface, with the level set to log.LOG_SILENT that will do require "log" LOG_LEVEL = log.LOGLEVEL_TRACE require "pins" local function GPIO_Input_init() local KEY1 = pins.setup(27, nil, pio.PULLUP)--take GPIO27 Initialize as input pull-up local LED1 = pins.setup(64, 0)--take GPIO64 Initialize as output while true do if KEY1() == 0 then--If KEY1 Grounded, then on LED1 LED1(1) else LED1(0)--Otherwise extinguish LED1 end sys.wait(1000)--Query once a second end end local LED2 function GPIO_Exti_cb(msg)--Interrupt callback function if msg == cpu.INT_GPIO_POSEDGE then--If the current interrupt is triggered by rising edge LED2(0)--Extinguish LED2 else LED2(1)--Otherwise, light up. LED2 end end local function GPIO_Exti_init() local KEY2 = pins.setup(28, GPIO_Exti_cb, pio.PULLUP)--take GPIO28 Initialize as input pull-up and enable interrupt callback LED2 = pins.setup(65, 0)--take GPIO65 Initialize as output end local function user_main() sys.wait(10000) sys.taskInit(GPIO_Input_init) sys.taskInit(GPIO_Exti_init) end --Start system framework sys.taskInit(user_main) sys.init(0, 0) sys.run()
After downloading the program, the running results are as follows: GPIO27 controls LED1, GPIO28 controls LED2
GPIO27 queries once a second, and the response speed is obviously stuck
GPIO28 uses interrupt, almost no delay
Four, summary
1. Official pins library function
- Pins. Setup (28, GPIO? Exi? CB, PIO. Pullup) - initializes GPIO28 as an input pull-up and enables interrupt callbacks
- pins.setup(27, nil, pio.PULLUP) - initializes GPIO27 as an input pull-up
2. Environment of two methods of use
- The query method is applicable to ordinary scenes. When the event sensitivity is not high, the query is not recommended to be too frequent. For general buttons, you can query once in 500ms, and the speed can also be received,
- The interrupt method is applicable to places with strong real-time performance, such as the emergency stop button of some equipment, which can't be queried. Such buttons have strict requirements on events, and can meet the requirements with interrupt
It won't be downloaded click here , go to my second blog 2. Air 720sl module Luat development: how to download it in the first Luat's Hello World
This is just my study notes. I'd like to share them with you. Welcome to criticize and correct. This tutorial is over