当前位置:网站首页>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);
}边栏推荐
- Powerful blog summary
- Gorilla/mux framework (RK boot): add tracing Middleware
- The difference between single power amplifier and dual power amplifier
- Vscode regular match replace console log(.*)
- Gossip about redis source code 80
- Smart fan system based on stm32f407
- D25:sequence search (sequence search, translation + problem solving)
- What is the Valentine's Day gift given by the operator to the product?
- 2/14 (regular expression, sed streaming editor)
- X Opencv feature point detection and matching
猜你喜欢

Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

The interviewer's biggest lie to deceive you, bypassing three years of less struggle

Is user authentication really simple

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Interesting 10 CMD commands

Actual combat | use composite material 3 in application

Investment demand and income forecast report of China's building ceramics industry, 2022-2028

How to quickly build high availability of service discovery

Idea a method for starting multiple instances of a service
随机推荐
Fluent learning (4) listview
"Learning notes" recursive & recursive
How to quickly build high availability of service discovery
A treasure open source software, cross platform terminal artifact tabby
China standard gas market prospect investment and development feasibility study report 2022-2028
Interesting 10 CMD commands
Social network analysis -social network analysis
Is user authentication really simple
ADB related commands
Pandaoxi's video
Actual combat | use composite material 3 in application
Selenium check box
The difference between single power amplifier and dual power amplifier
Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
C summary of knowledge point definitions, summary notes
It is forbidden to splice SQL in code
Subgraph isomorphism -subgraph isomorphism
C # basic knowledge (1)
[note] IPC traditional interprocess communication and binder interprocess communication principle
Ramble 72 of redis source code