Connecting the MD22 to the USB-I2C interface 

 

Introduction
The following example demonstrates how to connect the MD22 dual motor controller to the USB-I2C interface .
For the example I used the following free software:

Microsoft Visual Basic 2008 Express Edition - Available as a free download from Microsoft here: Visual Express


Connection diagram

Make sure you have the switches set for I2C address &HB0 (all switches on) for this example.

The complete project is available for download here: usb_i2c_md22.zip
Source code for Microsoft Visual Basic 2008 Express Edition 

' Example for interfacing a USB-I2C to a MD22 using Microsoft Visual Basic 2008 Express Edition
' written by C Clarke Nov 2008

Public Class Form1

    Dim
USB_I2C As New System.IO.Ports.SerialPort 'declares a handle for COM port called USB_I2C
   
Dim SerBuf(10) As Byte 'a global accessable array for data to be buffered for the COMX port

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       ' Show all available COM ports.   
        For Each sp As String In My.Computer.Ports.SerialPortNames
           comportselect.Items.Add(sp) 'adds COMX to listbox called "comportselect"
   
     Next
   
    comportselect.Text = "Select COM Port" 'adds the message when program is first loaded
   
End Sub

    ' the routine below is executed by a timer at a 100ms interval
   
Private Sub READMD22(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (USB_I2C.IsOpen) Then ' checks that a port has actually been opened to talk down
   
         SerBuf(0) = &H55 ' Command for Devices with 1 internal address
   
         SerBuf(1) = &HB1 ' MD22 I2C address + low bit set to indicate its a read operation
   
         SerBuf(2) = 0 ' start from register 0
   
         SerBuf(3) = 8 ' and read 8 registers (0-7)
   
         USB_I2C.Write(SerBuf, 0, 4) ' send the read command to the USB-I2C module
   
         read_i2c(8)
            TrackBar1.Value = SerBuf(1)
            TrackBar2.Value = SerBuf(2)
            TrackBar3.Value = SerBuf(3)
            TextBoxSpeed1.Text = SerBuf(1)
            TextBoxSpeed2.Text = SerBuf(2)
            TextBoxAcceleration.Text = SerBuf(3)
            TextBoxVersion.Text = SerBuf(7)
        End If
    End
Sub

   
Private Sub comportselect_SelectedItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comportselect.SelectedItemChanged
        If (USB_I2C.IsOpen) Then 'if program has previously opened a COM port, start by closing it
   
         USB_I2C.Close()
        End If
   
       Try
   
        USB_I2C = My.Computer.Ports.OpenSerialPort(comportselect.Text, 19200, 0, 8, 2) ' attempt to open a new COM port
   
          Catch 'catch open failures
   
              Return
   
     End Try
   
    USB_I2C.ReadTimeout = 100 'we will only arrive here on successfull open
   
     init_md22()
    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        If (USB_I2C.IsOpen) Then
   
         SerBuf(0) = &H55 ' Command for Devices with 1 internal address
   
         SerBuf(1) = &HB0 ' MD22 I2C address + low bit clear to indicate its a write operation
   
         SerBuf(2) = 1 ' speed1 at register 1
   
         SerBuf(3) = 1 ' only 1 data byte
   
         SerBuf(4) = TrackBar1.Value
            USB_I2C.Write(SerBuf, 0, 5) ' send the write 5 bytes from serbuf command to the USB-I2C module
   
         read_i2c(1)
        End If
   
End Sub

    Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
        If (USB_I2C.IsOpen) Then
   
         SerBuf(0) = &H55 ' Command for Devices with 1 internal address
   
         SerBuf(1) = &HB0 ' MD22 I2C address + low bit clear to indicate its a write operation
   
         SerBuf(2) = 2 ' speed2 at register 2
   
         SerBuf(3) = 1 ' only 1 data byte
   
         SerBuf(4) = TrackBar2.Value
            USB_I2C.Write(SerBuf, 0, 5) ' send the write 5 bytes from serbuf command to the USB-I2C module
   
         read_i2c(1)
        End If
   
End Sub

    Private Sub TrackBar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar3.Scroll
        If (USB_I2C.IsOpen) Then
   
         SerBuf(0) = &H55 ' Command for Devices with 1 internal address
   
         SerBuf(1) = &HB0 ' MD22 I2C address + low bit clear to indicate its a write operation
   
         SerBuf(2) = 3 ' acceleration at register 3
   
         SerBuf(3) = 1 ' only 1 data byte
   
         SerBuf(4) = TrackBar3.Value
            USB_I2C.Write(SerBuf, 0, 5) ' send the write 5 bytes from serbuf command to the USB-I2C module
   
         read_i2c(1)
        End If
   
End Sub

    Private Sub init_md22()
        SerBuf(0) = &H55 ' Command for Devices with 1 internal address
   
     SerBuf(1) = &HB0 ' MD22 I2C address + low bit clear to indicate its a write operation
   
     SerBuf(2) = 1 ' speed1 at register 1
   
     SerBuf(3) = 2 ' send 2 data bytes, speed 1 and speed 2
   
     SerBuf(4) = 128 ' speed1
   
     SerBuf(5) = 128 ' speed2
   
     USB_I2C.Write(SerBuf, 0, 6) ' send the write 6 bytes from serbuf command to the USB-I2C module
   
     read_i2c(1)
    End Sub

    Public Sub read_i2c(ByVal count As Object)    
        Dim byte_count As Byte

   
     Do While (byte_count < count) ' get the data bytes
   
           Try
   
            USB_I2C.Read(SerBuf, byte_count, 1)    
            Catch
   
             USB_I2C.Close() 'timeout for response, close the port!
   
             TrackBar1.Value = 0
                TrackBar2.Value = 0
                TrackBar3.Value = 0
                TextBoxSpeed1.Text = ""
   
             TextBoxSpeed2.Text = ""
   
             TextBoxAcceleration.Text = ""
   
             TextBoxVersion.Text = ""
   
                 Return
   
          End Try
   
        byte_count = byte_count + 1
        Loop
    End
Sub

End Class

 

The complete project is available for download here: usb_i2c_md22.zip