[开发工具] STM32使用arduino开发的方式のIIC

[复制链接]
1056|10
 楼主| goodluck09876 发表于 2019-8-26 15:23 | 显示全部楼层 |阅读模式
 楼主| goodluck09876 发表于 2019-8-26 15:24 | 显示全部楼层
 楼主| goodluck09876 发表于 2019-8-26 15:25 | 显示全部楼层
#include "Wire.h"
#include "SI7021.h"

SI7021 sensor;

// the setup function runs once when you press reset or power the board
void setup()
{
sensor.begin();        // initialization of Si7021 sensor
Serial.begin(9600); // initialization of Serial (STLink), 9600 Bd

// wait for serial
while (!Serial)
{
delay(100);
}

Serial.println("Test of Si7021");

if (!sensor.begin())
{
Serial.println("Si7021 sensor wasn't found");
while (true);
}
}
 楼主| goodluck09876 发表于 2019-8-26 15:25 | 显示全部楼层
// the loop function runs over and over again forever
void loop()
{
// send data through onboard USB-UART converter (STLink)
Serial.print("Temperature: ");
Serial.print(String(sensor.getCelsiusHundredths()/100.0, 2*/)); // 2450 = 24.50 ?C
Serial.println(" ?C");

Serial.print("Humidity: ");
Serial.print(sensor.getHumidityPercent());
Serial.println(" %");
delay(1000);
}
 楼主| goodluck09876 发表于 2019-8-26 15:25 | 显示全部楼层
I2C1 bus wit alternative pinout:
Whole example code you find here.
The important change is using of these fucntions. Wire.setSCL(PB6) and Wire.setSDA(PB7). These functions define where will be connected SDA and SCL of I2C. The connection has to corespond with document which is mentioned above (Pinout of Nucleo develepment boards).

#include "Wire.h"
#include "SI7021.h"

SI7021 sensor;

// the setup function runs once when you press reset or power the board
void setup()
{
Wire.setSCL(PB6); // alternative pinout of I2C1_SCL - PB6 (Morpho con) instead of PB8 (D15)
Wire.setSDA(PB7); // alternative pinout of I2C1_SDA - PB7 (D10) instead of PB9 (D14)

sensor.begin(); // initialization of Si7021 sensor
Serial.begin(9600); // initialization of Serial (STLink), 9600 Bd

// wait for serial
while (!Serial)
{
delay(100);
}

Serial.println("Test of Si7021 and STM32duino");

if (!sensor.begin())
{
Serial.println("Si7021 sensor wasn't found");
while (true);
}
}

// the loop function runs over and over again forever
void loop()
{
// send data through onboard USB-UART converter (STLink)
Serial.print("Temperature: ");
Serial.print(String(sensor.getCelsiusHundredths()/100.0, 2)); // 2450 = 24.50 ?C
Serial.println(" ?C");

Serial.print("Humidity: ");
Serial.print(sensor.getHumidityPercent());
Serial.println(" %");
delay(1000);
}
 楼主| goodluck09876 发表于 2019-8-26 15:26 | 显示全部楼层
 楼主| goodluck09876 发表于 2019-8-26 15:26 | 显示全部楼层
I2C2 bus instead of I2C1 - default pinout
Whole example code you finde here.

It is the same case like using of alternative pinout of I2C1.
You set the SDA (PB7) and SCL (PB6) through funtions and the STM32duino arduino package enables I2C2 bus instead of I2C1.
setSDA and setSCL.
The I2C2 is now default I2C bus.

#include "Wire.h"
#include "SI7021.h"

SI7021 sensor;

// the setup function runs once when you press reset or power the board
void setup()
{
Wire.setSCL(PB_6); // alternative pinout of I2C1_SCL - PB6 (Morpho con) instead of PB8 (D15)
Wire.setSDA(PB_7); // alternative pinout of I2C1_SDA - PB7 (D10) instead of PB9 (D14)

sensor.begin(); // initialization of Si7021 sensor
Serial.begin(9600); // initialization of Serial (STLink), 9600 Bd

// wait for serial
while (!Serial)
{
delay(100);
}

Serial.println("Test of Si7021 and STM32duino");

if (!sensor.begin())
{
Serial.println("Si7021 sensor wasn't found");
while (true);
}
}

// the loop function runs over and over again forever
void loop()
{
// send data through onboard USB-UART converter (STLink)
Serial.print("Temperature: ");
Serial.print(String(sensor.getCelsiusHundredths()/100.0, 2)); // 2450 = 24.50 ?C
Serial.println(" ?C");

Serial.print("Humidity: ");
Serial.print(sensor.getHumidityPercent());
Serial.println(" %");
delay(1000);
}

If you want to use I2C3 bus, the steps are the same like previous cases.
Don't forget to check PeripheralPins.cpp where are described pinouts of I2C buses.
 楼主| goodluck09876 发表于 2019-8-26 15:27 | 显示全部楼层
 楼主| goodluck09876 发表于 2019-8-26 15:27 | 显示全部楼层
看来 使用arduino 搞一个STM32的开发的确是一个很好的环境平台!
comparison 发表于 2019-8-26 15:32 | 显示全部楼层
全英文的啊!楼主厉害了
 楼主| goodluck09876 发表于 2019-8-26 17:11 | 显示全部楼层
这个是国外的一个网站!感觉比较新颖!没想到stm32还可以使用arduino来开发!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

220

主题

5843

帖子

25

粉丝
快速回复 在线客服 返回列表 返回顶部