教程:VENTUNO Q/微控制器示例:修订间差异

来自WindwizPi wiki
此页面具有访问限制。如果您看见此消息,则说明您没有权限访问此页面。
Windwiz​(留言 | 贡献)
导入 VENTUNO Q 中文教程
 
Windwiz​(留言 | 贡献)
导入 VENTUNO Q 中文教程
 
第4行: 第4行:
本指南介绍在 Arduino® VENTUNO™ Q 的 MCU 上运行的常见示例,包括 I2C、SPI、UART 以及模拟与数字 I/O。
本指南介绍在 Arduino® VENTUNO™ Q 的 MCU 上运行的常见示例,包括 I2C、SPI、UART 以及模拟与数字 I/O。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/mcu-thumb.png" alt="STM32H5F5 MCU 指南" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/mcu-thumb.png
 


本指南中的示例设计为在 Arduino App Lab 环境中运行,并提供在 VENTUNO Q 上运行它们的完整说明。
本指南中的示例设计为在 Arduino App Lab 环境中运行,并提供在 VENTUNO Q 上运行它们的完整说明。
第63行: 第65行:
下面的示例代码使用数字引脚 <code>D5</code> 控制 LED,并读取连接到数字引脚 <code>D4</code> 的按钮状态:
下面的示例代码使用数字引脚 <code>D5</code> 控制 LED,并读取连接到数字引脚 <code>D4</code> 的按钮状态:


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-digital-io.png" alt="数字 I/O 示例接线" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-digital-io.png
 


在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png" alt="创建新 App" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png
 


'''sketch.ino:'''
'''sketch.ino:'''
第117行: 第123行:
下面的示例代码从连接到 <code>A0</code> 的电位器读取模拟输入值,并在串口监视器中显示。要了解如何正确将电位器连接到 VENTUNO Q,请参考下图:
下面的示例代码从连接到 <code>A0</code> 的电位器读取模拟输入值,并在串口监视器中显示。要了解如何正确将电位器连接到 VENTUNO Q,请参考下图:


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-analog-adc.png" alt="ADC 输入示例接线" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-analog-adc.png
 


在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png" alt="创建新 App" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png
 


'''sketch.ino:'''
'''sketch.ino:'''
第162行: 第172行:
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png" alt="创建新 App" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png
 


'''sketch.ino:'''
'''sketch.ino:'''
第195行: 第207行:
DAC 输出应如下图所示:
DAC 输出应如下图所示:


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-analog-dac.png" alt="模拟正弦波 DAC 输出" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-analog-dac.png
 


<span id="pwm-example"></span>
<span id="pwm-example"></span>
第204行: 第218行:
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png" alt="创建新 App" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png
 


'''sketch.ino:'''
'''sketch.ino:'''
第237行: 第253行:
现在你可以通过旋转电位器来控制 PWM 信号的占空比。
现在你可以通过旋转电位器来控制 PWM 信号的占空比。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-pwm-output.png" alt="使用 PWM 的 PWM 输出信号" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-pwm-output.png
 


<blockquote>
<blockquote>
第255行: 第273行:
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png" alt="创建新 App" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png
 


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
第293行: 第313行:
上面的示例代码应输出如下内容:
上面的示例代码应输出如下内容:


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-spi.png" alt="SPI 数据流" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-spi.png
 


<span id="i2c"></span>
<span id="i2c"></span>
第302行: 第324行:
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png" alt="创建新 App" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png
 


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
第334行: 第358行:
上面的示例代码应输出如下内容:
上面的示例代码应输出如下内容:


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-i2c.png" alt="I2C 数据流" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-i2c.png
 


<span id="uart"></span>
<span id="uart"></span>
第356行: 第382行:
你应能在 VENTUNO Q 开发板的 '''TX''' 和 '''RX''' 引脚上看到如下结果(我使用逻辑分析仪捕获数据):
你应能在 VENTUNO Q 开发板的 '''TX''' 和 '''RX''' 引脚上看到如下结果(我使用逻辑分析仪捕获数据):


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-uart-2.png" alt="UART 传输" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-uart-2.png
 


