In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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])
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.