C# I2C programming - Sample code for I2C programming


1. Download and open C# project file
  1. Download source code.

    I2C.Sample1.zip



2. C# source code for I2C.Sample1
  1. Program.cs

      
    
    using System;
    
    using Microsoft.SPOT;
    using Microsoft.SPOT.Input;
    using Microsoft.SPOT.Presentation;
    using Microsoft.SPOT.Presentation.Controls;
    using Microsoft.SPOT.Hardware;
    
    using SPL.Script.Engine;
    
    namespace SPLMFConsoleWindow1
    {
        public class Program : Microsoft.SPOT.Application
        {
            public static void Main()
            {
                Program myApplication = new Program();
    
                Window mainWindow = myApplication.CreateWindow();
    
                GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
    
                myApplication.Run(mainWindow);
            }
    
            private Window mainWindow;
    
            //##########################  Sample Code  #################################
            private ConsoleWindow _consoleWindow;
    
            private I2CDevice _device = null;
            //##########################################################################
    
            public Window CreateWindow()
            {
                mainWindow = new Window();
                mainWindow.Height = SystemMetrics.ScreenHeight;
                mainWindow.Width = SystemMetrics.ScreenWidth;
    
                //##########################  Sample Code  #################################
                Font font = Resources.GetFont(Resources.FontResources.small);
                _consoleWindow = new ConsoleWindow(font);
    
                mainWindow.Child = _consoleWindow;
                //##########################################################################
    
                mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
    
                mainWindow.Visibility = Visibility.Visible;
    
                Buttons.Focus(mainWindow);
    
                //##########################  Sample Code  #################################
                _consoleWindow.WriteLine("Please push any button!.");
                _consoleWindow.WriteLine("Select button will clear screen.");
                //##########################################################################
    
    
                //##########################  Sample Code  #################################
                I2CDevice.Configuration config = new I2CDevice.Configuration(
                                58,
                                100
                                );
    
                _device = new I2CDevice(config);
                //##########################################################################
    
                return mainWindow;
            }
    
            private void OnButtonUp(object sender, ButtonEventArgs e)
            {
                Debug.Print(e.Button.ToString());
    
                //##########################  Sample Code  #################################
                if (e.Button == Microsoft.SPOT.Hardware.Button.VK_SELECT)
                {
                    _consoleWindow.ClearConsole();
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_UP)
                {
                    byte[] rBuff = new byte[2];
    
                    I2CDevice.I2CReadTransaction rTr = _device.CreateReadTransaction(rBuff);
                    I2CDevice.I2CTransaction[] rTrArr = new I2CDevice.I2CTransaction[] { rTr };
    
                    int count = _device.Execute(rTrArr, 1000);
    
    
                    _consoleWindow.WriteLine("Received data is " + Util.BytesToHex(rBuff));
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_DOWN)
                {
                    byte[] wBuff = new byte[4];
                    wBuff[0] = 0x31;
                    wBuff[1] = 0x32;
                    wBuff[2] = 0x33;
                    wBuff[3] = 0x34;
    
                    I2CDevice.I2CWriteTransaction wTr = _device.CreateWriteTransaction(wBuff);
                    I2CDevice.I2CTransaction[] wTrArr = new I2CDevice.I2CTransaction[] { wTr };
    
                    int count = _device.Execute(wTrArr, 1000);
    
                    _consoleWindow.WriteLine("I2C device write");
                }
                //##########################################################################
            }
        }
    }
    
    
      



3. Execute sample code
  1. Open "Properties" page of C# project.

  2. Choose ".NET Micro Framework" tab.

  3. Select "SPLMFConsoleEmulator" from the emulator list.



  4. Press "F5" key to run sample code.

  5. Click Down-Button to send data through I2C device.




4. Read data from I2C device
  1. Before read data from I2C device, you need to prepare emulator script to react for the read request.

  2. Select "Emulator" tab on the emulator.

  3. Add below script on the editor.

      
    BindI2CComponent  
    	/Id:i2c1
    	/Procedure_OnRead:proc1
    
    procedure proc1
    
    	print "OnReadRequested: " + value.Length + " bytes"
    	
    	buff = Util.CreateArrayByte(2)
    	buff[0] = 0x31
    	buff[1] = 0x32
    
    	i2c1.SetReadBuffer(buff)
    end
    
      




  4. Press "F5" key or click "Execute Script on the Emulator".

  5. Now, click Up-Button on the emulator, then you can see the result of read via I2C device.