Get the App
SLTechnology News&Howtos  ›  Development  › 

Analysis of .net Micro Framework implementation of I2C bus Simulator

Shulou Source: shulou.com Published: 2022-06-02 22:34:34 09月30日 Update

This article will explain in detail the analysis of the .net Micro Framework implementation of the I2C bus simulator. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

I2C has not had much contact before, so to do its simulation function, can only rely on the feeling to walk, there is something wrong, but also hope to correct. After making the simulation functions related to spi, it is not difficult to do I2C, but it is a little more troublesome for users to operate I2C bus to read and write data.

Test program run interface:

The relevant code of the I2C bus in the simulator is as follows:

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])

Tags: Simulator bus test code function analysis content article more knowledge program article not bad inappropriate interesting no interest just address basic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Xiaomi macOS MariaDB