要读取传入数据,可使用 <code>while()</code> 循环持续检查是否有可用数据并读取单个字符。下面的代码将传入字符存储在 String 变量中,并在收到行结束字符时处理数据:
要读取传入数据,可使用 <code>while()</code> 循环持续检查是否有可用数据并读取单个字符。下面的代码将传入字符存储在 String 变量中,并在收到行结束字符时处理数据:
第402行: 第430行:
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。
在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png" alt="创建新 App" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-create-app-small.png
 


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
第446行: 第476行:
运行后,打开 Arduino App Lab 串口监视器,你将看到从 <code>time.nist.gov</code> 服务器获取的时间与日期。
运行后,打开 Arduino App Lab 串口监视器,你将看到从 <code>time.nist.gov</code> 服务器获取的时间与日期。


<img src="https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-wifi.png" alt="Wi-Fi 示例" />
 
https://cdn.jsdelivr.net/gh/arduino/docs-content@main/content/hardware/ventuno/boards/ventuno-q/tutorials/guides/mcu-examples/assets/ventuno-wifi.png
 


<span id="summary"></span>
<span id="summary"></span>

2026年9月28日 (一) 06:01的最新版本

概述

本指南介绍在 Arduino® VENTUNO™ Q 的 MCU 上运行的常见示例,包括 I2C、SPI、UART 以及模拟与数字 I/O。


mcu-thumb.png


本指南中的示例设计为在 Arduino App Lab 环境中运行,并提供在 VENTUNO Q 上运行它们的完整说明。

硬件与软件要求

硬件要求

说明:

大多数示例还需要额外元件才能工作,例如常见电子元件(LED、按钮、电阻)。

软件要求

数字引脚

VENTUNO Q 的数字引脚可通过 Arduino 编程语言的内置函数用作输入或输出。

数字引脚的配置在 setup() 函数中使用内置函数 pinMode() 完成,如下所示:

// Pin configured as an input
pinMode(pin, INPUT);
// Pin configured as an output
pinMode(pin, OUTPUT);
// Pin configured as an input, internal pull-up resistor enabled
pinMode(pin, INPUT_PULLUP);

配置为输入的数字引脚状态可使用内置函数 digitalRead() 读取,如下所示:

// Read pin state, store value in a state variable
state = digitalRead(pin);

配置为输出的数字引脚状态可使用内置函数 digitalWrite() 更改,如下所示:

// Set pin on
digitalWrite(pin, HIGH);
// Set pin off
digitalWrite(pin, LOW);

下面的示例代码使用数字引脚 D5 控制 LED,并读取连接到数字引脚 D4 的按钮状态:


ventuno-digital-io.png


在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


ventuno-create-app-small.png


sketch.ino:

// Define button and LED pin
int buttonPin = D4;
int ledPin = D5;

// Variable to store the button state
int buttonState = 0;

void setup() {
  // Configure button and LED pins
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  // Initialize Serial communication
  Serial.begin(9600);
}

void loop() {
  // Read the state of the button
  buttonState = digitalRead(buttonPin);

  // If the button is pressed, turn on the LED and print its state to the Serial Monitor
  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);
    Serial.println("- Button is pressed. LED is on.");
  } else {
    // If the button is not pressed, turn off the LED and print to the Serial Monitor
    digitalWrite(ledPin, LOW);
    Serial.println("- Button is not pressed. LED is off.");
  }

  // Wait for 1000 milliseconds
  delay(1000);
}

模拟引脚

VENTUNO Q 在 JANALOG 连接器上提供常见的模拟引脚;更多用法详见下文:

模数转换器(ADC)

