*使用 Fritzing 制作的图像*
Raspberry Pi 2 的硬件接口通过开发板上的 40 排针 J8 公开。功能包括:
using Windows.Devices.Gpio;例如,以下代码将 GPIO 5 作为输出打开,并在该引脚上写出数字“<strong>1</strong>”:
- 13x - GPIO 引脚
- 2x - SPI 总线
- 1x - I2C 总线
- 2x - 5V 电源引脚
- 2x - 3.3V 电源引脚
- 8x - 接地引脚
GPIO 引脚
以下 GPIO 引脚可通过 API 访问:GPIO# | Power-on Pull | Header Pin |
---|---|---|
4 | PullUp | 7 |
5 | PullUp | 29 |
6 | PullUp | 31 |
12 | PullDown | 32 |
13 | PullDown | 33 |
16 | PullDown | 36 |
17 | PullDown | 11 |
18 | PullDown | 12 |
19 | PullDown | 35 |
20 | PullDown | 38 |
21 | PullDown | 40 |
22 | PullDown | 15 |
23 | PullDown | 16 |
24 | PullDown | 18 |
25 | PullDown | 22 |
26 | PullDown | 37 |
27 | PullDown | 13 |
35 | PullUp | Red Power LED |
47 | PullUp | Green Activity LED |
public void GPIO()
{
// Get the default GPIO controller on the system
GpioController gpio = GpioController.GetDefault();
if (gpio == null)
return; // GPIO not available on this system
// Open GPIO 5
using (GpioPin pin = gpio.OpenPin(5))
{
// Latch HIGH value first. This ensures a default value when the pin is set as output
pin.Write(GpioPinValue.High);
// Set the IO direction as output
pin.SetDriveMode(GpioPinDriveMode.Output);
} // Close pin - will revert to its power-on state
}
当你打开引脚时,它将处于其通电状态。若要断开拉电阻的连接并获取高阻抗输入,请将驱动程序模式设置为 GpioPinDriveMode.Input:
pin.SetDriveMode(GpioDriveMode.Input);当关闭引脚时,它将还原到其通电状态。
I2C 总线
排针上公开了一个 I2C 控制器 I2C1,带有 SDA 和 SCL 两条线。用于此总线的 1.8KΩ 内部上拉电阻已安装在开发板上。- 引脚 3 - I2C1 SDA
- 引脚 5 - I2C1 SCL
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
public async void I2C()
{
// Get a selector string for bus "I2C1"
string aqs = I2cDevice.GetDeviceSelector("I2C1");
// Find the I2C bus controller with our selector string
var dis = await DeviceInformation.FindAllAsync(aqs);
if (dis.Count == 0)
return; // bus not found
// 0x40 is the I2C device address
var settings = new I2cConnectionSettings(0x40);
// Create an I2cDevice with our selected bus controller and I2C settings
using (I2cDevice device = await I2cDevice.FromIdAsync(dis[0].Id, settings))
{
byte[] writeBuf = { 0x01, 0x02, 0x03, 0x04 };
device.Write(writeBuf);
}
}
SPI 总线
RPi2 上有 2 个 SPI 总线控制器可用: SPI0 和 SPI1。 SPI0 具有标准的 MOSI、<strong>MISO</strong> 和 SCLK 线,并且可以配置为使用 SPI0 CS0 和 SPI0 CS1 两种芯片选择线之一。- 引脚 19 - SPI0 MOSI
- 引脚 21 - SPI0 MISO
- 引脚 23 - SPI0 SCLK
- 引脚 24 - SPI0 CS0
- 引脚 26 - SPI0 CS1
- 引脚 38 - SPI1 MOSI
- 引脚 35 - SPI1 MISO
- 引脚 40 - SPI1 SCLK
- 引脚 11 - SPI1 CS0
using Windows.Devices.Enumeration;
using Windows.Devices.Spi;
public async void SPI()
{
// Get a selector string for bus "SPI0"
string aqs = SpiDevice.GetDeviceSelector("SPI0");
// Find the SPI bus controller device with our selector string
var dis = await DeviceInformation.FindAllAsync(aqs);
if (dis.Count == 0);
return; // "SPI0" not found on this system
// Use chip select line CS0
var settings = new SpiConnectionSettings(0);
// Create an SpiDevice with our bus controller and SPI settings
using (SpiDevice device = await SpiDevice.FromIdAsync(dis[0].Id, settings))
{
byte[] writeBuf = { 0x01, 0x02, 0x03, 0x04 };
device.Write(writeBuf);
}
}