'===============================================================================
' 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