下面的示例代码从连接到 A0 的电位器读取模拟输入值,并在串口监视器中显示。要了解如何正确将电位器连接到 VENTUNO Q,请参考下图:


ventuno-analog-adc.png


在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


ventuno-create-app-small.png


sketch.ino:

int sensorPin = A0;   // select the input pin for the potentiometer

int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);

  Serial.println(sensorValue);
  delay(100);
}

数模转换器(DAC)

要通过 DAC 引脚输出模拟电压值,请对 DAC 通道使用 analogWrite() 函数。参见下面的示例:

analogWrite(DAC0, value);   // the value should be in the range of the DAC resolution (e.g. 0-4095 with a 12 bits resolution)

说明:

如果将普通 GPIO 传给 analogWrite() 函数,输出将是 PWM 信号。

以下 sketch 将在 VENTUNO Q 的 A0/DAC0 引脚上生成 60 Hz 正弦波 信号:

在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


ventuno-create-app-small.png


sketch.ino:

const float freq = 60.0f;
const int   N    = 256;     // 256 samples/cycle
const uint32_t Ts_us = (uint32_t)llroundf(1e6f / (freq * N));

uint16_t lut[N]; // store the sine wave here

void setup() {
  analogWriteResolution(12);

  for (int i = 0; i < N; ++i){
      lut[i] = 2048 + (1000.0 * sin(2 * PI * i / N));
  }

}

void loop() {
  static uint32_t t_next = micros();
  for (int i = 0; i < N; ++i) {
    analogWrite(DAC0, lut[i]);  // output the sine wave values
    t_next += Ts_us;
    while ((int32_t)(micros() - t_next) < 0) { /* spin */ }
  }
}

DAC 输出应如下图所示:


ventuno-analog-dac.png


PWM 示例

以下是如何创建可变占空比 PWM 信号的示例:

在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


ventuno-create-app-small.png


sketch.ino:

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int pwmOutPin = D3;    // PWM output pin

int sensorValue = 0;  // value read from the pot
int outputValue = 0;  // value output to the PWM (analog out)

void setup() {
  // Define the PWM output resolution
  analogWriteResolution(10);  // 0 - 1023 -> 0 - 100% duty-cycle
  analogReadResolution(14);   // 0 - 16383
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 16383, 0, 1024);
  // change the analog out value:
  analogWrite(pwmOutPin, outputValue);

  // wait 2 milliseconds before the next loop for the ADC
  // to settle after the last reading:
  delay(2);
}

现在你可以通过旋转电位器来控制 PWM 信号的占空比。


ventuno-pwm-output.png


说明:

PWM 频率固定为 500 Hz。

通信协议

SPI

要向兼容 SPI 的设备传输数据,可使用以下示例中的命令:

在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


ventuno-create-app-small.png


#include <SPI.h>

#define SS D10

void setup() {
  // Set the chip select pin as output
  pinMode(SS, OUTPUT);

  // Pull the SS pin HIGH to unselect the device
  digitalWrite(SS, HIGH);

  // Initialize the SPI communication
  SPI.begin();
}

void loop() {
  // Replace with the target device’s address
  byte address = 0x35;
  // Replace with the value to send
  byte value = 0xFA;
  // Pull the SS pin LOW to select the device
  digitalWrite(SS, LOW);
  // Send the address
  SPI.transfer(address);
  // Send the value
  SPI.transfer(value);
  // Pull the SS pin HIGH to unselect the device
  digitalWrite(SS, HIGH);

  delay(2000);
}

上面的示例代码应输出如下内容:


ventuno-spi.png


I2C

要向兼容 I2C 的设备传输数据,可使用以下示例中的命令:

在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


ventuno-create-app-small.png


#include <Wire.h>

void setup() {
  // Initialize the I2C communication
  Wire.begin();
}

