B4R Code Snippet SN74HC595N - my effort with this shi(f)t register

The SN74HC595N seem to be a very temperamental shi(f)t register. Took me hours to try and figure it out. Seems to have a lot to do with the timing when driving LED's (sinking too much current if they are on for too long?).

Well, this is my effort - the LED's flicker on/off, one at a time, from LSB to MSB and then again from MSB to LSB.

No libraries other than the Core library.

Wiring Diagram:

shift_register_bb.png



Photo shot:
20190526_132516.jpg


Sample code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.

    Public Serial1 As Serial
    Private datapin, latchpin, clockpin As Pin
    Private Registers(8) As Boolean
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    datapin.Initialize(8, datapin.MODE_OUTPUT)                 'data - pin 14 on 74HC595
    latchpin.Initialize(9, latchpin.MODE_OUTPUT)               'latch - pin 12 on 74HC595
    clockpin.Initialize(10, clockpin.MODE_OUTPUT)              'clock - pin 11 on 74HC595
    AddLooper("myLooper")
    
End Sub

Sub myLooper

    For i = 0 To 7                  'switch off all led's
        Registers(i) = False
    Next
    UpdateRegister
    Delay(10)
    
    For i = 0 To 7                   'switch led's from LSB to MSB one at a time (only one led active at a time)
        Registers(i) = True
        For j = 0 To 7
            If j <> i Then
                Registers(j) = False
            End If
        Next
        Delay(10)       
        UpdateRegister       
    Next
    
    For i = 0 To 7                  'switch off all led's
        Registers(i) = False
    Next
    UpdateRegister
    Delay(10)
  
    For i = 7 To 0 Step - 1        'switch led's from MSB to LSB one at a time (only one led active at a time)
        Registers(i) = True
        For j = 0 To 7
            If j <> i Then
                Registers(j) = False
            End If
        Next
        Delay(10)
        UpdateRegister
    Next
    
    For i = 0 To 7                'switch off all led's
        Registers(i) = False
    Next
    UpdateRegister
    Delay(10)
    
End Sub

Private Sub UpdateRegister()
    
    latchpin.DigitalWrite(False)                   'take the latch pin low - make it ready to start pushing in 8 bits

    For i = 0 To 7
        clockpin.DigitalWrite(False)               'take the clock pin low
        Delay(1)
        datapin.DigitalWrite(Registers(i))         'push in a bit
        Delay(1)
        clockpin.DigitalWrite(True)                'take the clock pin high
        Delay(1)
    Next

    latchpin.DigitalWrite(True)                    'enable the 8 data outputs on the 74HC595 with the 8 bits that were pushed in
    Delay(1)
    
End Sub
 

Attachments

  • b4rShiftRegister.zip
    1.2 KB · Views: 458
Top