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;
And heres the code;
Some of the features are;Code:Device = 18F452 Clock = 20 #option _Segment_Port = PORTC // Define the Keypad PORT #option _Segment_TRIS = GetTRIS(_Segment_Port) // Nice way to automatically get the TRIS // automatically from a selected port Include "ISRTimer.bas" Include "utils.bas" Dim 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 Dim 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 Const Timer1 = 0, Timer2 = 1 // Update_Segments event, mutiplex the segment data... Sub 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 End Sub Function 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 End Function Sub 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) End Sub // Increment_Number event will increment the register Number Sub Increment_Number() Inc(Number) If Number = 100 Then Number = 0 EndIf EncodeSegmentData End Sub Timer.Initialize(2) // initialize the timer module... // initialise each timer Timer.Items(Timer1).Interval = 5 // 5ms Timer.Items(Timer1).OnTimer = @Update_Segments // timer1 event handler Timer.Items(Timer2).Interval = 500 // 500ms Timer.Items(Timer2).OnTimer = @Increment_Number // timer2 event handler Number = 0 // Initialize variables EncodeSegmentData Segment_TRIS = %00000000 // Make the segment data lines outputs Low(Segment_1_TRIS) Low(Segment_2_TRIS) Timer.Items(Timer1).Enabled = True // enable the timers... Timer.Items(Timer2).Enabled = True Timer.Start // start processing all timers... // main program loop... While True Wend
* 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
The ISRTimer library creates the required interrupt settings to perform the multiplexing and number incrementing 'on the fly'Code:// main program loop... While True Wend


Reply With Quote