【IoT】ESP32 Arduino GPIO 使用简析

一、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();
}

 

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
ESP32 Arduino MQTT is a way to connect an ESP32 microcontroller to an MQTT broker using the Arduino programming language. MQTT stands for Message Queuing Telemetry Transport, which is a lightweight messaging protocol designed for IoT devices. This protocol allows devices to publish and subscribe to topics, enabling communication between them. To use ESP32 Arduino MQTT, you need to first set up your ESP32 microcontroller with the Arduino IDE. Once you have done that, you can install the MQTT library for Arduino and start coding. Here are the basic steps: 1. Install the PubSubClient library in the Arduino IDE. 2. Connect your ESP32 to a Wi-Fi network using the Wi-Fi library. 3. Connect to an MQTT broker using the MQTT library and the PubSubClient library. 4. Publish messages to a topic using the MQTT library and the PubSubClient library. 5. Subscribe to topics and receive messages using the MQTT library and the PubSubClient library. Here is an example code for setting up an ESP32 Arduino MQTT connection: ``` #include <WiFi.h> #include <PubSubClient.h> // Wi-Fi credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // MQTT broker details const char* mqtt_server = "your_MQTT_broker_IP_address"; const int mqtt_port = 1883; const char* mqtt_username = "your_MQTT_username"; const char* mqtt_password = "your_MQTT_password"; // MQTT client WiFiClient espClient; PubSubClient client(espClient); void setup() { // Connect to Wi-Fi network WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); } // Connect to MQTT broker client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); while (!client.connected()) { if (client.connect("ESP32_client", mqtt_username, mqtt_password)) { client.subscribe("test/topic"); } else { delay(1000); } } } void loop() { // Publish message to MQTT broker client.publish("test/topic", "Hello, world!"); // Check for incoming messages client.loop(); } void callback(char* topic, byte* payload, unsigned int length) { // Handle incoming messages Serial.print("Message received on topic: "); Serial.println(topic); Serial.print("Message: "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } ``` This code connects to a Wi-Fi network and an MQTT broker, subscribes to a topic, publishes a message to that topic, and receives incoming messages. You can modify the code to fit your specific use case.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

产品人卫朋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值