Swordfish Example

Status
Not open for further replies.

gramo

New Member
I was just writing a conversion for some of my 16F projects to Swordfish, and realized that this little program covers many aspects of Swordfish all in one. It’s a simple 7 segment multiplexing program for the following circuit;

**broken link removed**

And heres the code;

Code:
[B]Device [/B]= 18F452
[B]Clock [/B]= 20

#[B]option [/B]_Segment_Port = PORTC                      // Define the Keypad PORT
#[B]option [/B]_Segment_TRIS = GetTRIS(_Segment_Port)     // Nice way to automatically get the TRIS
                                                   //  automatically from a selected port
                                                   
[B]Include [/B]"ISRTimer.bas"  
[B]Include [/B]"utils.bas"

[B]Dim [/B]Segment_1_TRIS As TRISD.1,                     // User must define what pins control
    Segment_2_TRIS As TRISD.0,                     //  each segment
    Segment_1 As PORTD.1,
    Segment_2 As PORTD.0            
[B]Dim [/B]Number As Byte,                                // Program variables that require no
    Segment_Data As _Segment_Port,                 //  user settings
    Segment_TRIS As _Segment_TRIS,
    Segment_1_Data As Byte,
    Segment_2_Data As Byte
[B]Const[/B]
    Timer1 = 0,
    Timer2 = 1
  
// Update_Segments event, mutiplex the segment data...
[B]Sub [/B]Update_Segments()

    If Segment_1 = 1 Then
        Segment_1 = 0
        Segment_Data = Segment_2_Data
        Segment_2 = 1
    Else
        Segment_2 = 0
        Segment_Data = Segment_1_Data
        Segment_1 = 1    
    EndIf

[B]End Sub[/B]

[B]Function [/B]Segment_Encode(Segment_Byte As Byte) As Byte  // Convert a byte into segment information
    Select Segment_Byte
    	    Case 0 
    	   	Result = %00111111
    	    Case 1			
    	   	Result = %00000110
    	    Case 2			
    		Result = %01011011
    	    Case 3			
    	   	Result = %01001111
    	    Case 4			
    	   	Result = %01100110
    	    Case 5			
    	   	Result = %01101101 
    	    Case 6			
       		Result = %01111100
    	    Case 7			
     		Result = %00000111
    	    Case 8			
    		Result = %01111111
    	    Case 9			
     		Result = %01100111		 
    	EndSelect
[B]End Function[/B]

[B]Sub [/B]EncodeSegmentData()                          // Encodes segment data

    Segment_1_Data = Digit(Number,1)             // Extract the first digit & convert it
    Segment_1_Data = Segment_Encode(Segment_1_Data)
    Segment_2_Data = Digit(Number,2)             // Extract the second digit & convert it
    Segment_2_Data = Segment_Encode(Segment_2_Data)

[B]End Sub[/B]

// Increment_Number event will increment the register Number
[B]Sub [/B]Increment_Number()

    Inc(Number)
    If Number = 100 Then 
        Number = 0
    EndIf
    
    EncodeSegmentData

[B]End Sub[/B]


[B]Timer[/B].[B]Initialize[/B](2)                              // initialize the timer module...

// initialise each timer
[B]Timer.Items[/B](Timer1).Interval = 5                 // 5ms
[B]Timer.Items([/B]Timer1).OnTimer = @Update_Segments   // timer1 event handler
[B]Timer.Items[/B](Timer2).Interval = 500               // 500ms
[B]Timer.Items[/B](Timer2).OnTimer = @Increment_Number  // timer2 event handler

Number = 0                                       // Initialize variables
EncodeSegmentData

Segment_TRIS = %00000000                         // Make the segment data lines outputs
[B]Low[/B](Segment_1_TRIS)
[B]Low[/B](Segment_2_TRIS)

[B]Timer.Items[/B](Timer1).Enabled = True               // enable the timers... 
[B]Timer.Items[/B](Timer2).Enabled = True
[B]Timer.Start[/B]                                      // start processing all timers...
 
// main program loop...
[B]While True[/B]

[B]Wend[/B]

Some of the features are;
* A quick sample of the comprehensive 'on compile' commands (#option in this case)
* Swordfish's ISRTimer Library
* Functions
* Sub Procedures


Notice that the main program loop is simply

Code:
// main program loop...
While True

Wend

The ISRTimer library creates the required interrupt settings to perform the multiplexing and number incrementing 'on the fly'
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…