Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I'm sure we're all looking forward to hearing, and seeing, how you get onMy Cubiko, backed on Kickstarter back in April, finally arrived today. Yay. Other users have been turning some good-looked milled boards. I didn't expect I'd make many milled boards, but times have changed.
There's a lot to learn.
View attachment 150402
View attachment 150403
'===============================================================================
' File: main.bas
' Author: burt and AI
'
' Created: July 31, 2025
'
' Target: PIC18F252
' Compiler: Swordfish BASIC
'
' Description:
' This program demonstrates sending a byte of data to a 74HC595 shift
' register using a PIC18F252 running on a 20MHz external crystal.
' This version uses a custom subroutine to shift the data out.
'
' Hardware Connections:
' PIC18F252 -> 74HC595
' ---------------------------------
' RC0 (Pin 16) -> DS (Serial Data In, Pin 14)
' RC1 (Pin 17) -> SHCP (Shift Register Clock, Pin 11)
' RC2 (Pin 18) -> STCP (Storage Register Clock / Latch, Pin 12)
'
' The 74HC595's OE (Output Enable, Pin 13) should be tied to GND to enable
' the outputs, and MR (Master Reset, Pin 10) should be tied to VCC to
' prevent resetting the register.
'===============================================================================
' --- Device and Clock Configuration ---
Device = 18F252
Clock = 20
Include "Utils.bas"
' --- Configuration Fuses ---
Config
OSC = HS, ' High-speed crystal oscillator
WDT = Off, ' Watchdog Timer disabled
LVP = Off, ' Low-Voltage Programming disabled
BOR = Off, ' Brown-out Reset disabled
PWRT = Off ' Power-up Timer disabled
' --- Pin Aliases for 74HC595 ---
Dim DataPin As PORTC.0 ' Alias for the Data pin (DS)
Dim ShiftPin As PORTC.1 ' Alias for the Shift Clock pin (SHCP)
Dim LatchPin As PORTC.2 ' Alias for the Latch/Storage pin (STCP)
' --- Variable Declaration ---
Dim DataToSend As Byte
' --- Custom Subroutine to send a byte ---
' This subroutine manually sends a byte of data, bit by bit.
Sub ShiftOutByte(pData As Byte)
Dim i As Byte
' Loop 8 times for 8 bits
For i = 0 To 7
' Check the most significant bit (MSB)
If (pData And %10000000) > 0 Then
High(DataPin)
Else
Low(DataPin)
End If
' Pulse the shift clock to send the bit
High(ShiftPin)
Low(ShiftPin)
' Shift the data to the left to get the next bit
pData = pData << 1
Next
End Sub
' --- Main Program ---
Sub Main()
' Initialize the data to send. % is used for binary numbers in Swordfish
DataToSend = %10101010
SetAllDigital()
' Main program loop
While True
' Send the byte of data to the 74HC595 using our custom subroutine
ShiftOutByte(DataToSend)
' Now, pulse the latch pin to move the data from the shift
' register to the output pins.
High(LatchPin)
DelayUS(10) ' A small delay to ensure the latch registers the pulse
Low(LatchPin)
' Wait for half a second
DelayMS(500)
' Invert the data for a blinking effect on the next loop
DataToSend = Not DataToSend
Wend
End Sub
'===============================================================================
' File: main.bas
' Author: burt and AI
'
' Created: July 31, 2025
'
' Target: PIC18F252
' Compiler: Swordfish BASIC
'
' Description:
' This program demonstrates sending a byte of data to a 74HC595 shift
' register using a PIC18F252 running on a 20MHz external crystal.
' This version uses a custom subroutine to shift the data out.
'
' Hardware Connections:
' PIC18F252 -> 74HC595
' ---------------------------------
' RC0 (Pin 16) -> DS (Serial Data In, Pin 14)
' RC1 (Pin 17) -> SHCP (Shift Register Clock, Pin 11)
' RC2 (Pin 18) -> STCP (Storage Register Clock / Latch, Pin 12)
'
' The 74HC595's OE (Output Enable, Pin 13) should be tied to GND to enable
' the outputs, and MR (Master Reset, Pin 10) should be tied to VCC to
' prevent resetting the register.
'===============================================================================
' --- Device and Clock Configuration ---
Device = 18F252
Clock = 20
Include "Utils.bas"
' --- Configuration Fuses ---
Config
OSC = HS, ' High-speed crystal oscillator
WDT = Off, ' Watchdog Timer disabled
LVP = Off, ' Low-Voltage Programming disabled
BOR = Off, ' Brown-out Reset disabled
PWRT = Off ' Power-up Timer disabled
' --- Pin Aliases for 74HC595 ---
Dim DataPin As PORTC.0 ' Alias for the Data pin (DS)
Dim ShiftPin As PORTC.1 ' Alias for the Shift Clock pin (SHCP)
Dim LatchPin As PORTC.2 ' Alias for the Latch/Storage pin (STCP)
' --- Variable Declaration ---
Dim DataToSend As Byte
' --- Custom Subroutine to send a byte ---
' This subroutine manually sends a byte of data, bit by bit.
Sub ShiftOutByte(pData As Byte)
Dim i As Byte
' Loop 8 times for 8 bits
For i = 0 To 7
' Check the most significant bit (MSB)
If (pData And %10000000) > 0 Then
High(DataPin)
Else
Low(DataPin)
End If
' Pulse the shift clock to send the bit
High(ShiftPin)
Low(ShiftPin)
' Shift the data to the left to get the next bit
pData = pData << 1
Next
End Sub
' --- Main Program ---
' Main()
' Initialize the data to send. % is used for binary numbers in Swordfish
DataToSend = %10101010
SetAllDigital()
Output (PORTC.0)
Output (PORTC.1)
Output (PORTC.2)
' Main program loop
While True
' Send the byte of data to the 74HC595 using our custom subroutine
ShiftOutByte(DataToSend)
' Now, pulse the latch pin to move the data from the shift
' register to the output pins.
High(LatchPin)
DelayUS(10) ' A small delay to ensure the latch registers the pulse
Low(LatchPin)
' Wait for half a second
DelayMS(500)
' Invert the data for a blinking effect on the next loop
DataToSend = Not DataToSend
Wend
'End
Here some code for mr deb to play with
'===============================================================================
' File: main_array.bas
' Author: burt and AI
'
' Created: August 1, 2025
'
' Target: PIC18F252
' Compiler: Swordfish BASIC
'
' Description:
' This program demonstrates sending a sequence of bytes from an array to a
' 74HC595 shift register, creating a "Cylon scanner" animation. It uses
' a PIC18F252 running on a 20MHz external crystal.
'
' Hardware Connections:
' PIC18F252 -> 74HC595
' ---------------------------------
' RC0 (Pin 16) -> DS (Serial Data In, Pin 14)
' RC1 (Pin 17) -> SHCP (Shift Register Clock, Pin 11)
' RC2 (Pin 18) -> STCP (Storage Register Clock / Latch, Pin 12)
'===============================================================================
' --- Device and Clock Configuration ---
Device = 18F252
Clock = 20
Include "Utils.bas"
' --- Configuration Fuses ---
Config
OSC = HS, ' High-speed crystal oscillator
WDT = Off, ' Watchdog Timer disabled
LVP = Off, ' Low-Voltage Programming disabled
BOR = Off, ' Brown-out Reset disabled
PWRT = Off ' Power-up Timer disabled
' --- Pin Aliases for 74HC595 ---
Dim DataPin As PORTC.0 ' Alias for the Data pin (DS)
Dim ShiftPin As PORTC.1 ' Alias for the Shift Clock pin (SHCP)
Dim LatchPin As PORTC.2 ' Alias for the Latch/Storage pin (STCP)
' --- Array Declaration ---
' This array holds the patterns for a Cylon scanner animation.
' The array has 14 elements (indices 0 to 13).
Dim Patterns(14) As Byte
Dim idx As Byte ' A variable to use as an index for the array
' --- Custom Subroutine to send a byte ---
' This subroutine manually sends a byte of data, bit by bit.
Sub ShiftOutByte(pData As Byte)
Dim i As Byte
Dim TempData As Byte
TempData = pData ' Use a local copy to avoid changing original data
For i = 0 To 7
' Check the most significant bit (MSB)
If (TempData And %10000000) > 0 Then
High(DataPin)
Else
Low(DataPin)
End If
' Pulse the shift clock to send the bit
High(ShiftPin)
Low(ShiftPin)
' Shift the data to the left to get the next bit
TempData = TempData << 1
Next
End Sub
' --- Subroutine to initialize the patterns ---
Sub InitPatterns()
Patterns(0) = %00000001
Patterns(1) = %00000010
Patterns(2) = %00000100
Patterns(3) = %00001000
Patterns(4) = %00010000
Patterns(5) = %00100000
Patterns(6) = %01000000
Patterns(7) = %10000000
Patterns(8) = %01000000
Patterns(9) = %00100000
Patterns(10) = %00010000
Patterns(11) = %00001000
Patterns(12) = %00000100
Patterns(13) = %00000010
End Sub
' --- Main Program ---
' Main()
' Configure port directions
SetAllDigital()
Output (PORTC.0)
Output (PORTC.1)
Output (PORTC.2)
' Load the animation data into the array
InitPatterns()
' Main program loop
While True
' Loop through each pattern in the array
For idx = 0 To 13
' Send the current pattern from the array to the 74HC595
ShiftOutByte(Patterns(idx))
' Pulse the latch pin to display the new pattern
High(LatchPin)
DelayUS(10)
Low(LatchPin)
' Wait a short time before showing the next pattern
DelayMS(60)
Next
Wend
'End
'===============================================================================
' File: main_8x_1D_array.bas
' Author: burt and AI
'
' Created: August 2, 2025
'
' Description:
' This version uses a 1D array to simulate a 2D array, which is more
' compatible with BASIC compilers. It controls 8 daisy-chained 74HC595s.
'===============================================================================
' --- Device and Clock Configuration ---
Device = 18F252
Clock = 20
Include "Utils.bas"
' --- Configuration Fuses ---
Config
OSC = HS, WDT = Off, LVP = Off, BOR = Off, PWRT = Off
' --- Pin Aliases for 74HC595 ---
Dim DataPin As PORTC.0 ' Alias for the Data pin (DS)
Dim ShiftPin As PORTC.1 ' Alias for the Shift Clock pin (SHCP)
Dim LatchPin As PORTC.2 ' Alias for the Latch/Storage pin (STCP)
' --- 1D Array Declaration for 64 Outputs ---
' We simulate a 2D array with one large 1D array. 63 frames * 8 bytes = 504 bytes.
' Array indices will be 0 to 503.
Dim Patterns(503) As Byte
Dim frame_idx As Byte ' Index for the animation frame
Dim byte_idx As Byte ' Index for the byte within a frame
' The calculated index can exceed 255, so it must be a Word (16-bit)
Dim index As Word
' --- Custom Subroutine to send one byte ---
' This subroutine is unchanged.
Sub ShiftOutByte(pData As Byte)
Dim i As Byte
Dim TempData As Byte
TempData = pData
For i = 0 To 7
If (TempData And %10000000) > 0 Then
High(DataPin)
Else
Low(DataPin)
End If
High(ShiftPin)
Low(ShiftPin)
TempData = TempData << 1
Next
End Sub
' --- Subroutine to initialize the 1D patterns ---
Sub InitPatterns()
Dim bit_pos As Byte
Dim value As Byte
' Clear the entire array first
Clear(Patterns)
' Create the forward sweep
For frame_idx = 0 To 62
bit_pos = frame_idx + 1
' Calculate which byte the bit is in and its value
byte_idx = 7 - (bit_pos / 8)
value = 1 << (bit_pos Mod 8)
' Calculate the position in our 1D array
index = (frame_idx * 8) + byte_idx
' Store the value at the calculated index
Patterns(index) = value
Next
End Sub
' --- Main Program ---
' Main()
' Configure port directions
SetAllDigital()
Output (PORTC.0)
Output (PORTC.1)
Output (PORTC.2)
' Load the animation data into the 1D array
InitPatterns()
' Main program loop
While True
' Loop through each frame of the 64-bit animation
For frame_idx = 0 To 62
' --- Send all 8 bytes for the current frame ---
For byte_idx = 0 To 7
' Calculate the index in our "flattened" 1D array
index = (frame_idx * 8) + byte_idx
' Send the byte from the calculated position
ShiftOutByte(Patterns(index))
Next
' Now that all 64 bits are sent, pulse the latch
High(LatchPin)
DelayUS(10)
Low(LatchPin)
' Wait before showing the next frame
DelayMS(25)
Next
Wend
'End
It's about the easiest way to expand port pins. But at least a tiny amount of understanding bits and bytes is needed.its really simple