且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.Net Micro Framework研究—带I2C总线的模拟器

更新时间:2022-09-22 15:42:12

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

测试程序运行界面:

.Net Micro Framework研究—带I2C总线的模拟器


 
模拟器中的I2C总线的相关代码如下:
    


  1. public class I2CComponent : I2cDevice  
  2.    {  
  3.        public byte[] bytData = new byte[8];  
  4.        protected override void DeviceRead(byte[] data)  
  5.        {  
  6.            try 
  7.            {  
  8.                for (int i = 0; i < data.Length; i++)  
  9.                {  
  10.                    if (i < bytData.Length) data[i] = bytData[i];  
  11.                }  
  12.           }  
  13.            catch { }   
  14.            base.DeviceRead(data);  
  15.        }  
  16.        protected override void DeviceWrite(byte[] data)  
  17.        {  
  18.            try 
  19.            {  
  20.                for (int i = 0; i < data.Length; i++)  
  21.                {  
  22.                    if (i < bytData.Length) bytData[i] = data[i];  
  23.                }  
  24.            }  
  25.            catch { }   
  26.            base.DeviceWrite(data);  
  27.        }  
  28.        protected override void DeviceBeginTransaction()  
  29.        {  
  30.            base.DeviceBeginTransaction();  
  31.        }  
  32.        protected override void DeviceEndTransaction()  
  33.        {  
  34.            base.DeviceEndTransaction();  
  35.        }  
  36.    }  

测试代码如下(还是在原来程序的基础上进行扩充):
   


  1.  static SPI _spi;  
  2.     public static void Main()  
  3.     {  
  4.         OutputPort[] output = new OutputPort[8];  
  5.         InputPort[] input = new InputPort[8];   
  6.           
  7.         //叶帆模拟器GPIO的pin定义  
  8.         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 };  
  9.         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 };  
  10.    
  11.         //SPI的pin定义  
  12.         _spi=new SPI(new SPI.Configuration((Cpu.Pin)30, true, 0, 0, falsefalse, 4000, SPI.SPI_module.SPI1));  
  13.    
  14.         //I2C定义                           模拟器I2C地址为100 时钟速度不要设置太小否则会有问题  
  15.         I2CDevice I2CBus = new I2CDevice(new I2CDevice.Configuration(100, 200));  
  16.    
  17.         for (int i = 0; i < 8; i++)  
  18.         {  
  19.             input[i] = new InputPort(pin_I[i], false, Port.ResistorMode.PullDown);  
  20.             output[i] = new OutputPort(pin_Q[i], false);  
  21.         }  
  22.    
  23.         int intNum = 0;  
  24.         while (true)  
  25.         {  
  26.             output[intNum].Write(!output[intNum].Read());  
  27.             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());  
  28.             Debug.Print("AD : "+ReadWriteAD((Int16)intNum).ToString() + " " + intNum.ToString());  
  29.             //---------------------------------------  
  30.    
  31.             //I2C读写  
  32.             byte[] bytRData = new byte[8];  
  33.             byte[] bytWData = new byte[3];  
  34.             bytWData[0] = (byte)intNum;  
  35.             bytWData[1] = (byte)(intNum * 2);  
  36.             bytWData[2] = (byte)(intNum * 3);  
  37.             I2CDevice.I2CTransaction[] i2c = new I2CDevice.I2CTransaction[2];  
  38.             i2c[0]=I2CBus.CreateReadTransaction(bytRData);  
  39.             i2c[1] = I2CBus.CreateWriteTransaction(bytWData);  
  40.             I2CBus.Execute(i2c, 100);   //执行  
  41.    
  42.             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());  
  43.    
  44.             //---------------------------------------  
  45.             if (++intNum > 7) intNum = 0;  
  46.             Thread.Sleep(800);  
  47.         }  
  48.     }  
  49.     public static Int16 ReadWriteAD(Int16 value)  
  50.     {  
  51.         byte[] bout = new byte[2];  
  52.         byte[] bin = new byte[2];  
  53.         bout[0] = (byte)(value >> 8);  
  54.         bout[1] = (byte)(value & 0xff);  
  55.         _spi.WriteRead(bout, bin);  
  56.         Int16 aw0=(Int16)((bin[0] << 8) + bin[1]);  
  57.         return aw0;  
  58.     }  
  59. }   

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

 ]












本文转自yefanqiu51CTO博客,原文链接:http://blog.51cto.com/yfsoft/322937,如需转载请自行联系原作者