当前位置:网站首页>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);
}边栏推荐
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Enter MySQL in docker container by command under Linux
- Pyqt5 sensitive word detection tool production, operator's Gospel
- D27:mode of sequence (maximum, translation)
- Live app source code, jump to links outside the station or jump to pages inside the platform
- Open 2022 efficient office, starting from project management
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- JarPath
- Kubedl hostnetwork: accelerating the efficiency of distributed training communication
- Kubedl hostnetwork: accelerating the efficiency of distributed training communication
猜你喜欢

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

EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance

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

Loop compensation - explanation and calculation of first-order, second-order and op amp compensation

Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method

Selenium check box

2022 system integration project management engineer examination knowledge points: software development model
![P3371 [template] single source shortest path (weakened version)](/img/c5/bcb22e1ea573253005e56ebbd73bb7.jpg)
P3371 [template] single source shortest path (weakened version)

2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination

Iclr2022: how does AI recognize "things I haven't seen"?
随机推荐
股票开户佣金最低的券商有哪些大家推荐一下,手机上开户安全吗
[MySQL] classification of multi table queries
Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
P1339 [USACO09OCT]Heat Wave G
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
[note] glide process and source code analysis
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
FPGA tutorial and Allegro tutorial - link
Idea a method for starting multiple instances of a service
SPI based on firmware library
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
Actual combat | use composite material 3 in application
D30:color tunnels (color tunnels, translation)
Make small tip
在恒泰证券开户怎么样?安全吗?
How to quickly build high availability of service discovery
Fluent learning (5) GridView
After the Lunar New Year and a half
Smart fan system based on stm32f407
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?