c#

位置:IT落伍者 >> c# >> 浏览文章

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


发布日期:2020年12月29日
 
I2C总线的模拟器的.Net Micro Framework实现

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

测试程序运行界面

)thiswidth=; height=>

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

public class ICComponent : IcDevice

{

public byte[] bytData = new byte[];

protected override void DeviceRead(byte[] data)

{

try

{

for (int i = ; i < dataLength; i++)

{

if (i < bytDataLength) data[i] = bytData[i];

}

}

catch { }

baseDeviceRead(data);

}

protected override void DeviceWrite(byte[] data)

{

try

{

for (int i = ; i < dataLength; i++)

{

if (i < bytDataLength) bytData[i] = data[i];

}

}

catch { }

baseDeviceWrite(data);

}

protected override void DeviceBeginTransaction()

{

baseDeviceBeginTransaction();

}

protected override void DeviceEndTransaction()

{

baseDeviceEndTransaction();

}

}

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

static SPI _spi;

public static void Main()

{

OutputPort[] output = new OutputPort[];

InputPort[] input = new InputPort[];

//叶帆模拟器GPIO的pin定义

CpuPin[] pin_I = new CpuPin[] { (CpuPin) (CpuPin) (CpuPin) (CpuPin) (CpuPin)(CpuPin) (CpuPin) (CpuPin) };

CpuPin[] pin_Q = new CpuPin[] { (CpuPin) (CpuPin) (CpuPin) (CpuPin) (CpuPin) (CpuPin) (CpuPin) (CpuPin) };

//SPI的pin定义

_spi=new SPI(new SPIConfiguration((CpuPin) true false false SPISPI_moduleSPI));

//IC定义 模拟器IC地址为 时钟速度不要设置太小否则会有问题

ICDevice ICBus = new ICDevice(new ICDeviceConfiguration( ));

for (int i = ; i < ; i++)

{

input[i] = new InputPort(pin_I[i] false PortResistorModePullDown);

output[i] = new OutputPort(pin_Q[i] false);

}

int intNum = ;

while (true)

{

output[intNum]Write(!output[intNum]Read());

DebugPrint(I : +input[]Read()ToString() + + input[]Read()ToString() + + input[]Read()ToString() + + input[]Read()ToString() + + input[]Read()ToString()+ + input[]Read()ToString() + + input[]Read()ToString() + + input[]Read()ToString());

DebugPrint(AD : +ReadWriteAD((Int)intNum)ToString() + + intNumToString());

//

//IC读写

byte[] bytRData = new byte[];

byte[] bytWData = new byte[];

bytWData[] = (byte)intNum;

bytWData[] = (byte)(intNum * );

bytWData[] = (byte)(intNum * );

ICDeviceICTransaction[] ic = new ICDeviceICTransaction[];

ic[]=ICBusCreateReadTransaction(bytRData);

ic[] = ICBusCreateWriteTransaction(bytWData);

ICBusExecute(ic ); //执行

DebugPrint(IC : + bytRData[]ToString() + + bytRData[]ToString() + + bytRData[]ToString() + + bytRData[]ToString() + + bytRData[]ToString() + + bytRData[]ToString() + + bytRData[]ToString() + + bytRData[]ToString());

//

if (++intNum > ) intNum = ;

ThreadSleep();

}

}

public static Int ReadWriteAD(Int value)

{

byte[] bout = new byte[];

byte[] bin = new byte[];

bout[] = (byte)(value >> );

bout[] = (byte)(value & xff);

_spiWriteRead(bout bin);

Int aw=(Int)((bin[] << ) + bin[]);

return aw;

}

}

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

               

上一篇:.NET中的特殊类型成员

下一篇:C#遍历文件夹返回列表类