当前位置:网站首页>ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
2022-07-03 23:49:00 【pocean2012】
1. Embedded development requires a deep understanding “ Limited resources ”
The main technical contradiction of embedded system design and development : Chip and board level resource constraints VS. Real time requirements for task and event processing
Memory ,Flash Space ,GPIO Number , ADC/DAC Number of channels, etc , Even the volume , Power waste , Embedded systems face many constraints , From the perspective of demand, the system requires timely processing , Fast response and low power consumption are better ...... wait , This pair of contradictions gave birth to the core technologies of various embedded systems , Low power consumption and power saving 、 Real-time operating system 、 High speed serial port ...... All to solve the above technical contradictions .
2. For god horse to have interruption mechanism ?
The main program of the embedded system mentioned above is basically a big cycle , If the request of each task is to be processed in the loop , The roughest way is to take turns to query , Polling for short , Polling can solve the problems of simple systems , But when the number of requests and processing complexity increase , It's hard to avoid , Interruption is to solve “ Distribute on demand ” Resource allocation problem . Let's take a look at a picture :
3. Basic concept and processing flow of interruption
In the process of program running , There is a system that must be created by CPU Immediate situation , here ,CPU The process of temporarily suspending the execution of a program to deal with this new situation is called interruption .
4. Three elements of interrupt processing
• Trigger Conditions ( type / Channel configuration, etc , The process is initialization )
5. With Timer Take interruption as an example , Is the easiest to understand , Set up an alarm clock , When the time comes “ work ”
Look at it ESP32-Arduino From the application of the platform, we can see various types of interrupts , Other external interrupts , Abnormal interruption, etc. expand in another article , Examples include the following applications :
*Alarm Type timed interrupt
* Set the clock running in the background “ Tick tock ”, Deal with different tasks according to the beat (ticker)
*RTC Real time clock , Provide a clock that is aligned and synchronized with the real world ( Specific date , Minutes and seconds )
*WDT watchdog
6. The sample code
Case study 1-----Timer-sample1 Timed interrupt
#define LED_ON LOW;
#define LED_OFF HIGH;
hw_timer_t *timer=NULL;
uint32_t flag=0;
static void IRAM_ATTR Timer0_CallBack(void);
const int led=22;
int ledState=LED_OFF;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(led, OUTPUT);
timer=timerBegin(0,80,true);
timerAttachInterrupt(timer,Timer0_CallBack,true);
timerAlarmWrite(timer,1000000,true); // Company us, Timing mode
timerAlarmEnable(timer); // Start timer
}
void loop() {
// put your main code here, to run repeatedly:
if(flag){
flag=0;
Serial.println("time out. "); // Serial port printing only requires monitoring , Non time sensitive
}
}
static void IRAM_ATTR Timer0_CallBack(void){
flag=1;
ledState=!ledState; // Controlling flashing lights is a time sensitive task
digitalWrite(led,ledState);
}
Case study 2----- Use clock ticking to deal with multitasking
#include <Arduino.h>
#include <Ticker.h>
// attach a LED to pPIO 21
#define LED_PIN 21
Ticker blinker;
Ticker toggler;
Ticker changer;
float blinkerPace = 0.1; //seconds
const float togglePeriod = 5; //seconds
void change() {
blinkerPace = 0.5;
}
void blink() {
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
void toggle() {
static bool isBlinking = false;
if (isBlinking) {
blinker.detach();
isBlinking = false;
}
else {
blinker.attach(blinkerPace, blink);
isBlinking = true;
}
digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON)
}
void setup() {
pinMode(LED_PIN, OUTPUT);
toggler.attach(togglePeriod, toggle);
changer.once(30, change);
}
void loop() {
}
Case study 3--RTC Real time clock
Learn how to be arduino Get library support in
Tools -> Management of the library -> Search for “ESP32TIME”-> install
And then in “ Example ” Open the corresponding example in OK 了 . Here we use an example to illustrate the simplest call
/*
esp32time sample
*/
#include <ESP32Time.h>
ESP32Time rtc;
void setup() {
Serial.begin(115200);
rtc.setTime(30, 24, 15, 17, 1, 2022); // 17th Jan 2022 15:24:30
}
void loop() {
// Serial.println(rtc.getTime()); // (String) 15:24:38
// Serial.println(rtc.getDate()); // (String) Sun, Jan 17 2021
// Serial.println(rtc.getDate(true)); // (String) Sunday, January 17 2021
// Serial.println(rtc.getDateTime()); // (String) Sun, Jan 17 2021 15:24:38
// Serial.println(rtc.getDateTime(true)); // (String) Sunday, January 17 2021 15:24:38
// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021
// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021
//
// Serial.println(rtc.getMicros()); // (long) 723546
// Serial.println(rtc.getMillis()); // (long) 723
// Serial.println(rtc.getEpoch()); // (long) 1609459200
// Serial.println(rtc.getSecond()); // (int) 38 (0-59)
// Serial.println(rtc.getMinute()); // (int) 24 (0-59)
// Serial.println(rtc.getHour()); // (int) 3 (0-12)
// Serial.println(rtc.getHour(true)); // (int) 15 (0-23)
// Serial.println(rtc.getAmPm()); // (String) pm
// Serial.println(rtc.getAmPm(true)); // (String) PM
// Serial.println(rtc.getDay()); // (int) 17 (1-31)
// Serial.println(rtc.getDayofWeek()); // (int) 0 (0-6)
// Serial.println(rtc.getDayofYear()); // (int) 16 (0-365)
// Serial.println(rtc.getMonth()); // (int) 0 (0-11)
// Serial.println(rtc.getYear()); // (int) 2021
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
// formating options http://www.cplusplus.com/reference/ctime/strftime/
struct tm timeinfo = rtc.getTimeStruct();
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); // (tm struct) Sunday, January 17 2021 07:24:38
delay(1000);
}
边栏推荐
- C # basic knowledge (1)
- Introduction to the gtid mode of MySQL master-slave replication
- 2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
- [Mongodb] 2. Use mongodb --------- use compass
- 2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
- Gossip about redis source code 78
- Selenium library 4.5.0 keyword explanation (I)
- 2022 system integration project management engineer examination knowledge points: software development model
- Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
- Selenium library 4.5.0 keyword explanation (III)
猜你喜欢
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
Solve the problem that the kaggle account registration does not display the verification code
Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
Double efficiency. Six easy-to-use pychar plug-ins are recommended
Fluent learning (4) listview
Vscode regular match replace console log(.*)
Idea integrates Microsoft TFs plug-in
How will the complete NFT platform work in 2022? How about its core functions and online time?
A preliminary study on the middleware of script Downloader
The first game of the new year, many bug awards submitted
随机推荐
Gossip about redis source code 73
Design of logic level conversion in high speed circuit
Amway by head has this project management tool to improve productivity in a straight line
2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
Open 2022 efficient office, starting from project management
Gossip about redis source code 82
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
"Learning notes" recursive & recursive
The upload experience version of uniapp wechat applet enters the blank page for the first time, and the page data can be seen only after it is refreshed again
Alibaba cloud container service differentiation SLO hybrid technology practice
Interesting 10 CMD commands
D28:maximum sum (maximum sum, translation)
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
Live app source code, jump to links outside the station or jump to pages inside the platform
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
股票開戶傭金最低的券商有哪些大家推薦一下,手機上開戶安全嗎
SPI based on firmware library
2022 t elevator repair registration examination and the latest analysis of T elevator repair
How to quickly build high availability of service discovery
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?