Arduino ESP32TimerInterrupt库实现数码管定时扫描
时间: 2023-06-26 12:10:27 浏览: 169
Arduino ESP32TimerInterrupt库可以用于实现定时器中断,这对于实现数码管的定时扫描非常有用。下面是一个简单的示例代码,演示如何使用ESP32TimerInterrupt库实现数码管的定时扫描。
```c++
#include <ESP32TimerInterrupt.h>
// 数码管引脚定义
const int SEG_A = 4;
const int SEG_B = 5;
const int SEG_C = 6;
const int SEG_D = 7;
const int SEG_E = 8;
const int SEG_F = 9;
const int SEG_G = 10;
const int SEG_DP = 11;
const int DIGIT_1 = 16;
const int DIGIT_2 = 17;
const int DIGIT_3 = 18;
const int DIGIT_4 = 19;
// 数码管数字定义
const byte digits[] = {
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11100110, // 9
};
// 数码管显示缓存
byte displayBuffer[4];
// 数码管扫描计数器
volatile int scanCounter = 0;
// 定时器中断处理函数
void onTimer() {
// 数码管扫描
digitalWrite(DIGIT_1, HIGH);
digitalWrite(DIGIT_2, LOW);
digitalWrite(DIGIT_3, LOW);
digitalWrite(DIGIT_4, LOW);
digitalWrite(SEG_A, bitRead(displayBuffer[0], 0));
digitalWrite(SEG_B, bitRead(displayBuffer[0], 1));
digitalWrite(SEG_C, bitRead(displayBuffer[0], 2));
digitalWrite(SEG_D, bitRead(displayBuffer[0], 3));
digitalWrite(SEG_E, bitRead(displayBuffer[0], 4));
digitalWrite(SEG_F, bitRead(displayBuffer[0], 5));
digitalWrite(SEG_G, bitRead(displayBuffer[0], 6));
digitalWrite(SEG_DP, bitRead(displayBuffer[0], 7));
delayMicroseconds(1000);
digitalWrite(DIGIT_1, LOW);
digitalWrite(DIGIT_2, HIGH);
digitalWrite(DIGIT_3, LOW);
digitalWrite(DIGIT_4, LOW);
digitalWrite(SEG_A, bitRead(displayBuffer[1], 0));
digitalWrite(SEG_B, bitRead(displayBuffer[1], 1));
digitalWrite(SEG_C, bitRead(displayBuffer[1], 2));
digitalWrite(SEG_D, bitRead(displayBuffer[1], 3));
digitalWrite(SEG_E, bitRead(displayBuffer[1], 4));
digitalWrite(SEG_F, bitRead(displayBuffer[1], 5));
digitalWrite(SEG_G, bitRead(displayBuffer[1], 6));
digitalWrite(SEG_DP, bitRead(displayBuffer[1], 7));
delayMicroseconds(1000);
digitalWrite(DIGIT_1, LOW);
digitalWrite(DIGIT_2, LOW);
digitalWrite(DIGIT_3, HIGH);
digitalWrite(DIGIT_4, LOW);
digitalWrite(SEG_A, bitRead(displayBuffer[2], 0));
digitalWrite(SEG_B, bitRead(displayBuffer[2], 1));
digitalWrite(SEG_C, bitRead(displayBuffer[2], 2));
digitalWrite(SEG_D, bitRead(displayBuffer[2], 3));
digitalWrite(SEG_E, bitRead(displayBuffer[2], 4));
digitalWrite(SEG_F, bitRead(displayBuffer[2], 5));
digitalWrite(SEG_G, bitRead(displayBuffer[2], 6));
digitalWrite(SEG_DP, bitRead(displayBuffer[2], 7));
delayMicroseconds(1000);
digitalWrite(DIGIT_1, LOW);
digitalWrite(DIGIT_2, LOW);
digitalWrite(DIGIT_3, LOW);
digitalWrite(DIGIT_4, HIGH);
digitalWrite(SEG_A, bitRead(displayBuffer[3], 0));
digitalWrite(SEG_B, bitRead(displayBuffer[3], 1));
digitalWrite(SEG_C, bitRead(displayBuffer[3], 2));
digitalWrite(SEG_D, bitRead(displayBuffer[3], 3));
digitalWrite(SEG_E, bitRead(displayBuffer[3], 4));
digitalWrite(SEG_F, bitRead(displayBuffer[3], 5));
digitalWrite(SEG_G, bitRead(displayBuffer[3], 6));
digitalWrite(SEG_DP, bitRead(displayBuffer[3], 7));
delayMicroseconds(1000);
// 数码管扫描计数器加一
scanCounter++;
}
void setup() {
// 数码管引脚初始化
pinMode(SEG_A, OUTPUT);
pinMode(SEG_B, OUTPUT);
pinMode(SEG_C, OUTPUT);
pinMode(SEG_D, OUTPUT);
pinMode(SEG_E, OUTPUT);
pinMode(SEG_F, OUTPUT);
pinMode(SEG_G, OUTPUT);
pinMode(SEG_DP, OUTPUT);
pinMode(DIGIT_1, OUTPUT);
pinMode(DIGIT_2, OUTPUT);
pinMode(DIGIT_3, OUTPUT);
pinMode(DIGIT_4, OUTPUT);
// 数码管扫描计数器清零
scanCounter = 0;
// 定时器中断初始化
Timer1.attachInterruptInterval(1000, onTimer); // 每隔1ms触发一次中断
Timer1.start();
}
void loop() {
// 数码管显示数字
displayBuffer[0] = digits[scanCounter % 10];
displayBuffer[1] = digits[(scanCounter / 10) % 10];
displayBuffer[2] = digits[(scanCounter / 100) % 10];
displayBuffer[3] = digits[(scanCounter / 1000) % 10];
}
```
在上面的示例代码中,我们使用ESP32TimerInterrupt库实现了定时器中断,并在中断处理函数中实现了数码管的定时扫描。同时,我们在主循环中更新了数码管显示缓存,以便在下一个定时器中断时显示新的数字。需要注意的是,由于定时器中断处理函数需要尽可能快地执行完毕,因此我们在中断处理函数中使用了延时函数,而不是使用阻塞循环来等待一段时间。
阅读全文