I2C以前没有怎么接触过,所以做它的模拟功能,只能靠着感觉走,有不妥之处,还望方家指正。做出了spi相关的模拟功能,做I2C相对也就不难了,只不过用户操作I2C总线读写数据的时候要稍微麻烦一点。
测试程序运行界面:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/yefanqiu/MF071231151.jpg)
模拟器中的I2C总线的相关代码如下:
public class I2CComponent : I2cDevice
{
public byte[] bytData = new byte[8];
protected override void DeviceRead(byte[] data)
{
try
{
for (int i = 0; i < data.Length; i++)
{
if (i < bytData.Length) data[i] = bytData[i];
}
}
catch { }
base.DeviceRead(data);
}
protected override void DeviceWrite(byte[] data)
{
try
{
for (int i = 0; i < data.Length; i++)
{
if (i < bytData.Length) bytData[i] = data[i];
}
}
catch { }
base.DeviceWrite(data);
}
protected override void DeviceBeginTransaction()
{
base.DeviceBeginTransaction();
}
protected override void DeviceEndTransaction()
{
base.DeviceEndTransaction();
}
}
测试代码如下(还是在原来程序的基础上进行扩充):
static SPI _spi;
public static void Main()
{
OutputPort[] output = new OutputPort[8];
InputPort[] input = new InputPort[8];
//
叶帆模拟器GPIO的pin定义
Cpu.Pin[] pin_I = new Cpu.Pin[8] { (Cpu.Pin)10, (Cpu.Pin)11, (Cpu.Pin)12, (Cpu.Pin)13, (Cpu.Pin)14,(Cpu.Pin)15, (Cpu.Pin)16, (Cpu.Pin)17 };
Cpu.Pin[] pin_Q = new Cpu.Pin[8] { (Cpu.Pin)20, (Cpu.Pin)21, (Cpu.Pin)22, (Cpu.Pin)23, (Cpu.Pin)24, (Cpu.Pin)25, (Cpu.Pin)26, (Cpu.Pin)27 };
//SPI
的pin定义
_spi=new SPI(new SPI.Configuration((Cpu.Pin)30, true, 0, 0, false, false, 4000, SPI.SPI_module.SPI1));
//I2C
定义 模拟器I2C地址为100 时钟速度不要设置太小否则会有问题
I2CDevice I2CBus = new I2CDevice(new I2CDevice.Configuration(100, 200));
for (int i = 0; i < 8; i++)
{
input[i] = new InputPort(pin_I[i], false, Port.ResistorMode.PullDown);
output[i] = new OutputPort(pin_Q[i], false);
}
int intNum = 0;
while (true)
{
output[intNum].Write(!output[intNum].Read());
Debug.Print("I : "+input[0].Read().ToString() + " " + input[1].Read().ToString() + " " + input[2].Read().ToString() + " " + input[3].Read().ToString() + " " + input[4].Read().ToString()+ " " + input[5].Read().ToString() + " " + input[6].Read().ToString() + " " + input[7].Read().ToString());
Debug.Print("AD : "+ReadWriteAD((Int16)intNum).ToString() + " " + intNum.ToString());
//---------------------------------------
//I2C
读写
byte[] bytRData = new byte[8];
byte[] bytWData = new byte[3];
bytWData[0] = (byte)intNum;
bytWData[1] = (byte)(intNum * 2);
bytWData[2] = (byte)(intNum * 3);
I2CDevice.I2CTransaction[] i2c = new I2CDevice.I2CTransaction[2];
i2c[0]=I2CBus.CreateReadTransaction(bytRData);
i2c[1] = I2CBus.CreateWriteTransaction(bytWData);
I2CBus.Execute(i2c, 100); //
执行
Debug.Print("I2C : " + bytRData[0].ToString() + " " + bytRData[1].ToString() + " " + bytRData[2].ToString() + " " + bytRData[3].ToString() + " " + bytRData[4].ToString() + " " + bytRData[5].ToString() + " " + bytRData[6].ToString() + " " + bytRData[7].ToString());
//---------------------------------------
if (++intNum > 7) intNum = 0;
Thread.Sleep(800);
}
}
public static Int16 ReadWriteAD(Int16 value)
{
byte[] bout = new byte[2];
byte[] bin = new byte[2];
bout[0] = (byte)(value >> 8);
bout[1] = (byte)(value & 0xff);
_spi.WriteRead(bout, bin);
Int16 aw0=(Int16)((bin[0] << 8) + bin[1]);
return aw0;
}
}
好了,模拟器的工作暂时告一个段落,有时间把该模拟器完善一下,给感兴趣的网友共享,这样就不用购买硬件就可以测试一些有意思的代码和功能了(一个测试板卡要好几百美元呢!)。