Swordfish Code Snippet - Structures

swordfish

Structures are a powerful addition to the Swordfish arsenal, and here's a quick example of how to use them;

Structure TTime
 Hours As Byte
 Minutes As Byte
 Seconds As Byte
End Structure

The declaration above informs the compiler that TTime contains three members, all of which are byte types (Hours, Minutes and Seconds). We can now create a variable of Type TTime, in exactly the same way as you would any other compiler type, such as Byte or Float,

Dim Time As TTime

 

Access To an individual member within the variable Time is achieved by using the period (.) notation,

Time.Hours = 9
Time.Minutes = 59
Time.Seconds = 30

 

Here's an example in full context (note that the Timer setup procedure 'Setup_Timer' has been removed for ease of explanation);

Device = 18F452
Clock = 20
 
Structure TTime
 Hours As Byte
 Minutes As Byte
 Seconds As Byte
 Milli_Seconds As Word
End Structure
 
Dim Time As TTime
 
 
Interrupt TMR_Interrupt()
 Inc(Time.Milli_Seconds)
 If Time.Milli_Seconds = 1000 Then
 Time.Milli_Seconds = 0
 Time.Seconds = Time.Seconds + 1
 If Time.Seconds = 60 Then 
 Time.Seconds = 0
 Time.Minutes = Time.Minutes + 1
 If Time.Minutes = 60 Then
 Time.Hours = Time.Hours + 1
 EndIf
 EndIf
 EndIf
End Interrupt
 
Setup_Timer
Enable(TMR_Interrupt)
 
While True
Wend

 

The member structure is just that, a structure. You could use the same structure for other variables.

Forum Activity

Member Access