这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 【Let'sdo第1期-DIY功率检测与控制系统】过程贴:环境搭建与基础任务的实

共3条 1/1 1 跳转至

【Let'sdo第1期-DIY功率检测与控制系统】过程贴:环境搭建与基础任务的实现

助工
2025-06-10 17:27:54     打赏

上网查了一下,STM32F411这颗芯片已经被Arduino支持了。心中不由窃喜。使用Vscode+platformIO搭建起了开发环境。使用arduino,编程变得简单多啦!


任务1:使用单片机定时器实现1s翻转LED验证开发板时钟晶振是否正确

1749540569170.jpg

使用platformio创建一个项目,板卡选择“nucleo_f411re”,烧写方式选择stlink。这里修改了platformio的配置文件,学习网上的一个项目实现多个例程的功能。首先是实现LED灯的翻转。

查看文档可知,板子上板载了一颗LED灯,对应管脚为PA5,在arduino中映射到D13。

image.png

#include <Arduino.h>
#define LED_BUILTIN D13
void setup(void)
{
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
}
void loop(void)
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  Serial.println("Hello World!");
}

image.png


2. 实现OLED屏幕显示信息。这次搭配的屏幕是一款双色0.96寸的OLED屏幕,分辨率为128X64;接口为I2C接口。

image.png

image.png

参考着说明文档,将OLED接到板子的I2C接口上去。SCL->D15(PB8),SDA->D14(PB9),先用杜邦线接好连线。OLED屏幕使用5v供电,魏雷方便后边的拓展,使用了个I2C的拓展卡来方便接线。

image.png

修改platfomio.ini配置文件,增加一个oled的例程。这里使用了U8g2的驱动OLED的例程库。使用这个例程库的好处是,可以很轻松地支持起中文

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

; NUCLEO-F411RE
[env]
platform = ststm32
board = nucleo_f411re
framework = arduino
monitor_speed = 115200
upload_protocol = stlink

[platformio]
; default_envs = 41_LEDBLANK
default_envs = 42_IIC_SSD1306_OLED
lib_deps =
    olikraus/U8g2 @ ^2.36.5

src_dir = examples/${platformio.default_envs}
[env:41_LEDBLANK]
[env:42_IIC_SSD1306_OLED]
#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>

// iic驱动方式
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); 
// U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ PB3, /* data=*/ PB5, /* reset=*/ U8X8_PIN_NONE);   // 使用软件IIC,SCL PB6,SDA PB7
void setup(void)
{
  Serial.begin(115200);
  u8g2.begin();
  u8g2.enableUTF8Print(); // enable UTF8 support for the Arduino print() function
}
void loop(void)
{
  u8g2.setFont(u8g2_font_unifont_t_chinese2); // use chinese2
  u8g2.firstPage();
  do
  {
    u8g2.setCursor(5, 15);
    u8g2.print("Let's do 2025"); // Chinese "Hello World"
    u8g2.setCursor(4, 30);
    u8g2.print("电子产品世界"); // Chinese "Hello World"
  } while (u8g2.nextPage());
  delay(1000);
}

image.png


任务3. 使用开发板实现对INA219的负载测量5V,12V电压电流并显示在OLED。

image.png

这个是INA219电流检测模块 - SEN0291 。原理是,在负载上串联了一颗电阻(阻值为:10毫欧),通过测量电阻两端的电压值,计算负载的电流值大小。使用I2C接口通讯。所以这里就直接接到了I2C扩展板上了。与OLED使用同一组I2C接口。

; NUCLEO-F411RE
[env]
platform = ststm32
board = nucleo_f411re
framework = arduino
monitor_speed = 115200
upload_protocol = stlink

[platformio]
; default_envs = 41_LEDBLANK
; default_envs = 42_IIC_SSD1306_OLED
; lib_deps =
;     olikraus/U8g2 @ ^2.36.5
; default_envs = 43_INA219
default_envs = 44_POWER

src_dir = examples/${platformio.default_envs}
[env:41_LEDBLANK]
[env:42_IIC_SSD1306_OLED]
[env:43_INA219]
[env:44_POWER]

这个电流模块可以在DFRobot的官网找到Arduino下的驱动代码,下载后参考着例程使用。

#include <Wire.h>
#include "DFRobot_INA219.h"

#define LED_BUILTIN D13
DFRobot_INA219_IIC     ina219(&Wire, INA219_I2C_ADDRESS4);
float ina219Reading_mA = 1000;
float extMeterReading_mA = 1000;

void setup(void) 
{
    Serial.begin(115200);
    //Open the serial port
    while(!Serial);
    
    Serial.println();
    //Initialize the sensor
    while(ina219.begin() != true) {
        Serial.println("INA219 begin faild");
        delay(2000);
    }
    //Linear calibration
    ina219.linearCalibrate(/*The measured current before calibration*/ina219Reading_mA, /*The current measured by other current testers*/extMeterReading_mA);
    Serial.println();
}

void loop(void)
{
    Serial.print("BusVoltage:   ");
    Serial.print(ina219.getBusVoltage_V(), 2);
    Serial.println("V");
    Serial.print("ShuntVoltage: ");
    Serial.print(ina219.getShuntVoltage_mV(), 3);
    Serial.println("mV");
    Serial.print("Current:      ");
    Serial.print(ina219.getCurrent_mA(), 1);
    Serial.println("mA");
    Serial.print("Power:        ");
    Serial.print(ina219.getPower_mW(), 1);
    Serial.println("mW");
    Serial.println("");
    delay(1000);
}

7b046a38c06ab9385e1671e5f8dc668.jpg

上电后,电流模块不接负载,不接电,OLED显示

image.png

power.zip






菜鸟
2025-06-13 10:02:29     打赏
2楼

终于有一个跟我一样使用Arduino的啦


助工
2025-06-16 08:48:56     打赏
3楼

已经成功地烧毁了电流检测模块,看看淘宝能不能买个芯片换上!


共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]