Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

using a RTC in SF basic

MrDEB

Well-Known Member
I have a DS-1302 RTC module but how to use it?
I recall Jon Chandler made a vu meter clock but what he used for timing?
Am planning on a clock that has no hands, just LEDs but precision would be nice.
 
Here some code for mr deb to play with un tested just change to your chip and send out 1 and 0
Code:
'===============================================================================
' 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
 
This code really works
Code:
'===============================================================================
' 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

If you read back through this, um...., mess, you'll see that everything has been explained to MrDEB numerous times, using both the standard Swordfish SHIFT and SPI modules. As far as I have seen, he's still lost in the weeds.

I really don't think replacing the standard modules with additional code that's not likely to be understood is going to change the situation. More power to you if you can guide him to something that works (but I'm not holding my breath).
 
here with arrays
Code:
'===============================================================================
' 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
 
Here's the last freebie
Code:
'===============================================================================
' 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
 
Last edited:
Me and AI getting good at swordfish I don't think i'll ever be good at coding but with AI help I can probably wire some better code its been good at coding if you can get across what you want but it all has some bugs to fix that's my part
 

Latest threads

Back
Top