Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Shift-Register Problems


  • Please log in to reply
7 replies to this topic

#1 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 09 October 2012 - 07:35 AM

Hi community.
i trying to send data to 2 shift-registers(74HC595). But not a t the normal way. Shift register 1 is connected to 13, 11 and 9. the second is conneted to 13, 11 and 8. Because when the first broken, then it dont stop the second register, but it dont work.

With the Toolbox.NETMF-Libary works it, but then crash the Telnet-Project and it say always some "Check your Hardware" errors :/
I use this code:
Imports Microsoft.SPOT.Hardware


Class ShiftRegister
#Region "Private Variables"

    ''' <summary>
    ''' Array containing the bits to be output to the shift register.
    ''' </summary>
    Private _bits As Boolean()

    ''' <summary>
    ''' Number of chips required to implement this ShiftRegister.
    ''' </summary>
    Private _numberOfChips As Integer

    ''' <summary>
    ''' SPI interface used to communicate with the shift registers.
    ''' </summary>
    Private _spi As SPI

#End Region

#Region "Constructor(s)"

    ''' <summary>
    ''' Default constructor.
    ''' </summary>
    Private Sub New()
        _bits = Nothing
        _spi = Nothing
    End Sub

    ''' <summary>
    ''' Constructor for the ShiftRegister.
    ''' </summary>
    ''' <param name="bits">Number of bits in the shift register (should be a multiple of 8 bits).</param>
    Public Sub New(ByVal bits As Integer, Optional ByVal latchPin As Cpu.Pin = Cpu.Pin.GPIO_Pin8, Optional ByVal spiModule As SPI.SPI_module = SPI.SPI_module.SPI1, Optional ByVal speedKHz As UInteger = 10)
        If (bits > 0) AndAlso ((bits Mod 8) = 0) Then
            _bits = New Boolean(bits - 1) {}
            _numberOfChips = bits / 8
            For index As Integer = 0 To bits - 1
                _bits(index) = False
            Next

            Dim config As SPI.Configuration

            config = New SPI.Configuration(SPI_mod:=spiModule, ChipSelect_Port:=latchPin, ChipSelect_ActiveState:=False, ChipSelect_SetupTime:=0, ChipSelect_HoldTime:=0, Clock_IdleState:=True, _
                Clock_Edge:=False, Clock_RateKHz:=speedKHz)

            _spi = New SPI(config)
        Else
            Throw New ArgumentOutOfRangeException("ShiftRegister: Size must be greater than zero and a multiple of 8 bits")
        End If
    End Sub

#End Region

#Region "Methods"

    ''' <summary>
    ''' Overload the index operator to allow the user to get/set a particular
    ''' bit in the shift register.
    ''' </summary>
    ''' <param name="bit">Bit number to get/set.</param>
    ''' <returns>Value in the specified bit.</returns>
    Default Public Property Item(ByVal bit As Integer) As Boolean
        Get
            If (bit >= 0) AndAlso (bit < _bits.Length) Then
                Return (_bits(bit))
            End If
            Throw New IndexOutOfRangeException("ShiftRegister: Bit index out of range.")
        End Get
        Set(ByVal value As Boolean)
            If (bit >= 0) AndAlso (bit < _bits.Length) Then
                _bits(bit) = value
            Else
                Throw New IndexOutOfRangeException("ShiftRegister: Bit index out of range.")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Send the data to the SPI interface.
    ''' </summary>
    Public Sub LatchData()
        Dim data As Byte() = New Byte(_numberOfChips - 1) {}

        For chip As Integer = 0 To _numberOfChips - 1
            data(chip) = 0
            Dim bitValue As Byte = 1
            Dim offset As Integer = chip * 8
            For bit As Integer = 0 To 7
                If _bits(offset + bit) Then
                    data(chip) = data(chip) Or bitValue
                End If
                bitValue <<= 1
            Next
        Next
        _spi.Write(data)
    End Sub

#End Region
End Class

And in the Module1.Main()
Dim SHiftRegister As ShiftRegister = New ShiftRegister(8, Pins.GPIO_PIN_D8)
        Dim ShiftRegister2 As ShiftRegister = New ShiftRegister(8, Pins.GPIO_PIN_D9)
        ShiftRegister2(0) = False
        While (True)
            For index As Integer = 0 To 7
                SHiftRegister(index) = True
                SHiftRegister.LatchData()
                Thread.Sleep(100)
                SHiftRegister(index) = False
            Next
        End While
When i start it and when it declare the ShiftRegister2, then it comes in the Libary an errors on _spi = New SPI(config) in Public Sub New(ByVal bits As Integer, Optional ByVal latchPin As Cpu.Pin = Cpu.Pin.GPIO_Pin8, Optional ByVal spiModule As SPI.SPI_module = SPI.SPI_module.SPI1, Optional ByVal speedKHz As UInteger = 10)
The Error: Eine nicht behandelte Ausnahme des Typs "System.InvalidOperationException" ist in Microsoft.SPOT.Hardware.dll aufgetreten.

i cant see the error. with one shiftregister, it works :/

mfg

gfcwfzkm

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 09 October 2012 - 12:20 PM

You can't have multiple instances of the same SPI port. Either you open-then-close for a certain latch pin, or use two different Config structures with the same port. Note that the second way is buggy on the port disposal. Question: why don't you chain the two registers, so that the above problem won't apply?
Biggest fault of Netduino? It runs by electricity.

#3 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 09 October 2012 - 03:50 PM

Because when Register A is damaged, then RegisterB maybe dont work.

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 09 October 2012 - 05:04 PM

You could also connect the registers so they share the same SPI lines and use two separate latch pins.

#5 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 09 October 2012 - 05:55 PM

that what i trying. with the Toolbox.NETMF.Hardware.dll it worked fine, but it crash the Project when i use it in my Telnet-Server (Unsing Toolbox-Lib. too) And here it crash :S mfg

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 09 October 2012 - 10:06 PM

Could be you're using too much RAM. do you use visual studio express or professional? In the latter you can add a c# project in the same solution as a vb project. That way you can just use the required .cs files instead of the full dlls.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#7 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 10 October 2012 - 11:21 AM

Im using Visual Basic Express, but i have Visual C# Express too. I go download the Visual Studio testversion and try it there. EDIT!: Has someone the Downoad link for VisualStudio 2010 test professional? The 2012 Version is crap, it dont unterstand .vb files... mfg gfc

#8 gfcwfzkm

gfcwfzkm

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts
  • LocationBrig-Glis, Switzerland

Posted 13 October 2012 - 07:27 AM

I tried to change the libarys and delete some HardWare and NET-Files (and then rebuild the libary) but Visual C# 2010 cant understand the .csproj It say: The installation dont support these files What can i do? :( mfg gfc EDIT://THANKS, it works




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.