Introduction
Two routines are presented. S_OUT_1.Bas illustrates how to output a byte to a single 74X595 serial in, parallel out shift register and latch. S_OUT_2.Bas illustrates how to output two bytes to two cascaded 74X595s.
Note that we sell the 74HC595 for $1.00.
' Program S_OUT_1.BAS ' ' Illustrates how to use a single 74HC595 serial-in shift register to ' output 8-bit of data. This is a relatively inexpensive and simple ' technique for expanding the number of outputs on either the BasicX-1 ' or on the BX24. This might be expanded to two or even more cascaded ' shift registers. ' ' Data is setup, most sign bit first on S_dat and shifted into the shift ' register on the rising edge of S_clock. When all eight bits have been ' loaded in the shift register, the content of the SR is transferred to ' the output latch. ' ' Note that on the 74HC595, RESET is tied a logic one and /OE to a logic ' zero. ' ' Copyright, Peter H. Anderson, Baltimore, Oct, 99 Public Const S_latch as Byte = 37 Public Const S_clock as Byte = 38 Public Const S_dat as Byte = 39 Sub Main() Dim Patt as Byte Dim N as Integer Call OpenSerialPort(2, 9600) ' For debugging Do For N = 0 TO 7 ' generate a walking LED If (N = 0) then Patt = 1 Else Patt = Patt * 2 End if Call ShiftOutByte(Patt) ' output Patt to 75HC595 Call Sleep(0.5) Next Loop End Sub Sub ShiftOutByte(ByVal Patt as Byte) Dim N as Integer Call PutPin(S_latch, 0) ' start with latch and clock low Call PutPin(S_clock, 0) For N = 0 To 7 If ((Patt \ 128) = 1) then ' most sig bit first Call PutPin(S_dat, 1) Else Call PutPin(S_dat, 0) End if Call PutPin(S_clock, 1) ' clock in the data Call PutPin(S_clock, 0) Patt = Patt * 2 Next Call PutPin(S_latch, 1) ' transfer the shift register to latch Call PutPin(S_latch, 0) End Sub
' Program SHIFT_OUT_2.BAS ' ' Illustrates how to use a two cascaded 74HC595 serial-in shift registers to ' output two bytes of data. This is a relatively inexpensive and simple ' technique for expanding the number of outputs. This might be expanded to ' even more cascaded shift registers. ' ' Note that the Serial Data Out (terminal 9) of the first 74HC585 (low byte) ' is connected to Serial Data In (terminal 14) of the second (high byte). ' ' Data is setup, most sign bit first on S_dat and shifted into the shift ' registers on the rising edge of S_clock. When all 16 bits have been ' loaded in the shift register, the content of the SRs is transferred to ' the output latches. ' ' Note that on the 74HC595s, RESET is tied a logic one and /OE to a logic ' zero. ' ' Copyright, Peter H. Anderson, Baltimore, Oct, 99 ' Public Const S_latch as Byte = 37 Public Const S_clock as Byte = 38 Public Const S_dat as Byte = 39 Sub Main() Dim Patt_Hi as Byte, Patt_Lo as Byte Dim N as Integer Call OpenSerialPort(2, 9600) ' For debugging Do For N = 0 TO 8 ' generate a pair of walking LEDs If (N = 0) then Patt_Hi = bx11111111 Patt_Lo = bx11111111 Else Patt_Hi = Patt_Hi * 2 Patt_lo = Patt_lo \ 2 End if Call ShiftOutTwoBytes(Patt_Hi, Patt_Lo) Call Sleep(0.1) Call PutB(Patt_Hi) Call NewLine() Next Loop End Sub Sub ShiftOutTwoBytes(ByVal Patt_Hi as Byte, ByVal Patt_Lo as Byte) Dim N as Integer Call PutPin(S_latch, 0) ' start with latch and clock low Call PutPin(S_clock, 0) For N = 0 To 7 ' first the high byte If ((Patt_Hi \ 128)= 1) then ' most sig bit first Call PutPin(S_dat, 1) Else Call PutPin(S_dat, 0) End if Call PutPin(S_clock, 1) ' clock in the data Call PutPin(S_clock, 0) Patt_Hi = Patt_Hi * 2 Next For N = 0 To 7 If ((Patt_Lo \ 128 = 1)) then ' most sig bit first Call PutPin(S_dat, 1) Else Call PutPin(S_dat, 0) End if Call PutPin(S_clock, 1) ' clock in the data Call PutPin(S_clock, 0) Patt_Lo = Patt_Lo * 2 Next Call PutPin(S_latch, 1) ' transfer the shift register to latch Call PutPin(S_latch, 0) End Sub