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 WhileWhen 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