当前位置:网站首页>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);
}边栏推荐
- Smart fan system based on stm32f407
- Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
- 炒股開戶傭金優惠怎麼才能獲得,網上開戶安全嗎
- 2022.02.13
- Vscode regular match replace console log(.*)
- D24:divisor and multiple (divisor and multiple, translation + solution)
- After the Lunar New Year and a half
- Pandaoxi's video
- "Learning notes" recursive & recursive
- Introducing Software Testing
猜你喜欢

How to quickly build high availability of service discovery

Alibaba cloud container service differentiation SLO hybrid technology practice

Amway by head has this project management tool to improve productivity in a straight line

Pyqt5 sensitive word detection tool production, operator's Gospel
![P3371 [template] single source shortest path (weakened version)](/img/c5/bcb22e1ea573253005e56ebbd73bb7.jpg)
P3371 [template] single source shortest path (weakened version)

Idea integrates Microsoft TFs plug-in

How to understand the gain bandwidth product operational amplifier gain

A treasure open source software, cross platform terminal artifact tabby

Double efficiency. Six easy-to-use pychar plug-ins are recommended

Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
随机推荐
Gossip about redis source code 77
Pyqt5 sensitive word detection tool production, operator's Gospel
Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
Interesting 10 CMD commands
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
After the Lunar New Year and a half
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
How can I get the Commission discount of stock trading account opening? Is it safe to open an account online
Scratch uses runner Py run or debug crawler
D24:divisor and multiple (divisor and multiple, translation + solution)
D29:post Office (post office, translation)
C # basic knowledge (2)
Is user authentication really simple
[MySQL] sql99 syntax to realize multi table query
A preliminary study on the middleware of script Downloader
Current detection circuit - including op amp current scheme
[note] glide process and source code analysis
Alibaba cloud container service differentiation SLO hybrid technology practice
Pyqt5 sensitive word detection tool production, operator's Gospel