I2C总线的模拟器的.Net Micro Framework实现

开发 后端
I2C以前没有怎么接触过,所以做它的模拟功能,只能靠着感觉走,有不妥之处,还望方家指正。做出了spi相关的模拟功能,做I2C相对也就不难了,只不过用户操作I2C总线读写数据的时候要稍微麻烦一点。

I2C以前没有怎么接触过,所以做它的模拟功能,只能靠着感觉走,有不妥之处,还望方家指正。做出了spi相关的模拟功能,做I2C相对也就不难了,只不过用户操作I2C总线读写数据的时候要稍微麻烦一点。

测试程序运行界面:

                         
 
模拟器中的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;
    }
}
好了,模拟器的工作暂时告一个段落,有时间把该模拟器完善一下,给感兴趣的网友共享,这样就不用购买硬件就可以测试一些有意思的代码和功能了(一个测试板卡要好几百美元呢!)。

【编辑推荐】

  1. .Net Micro数据的永久存储浅析
  2. 在.Net Micro Framework中支持英文输入法
  3. 详解.Net Micro实现SideShow窗体界面实例
责任编辑:符甲 来源: CNEMB.com
相关推荐

2009-07-17 13:35:12

IO模拟器研究.Net Micro

2010-06-08 16:25:35

I2C总线协议

2010-07-01 16:45:05

I2C总线协议

2022-06-13 09:21:45

I2C DriverI2C 子系统

2010-06-08 16:21:50

I2C总线协议

2017-03-16 08:30:19

Android ThAndroidI2C

2009-07-16 15:02:33

Shapes命名空间Net Micro F

2010-10-08 17:03:59

.NET Micro Visual Stud

2009-07-09 09:28:19

.Net Micro

2009-06-29 10:19:42

.NET Micro性能优化

2009-07-17 14:51:22

.Net Micro

2009-07-17 14:38:49

Micro Frame

2017-08-11 18:25:25

Linuxi2c

2009-11-17 10:29:39

.NET Micro

2022-05-27 09:00:09

I2C子系统协议

2022-06-12 07:30:13

I3C通讯协议

2009-07-17 13:56:44

.Net Micro

2009-07-17 13:46:52

.Net Micro

2023-04-07 09:14:31

硬件通信串口通信实验

2021-01-19 19:32:01

Cortex-A9 R嵌入式系统i2c 外设
点赞
收藏

51CTO技术栈公众号