void loop() {
  // Replace with the target device’s I2C address
  byte deviceAddress = 0x35;
  // Replace with the appropriate instruction byte
  byte instruction = 0x00;
  // Replace with the value to send
  byte value = 0xFA;
  // Begin transmission to the target device
  Wire.beginTransmission(deviceAddress);
  // Send the instruction byte
  Wire.write(instruction);
  // Send the value
  Wire.write(value);
  // End transmission
  Wire.endTransmission();

  delay(2000);
}

上面的示例代码应输出如下内容:


ventuno-i2c.png


UART

要测试 UART 发送方法,请使用以下示例;请记得在 Arduino App Lab 中创建新 App,然后复制粘贴下面的示例:

void setup() {
  // Initialize the hardware UART at 115200 bps
  Serial1.begin(115200);
}

void loop() {
  // Transmit the string "Hello VENTUNO Q" followed by a newline character
  Serial1.println("Hello VENTUNO Q");
  delay(1000);
}

你应能在 VENTUNO Q 开发板的 TX 和 RX 引脚上看到如下结果(我使用逻辑分析仪捕获数据):


ventuno-uart-2.png


要读取传入数据,可使用 while() 循环持续检查是否有可用数据并读取单个字符。下面的代码将传入字符存储在 String 变量中,并在收到行结束字符时处理数据:

String incoming = "";

void setup() {
  // Initialize the hardware UART at 115200 baud
  Serial1.begin(115200);
}

void loop() {
  while (Serial1.available()) {
    char c = Serial1.read();

    if (c == '\n') {
      // Echo the buffered message and add a newline
      Serial1.println(incoming);

      // Clear for the next message
      incoming = "";
    } else {
      incoming += c;
    }
  }
}

使用此示例,VENTUNO Q 会将在 UART 上收到的内容原样发回。

说明:

要通过 JDIGITAL 连接器上的硬件串口引脚通信,必须使用 Serial1 对象。否则,Serial 将与你的 USB 串口终端通信。

在 MCU 上启用 Wi-Fi®

由于无线电模块连接到 Dragonwing™ QCS8275,我们需要 Bridge 来将连接能力暴露给微控制器。

以下示例通过 socket RPC 调用使用 TCP 获取 UTC 时间,并在串口监视器中打印:

在 Arduino App Lab 中创建新 App,然后将下面的示例复制粘贴到新 App 的 “sketch” 部分。


ventuno-create-app-small.png


BridgeTCPClient<> client(Bridge);

void setup() {
  if (!Bridge.begin()) {
    while (true) {}
  }

  Serial.begin(9600);

  Serial.println("TCP Daytime Demo started");
}

void loop() {
  Serial.println("\nConnecting to time.nist.gov ...");

  if (client.connect("time.nist.gov", 13) < 0) {
    Serial.println("Connection failed!");
    delay(5000);
    return;
  }

  Serial.println("Connected, reading response...");
  String line;
  while (client.connected() || client.available()) {
    if (client.available()) {
      char c = client.read();
      if (c == '\n') break; // daytime sends one line
      if (c != '\r') line += c;
    }
  }

  Serial.print("Server says: ");
  Serial.println(line);

  client.stop();
  delay(10000);
}

运行后,打开 Arduino App Lab 串口监视器,你将看到从 time.nist.gov 服务器获取的时间与日期。


ventuno-wifi.png


总结

在本指南中,你已成功探索 VENTUNO Q 的微控制器能力。你学习了如何使用数字与模拟 I/O 与物理世界交互、生成 PWM 信号,以及使用 SPI、I2C 和 UART 协议与外部传感器和模块通信。

此外,你还体验了 VENTUNO Q 混合架构的强大之处:通过使用 Bridge 库,STM32 MCU 可以无缝访问由基于 Linux 的 MPU 提供的 Wi-Fi 连接。

延伸阅读


原文:https://docs.arduino.cc/tutorials/ventuno-q/mcu-examples/ 本页为 Arduino 官方教程的中文翻译,供内部查阅。命令与代码保持原文。