且构网

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

.Net Micro Framework研究—带AD的模拟器

更新时间:2022-09-11 08:16:51

继昨天的研究,希望再接再厉把AD模入模出在模拟器上也实现出来,本以为挺简单,其实AD相关功能,与GPIO不同,在模拟器中前者通过GpioPort实现,后者通过SpiDevice实现。幸好有Temperature(温度采集)示例可以参考,否则一时真无法下手。不知道为什么,很简单的代码他们总写的很复杂,让你很难看懂,幸好有以前的相关的经验做底子,用了大半天的时间模入模出都搞定了。
下面就是模拟器中的AD输入输出的相关代码。
    


  1. public class SpiADComponent : SpiDevice  
  2.     {  
  3.         public Int16 AW0 = 0;  
  4.         public Int16 QW0 = 0;  
  5.         protected override byte[] Write(byte[] data)  
  6.         {  
  7.             //------------  
  8.             try 
  9.             {  
  10.                 QW0 = (Int16)((data[0] << 8) + data[1]);  
  11.             }  
  12.             catch { }   
  13.             //------------  
  14.             byte[] bytes = new byte[2];  
  15.             bytes[0] = (byte)(AW0 >> 8);  
  16.             bytes[1] = (byte)(AW0 & 0xff);  
  17.             return bytes;  
  18.         }  
  19.     }  

 注:由于受西门子PLC200的影响比较大,所以把模拟量入和出,定义为AW0和QW0,并且用两个字节的整数表示,根据需要可以进行折算。
我们把昨天的GPIO代码稍加改进,把AD部分测试代码添加进去,代码如下:
 


  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.         for (int i = 0; i < 8; i++)  
  15.         {  
  16.             input[i] = new InputPort(pin_I[i], false, Port.ResistorMode.PullDown);  
  17.             output[i] = new OutputPort(pin_Q[i], false);  
  18.         }  
  19.    
  20.         int intNum = 0;  
  21.         while (true)  
  22.         {  
  23.             output[intNum].Write(!output[intNum].Read());  
  24.             Debug.Print(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());  
  25.             Debug.Print(ReadWriteAD((Int16)intNum).ToString() + " " + intNum.ToString());  
  26.             if (++intNum > 7) intNum = 0;  
  27.             Thread.Sleep(1000);  
  28.         }  
  29.     }  
  30.     public static Int16 ReadWriteAD(Int16 value)  
  31.     {  
  32.         byte[] bout = new byte[2];  
  33.         byte[] bin = new byte[2];  
  34.         bout[0] = (byte)(value >> 8);  
  35.         bout[1] = (byte)(value & 0xff);  
  36.         _spi.WriteRead(bout, bin);  
  37.         Int16 aw0=(Int16)((bin[0] << 8) + bin[1]);  
  38.         return aw0;  
  39.     }  
  40.  }   

运行后的界面如下:

 

.Net Micro Framework研究—带AD的模拟器

从上图可以看出,模拟器已经和测试程序进行交互了,即可以设置AW0的值,也可以获取QW0的值。

.Net Micro Framework研究—带AD的模拟器


 
我又把模拟器改进了一下,可以支持多种IO模拟功能,目前已经完成了GPIO和AD,I2C应该也不难,但是Serial(串口)估计有问题,因为模拟器支持库没有相关函数。















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