一、GPIO 中斷使用簡析
1、中斷觸發方式
ESP32 Arduino 有以下四種觸發方式:
LOW 低電平觸發
CHANGE 電平變化
RISING 上升沿觸發
FALLING 下降沿觸發
HIGH 高電平觸發
2、配置中斷
在定義中斷函數後,需要在 setup 函數配置中斷函數
// interrupt=中斷通道編號,function=中斷函數,mode=中斷觸發模式
attachInterrupt(interrupt, function, mode);
// pin=中斷引腳,function=中斷函數,mode=中斷觸發模式
attachInterrupt(pin, function, mode);
如果在程序運行過程不需要使用外部中斷了,可以用中斷分離函數來取消這一中斷設置:
detachInterrupt(interrupt);
detachInterrupt(Pin);。
3、示例
void setup()
{
// 初始化日誌打印串口
Serial.begin(115200);
// 配置中斷引腳
pinMode(26, INPUT|PULLUP );
// 檢測到引腳 26 下降沿,觸發中斷函數 blink
attachInterrupt(26, blink, FALLING);
Serial.println("\nstart irq test");
}
void loop()
{
}
// 中斷函數
void blink()
{
Serial.println("IRQ");
}
二、IIC 使用簡析
示例:
#include <Wire.h>
void setup() {
// 啓動 i2c 總線
Wire.begin();
// 初始化串口
Serial.begin(9600);
}
int reading = 0;
void loop() {
// step 1: 啓動與從設備 #112 0x70 的數據交互
Wire.beginTransmission(112);
// 發送數據
Wire.write(byte(0x00));
Wire.write(byte(0x50));
// 結束通信
Wire.endTransmission();
// step 2: 等待讀數據
delay(70);
// step 3: 讀取指定寄存器
Wire.beginTransmission(112);
Wire.write(byte(0x02));
Wire.endTransmission();
// step 4: 請求讀 2 字節數據
Wire.requestFrom(112, 2);
// step 5: 接收數據
if (2 <= Wire.available()) {
reading = Wire.read();
reading = reading << 8;
reading |= Wire.read();
Serial.println(reading);
}
delay(250);
}
三、SPI 使用簡析
示例:
/* The ESP32 has four SPi buses, however as of right now only two of
* them are available to use, HSPI and VSPI. Simply using the SPI API
* as illustrated in Arduino examples will use HSPI, leaving VSPI unused.
*
* However if we simply intialise two instance of the SPI class for both
* of these buses both can be used. However when just using these the Arduino
* way only will actually be outputting at a time.
*
* Logic analyser capture is in the same folder as this example as
* "multiple_bus_output.png"
*
* created 30/04/2018 by Alistair Symonds
*/
#include <SPI.h>
static const int spiClk = 1000000; // 1 MHz
//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;
SPIClass * hspi = NULL;
void setup() {
// 初始化 SPI 實例 VSPI、HSPI
vspi = new SPIClass(VSPI);
hspi = new SPIClass(HSPI);
//clock miso mosi ss
//使用默認 VSPI 引腳:SCLK = 18, MISO = 19, MOSI = 23, SS = 5
vspi->begin();
// alternatively route through GPIO pins of your choice
//vspi->begin(0, 2, 4, 33); // SCLK, MISO, MOSI, SS
//使用默認引腳初始化 HSPI
//SCLK = 14, MISO = 12, MOSI = 13, SS = 15
hspi->begin();
//alternatively route through GPIO pins
//hspi->begin(25, 26, 27, 32); //SCLK, MISO, MOSI, SS
// 初始化 ss 片選引腳,默認爲低電平
pinMode(5, OUTPUT); //VSPI SS
pinMode(15, OUTPUT); //HSPI SS
}
// the loop function runs over and over again until power down or reset
void loop() {
//use the SPI buses
vspiCommand();
hspiCommand();
delay(100);
}
void vspiCommand() {
// 模擬數據
byte data = 0b01010101;
// 啓動 VSPI 傳輸
vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(5, LOW);
vspi->transfer(data);
digitalWrite(5, HIGH);
vspi->endTransaction();
}
void hspiCommand() {
byte stuff = 0b11001100;
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(15, LOW);
hspi->transfer(stuff);
digitalWrite(15, HIGH);
hspi->endTransaction();
}