Tuesday, November 23, 2010

Using Joysticks from VB.NET

There are several ways for using joystick on VB.NET: APIs and DirectX. Here I'll show you the DirectX Method.

First, download DirectX SDK from Microsoft:
and then install it.

Starts a new project on VB.NET and add a reference to DirectX:
Project -> Add reference.
Select Microsoft.DirectX.DirectInput from list in ".NET" tab:

First thing to do is obtain a device list, but VB.NET will throw an exception (Loader Lock). Deactivate Loader Lock exception from menu Debud -> Exceptions. Explode "Managed Debugging Assistant", find "Loader Lock" and uncheck the box "Generated":

Imports DirectInput name space at top of your program:

Add a timer (timer1) on your form and set "Enabled" property on true (we'll use it for joystick polling).

Add a textbox named txtDir
Add a textbox named txtButtons

The code is the following:

Imports Microsoft.DirectX.DirectInput

Public Class Form1

    Dim joystickDevice As Device

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' List of attached joysticks
        Dim gameControllerList As DeviceList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)

        ' There is one controller at least?
        If (gameControllerList.Count > 0) Then

            ' Select first joystick
            ' Create an object instance
            Dim deviceInstance As DeviceInstance = gameControllerList.Current

            joystickDevice = New Device(deviceInstance.InstanceGuid)
            joystickDevice.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)

        End If



    End Sub

    Private Sub joystickPolling()

            Dim state As JoystickState = joystickDevice.CurrentJoystickState

            txtDir.Text = "X=" & state.X & " Y=" & state.Y & " Z=" & state.Z

            txtbuttons.Text = state.GetButtons(0) & " " & state.GetButtons(1)

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    End Sub
End Class

state object will contain all informations you need. State.X is the X position of joystick (type state followed by dot and see the entire list of controls).

the GetButtons property of state object will give you the state of buttons. GetButtons give an array. 

dim button1 as byte = state.GetButtons(0) ' retrieve first button state

if the value is 128, the button is pressed, if the value is 0 the button isn't pressed or doesn't exists 

Monday, November 8, 2010

Modem Siemens TC35 - Ignition

The modems Siemens TC35, TC35i, TC37 are mainly used for deliver/receive sms in embedded systems. Those modems requires a start sequence to power up. Let's take a look to datasheet:

VBatt+ is the main voltage on TC35 (on TC35i this is called only Batt+), located on pins 1-5 of the module. VDDLP is a secondary power source used for RTCC backup, located on pin 30. IGT is the ignition line, used to turn on the module, pin 15.

As you see from the diagrams, the module will no power up with only with VBatt+, but it needs a signal on IGT pin for at least 100mS at low level when you power up the main line. After the 100mS at low level, IGT line must go to High Impedance, but I've tried with a circuit that goes to high level instead High impedance without any damage (please try it at your own risk!).

The schematic is the following (thanks to P. Seminara of robotics-net.com):

WARNING! This schematic is only usable with a TC35 mounted on an adapter board that translates logic levels (5V <-> 3V)

When you power up the circuit (+5V), C1 will let pass some current (until it's fully charged). This current, through R1 will take T1 to saturation, closing collector with emitter and then on IGT line there is 0V. When C1 is fully charged (after about 150mS, RC constant of this circuit is of 147mS), T1 will be interdicted because C1 will stop the current flow and then the base of T1 will tied down by R2. 

Actually, T1 will be interdicted when Vbe falls under 0.7V

In this condition the circuit betwewn collector and emitter will open and the IGT goes to high level thanks R3 (pullup resistor).

On the oscilloscope we'll observe this behaviour:

The Cyan line is the main power, yellow is the IGniTion line. After power-up, the IGT line goes to low level for about 130mS (time for C1 charging) and then goes to high level. 

(only for www.settorezero.com subscribers)

Thursday, November 4, 2010

How to control 16 leds with only 3 I/O

The idea is quite simple: using a shift register to drive a led matrix using only few I/O of a microcontroller. Here I'll show you a my idea on how to drive 16 leds using only 3 I/Os with a Latched Shift Register 74HC595:

The microcotroller is connected to this circuit using the lines I've named as CLOCK, DATA and LATCH. 

The 74HC595 has a shift register and a latch. Incoming data are sent into shift register  using pin 14 (DS, serial data in) and pin 11 (SH_CP, Shift register clock). When we've  finished to sent serial data, we must  transfer data from shift register to the latch pulling high the pin 12 (ST_CP, Storage clock). If pin 13 (OE, output enable) is pulled down, the output pins (Q0-Q7) will reflect the latch status. Pin 10 is the Master Reset, pulling it down will cause the reset of the shift register. Pin 9 is serial data out and is used to drive more 74HC595.

We must feed data on DATA line using CLOCK and keeping LATCH down. When we have finished to send data, LATCH line must be pulled high and data are transferred from shift register to latch and value is displayed on leds.  We must pull LATCH down before start a new transfer but leds will remain in actual state. This is possible because pin 13 (OE = output enabled) is always low and then outputs are always enabled.

In order to display a value on 16 leds, data must be multiplexed. Entire first row will be lighted from serial value 0001 1111 : remember that high nibble drives the columns, low nibble drives the rowss. Entire second row will be lighted sending value  0010 1111 . Second led of third row (row=3 column=2) will be lighted sending value 0010 0010 and so on. Using a latched shift register will prevent the ghosting effect on leds.

Here there is an article in italian language I've written about using a shift register 74LS164 with a picmicro microcontroller. The informations and some code are about the same for 74HC595. The only two difference are:

- on 74HC595 data are sent from MSB to LSB, then the first bit you send on data line will be the bit n°8
- after you've send data, you must enable and then disable latch.

I have successfully tested this circuit. I've written a very long article on how to use this circuit witch a picmicro microcontroller. Article is in italian language, you can find it  clicking here

There are some examples here on how to use one or more 74HC595 with Arduino.


74HC595 datasheet