5.I2C主从数据传输测试
该教程演示如何通过两块开发板实现I2C主从数据传输测试
一、准备工作
- 准备2块BearPi-Pico H2821开发板
二、编译代码
在Windows下编译操作
点击左侧的“KConfig”,打开配置界面.
选择Support I2C Sample.
Application ---> [*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [ ] Support DMA Sample. [*] Support I2C Sample. I2C Sample Configuration --->
设置I2C主设备的I2C id和GPIO,id可设置为0或1,GPIO可设置任意引脚。
I2C Sample Configuration ---> [*] Support I2C Master Sample. (0) Choose I2C master bus id. (NEW) (13) Choose I2C SCL master pin. (NEW) (14) Choose I2C SDA master pin. (NEW)
按下"Save"键保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
设置I2C从设备的I2C id和GPIO,id可设置为0或1,GPIO可设置任意引脚。
I2C Sample Configuration ---> [*] Support I2C Slave Sample. (0) Choose I2C slave bus id. (NEW) (13) Choose I2C SCL slave pin. (NEW) (14) Choose I2C SDA slave pin. (NEW)
按下"Save"键保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
在Ubuntu下编译操作
在MobaXterm中输入:
./build.py menuconfig
选择Support I2C Sample.
Application ---> [*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [ ] Support DMA Sample. [*] Support I2C Sample. I2C Sample Configuration --->
设置I2C主设备的I2C id和GPIO,id可设置为0或1,GPIO可设置任意引脚。
I2C Sample Configuration ---> [*] Support I2C Master Sample. (0) Choose I2C master bus id. (NEW) (13) Choose I2C SCL master pin. (NEW) (14) Choose I2C SDA master pin. (NEW)
按下"ESC"键退出并保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
设置I2C从设备的I2C id和GPIO,id可设置为0或1,GPIO可设置任意引脚。
I2C Sample Configuration ---> [*] Support I2C Slave Sample. (0) Choose I2C slave bus id. (NEW) (13) Choose I2C SCL slave pin. (NEW) (14) Choose I2C SDA slave pin. (NEW)
按下"ESC"键退出并保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
三、测试
烧录固件后按下开发的复位按键,将两块开发板设置为SCL的引脚接一起;将两块开发板设置为SDA的引脚接一起;将两块开发板GND引脚接一起;观察两块开发板的运行日志。
主设备端日志:
i2c0 master send start!
i2c0 master send succ!
i2c0 master receive start!
i2c0 master receive data is 0
i2c0 master receive data is 1
i2c0 master receive data is 2
i2c0 master receive data is 3
i2c0 master receive data is 4
i2c0 master receive data is 5
i2c0 master receive data is 6
i2c0 master receive data is 7
i2c0 master receive succ!
从设备端日志:
i2c0 slave send start!
i2c0 slave receive start!
i2c slave receive data is 0
i2c slave receive data is 1
i2c slave receive data is 2
i2c slave receive data is 3
i2c slave receive data is 4
i2c slave receive data is 5
i2c slave receive data is 6
i2c slave receive data is 7
i2c0 slave receive succ!