Research on. NET micro framework-Simulator with I2C bus

Source: Internet
Author: User

I2C has never been touched before, so its simulation function can only rely on the feeling, there is something wrong, still hope that the House correct. With the SPI-related simulation function, it is not difficult to do I2C, but it is a little troublesome for users to read and write data through the I2C bus.

Test program running interface: the I2C bus code 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. l Ength) bytdata [I] = data [I] ;}} catch {} base. devicewrite (data);} protected override void devicebegintransaction () {base. devicebegintransaction ();} protected override void deviceendtransaction () {base. deviceendtransaction () ;}} test code: (expanded based on the original program): static SPI _ SPI; public static void main () {outputport [] Output = new outputport [8]; inputport [] input = new inputport [8]; // pin definition of gpio of the Yifan Simulator 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 definition _ SPI = new SPI (New SPI. configuration (CPU. PIN) 30, true, 0, 0, false, false, 4000, SPI. spi_module.spi1); // I 2c defines that the I2C address of the simulator is 100. Do not set the clock speed too small. Otherwise, the 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 read/write 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); // execute 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 ;}}

Now, the simulator's work is coming up with a paragraph to improve the simulator and share it with interested netizens, in this way, you can test some interesting code and functions without buying hardware (a test board costs hundreds of dollars !).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.