Swordfish - Structures

Status
Not open for further replies.

gramo

New Member
Just learning new things here and there, thought someone out there would benefit from this.

Structures are a powerful addition to the Swordfish arsenal, and here's a quick example of how to use them;
Code:
[B]Structure [/B]TTime
    Hours [B]As Byte[/B]
    Minutes [B]As Byte[/B]
    Seconds [B]As Byte[/B]
E[B]nd Structure[/B]
The declaration above informs the compiler that TTime contains three Byte fields (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,
Code:
[B]Dim [/B]Time [B]As [/B]TTime
Access To an individual field within the variable Time is achieved by using the dot (.) notation,
Code:
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);
Code:
[B]Device [/B]= 18F452
[B]Clock [/B]= 20

[B]Structure [/B]TTime
    Hours [B]As Byte[/B]
    Minutes [B]As Byte[/B]
    Seconds [B]As Byte[/B]
    Milli_Seconds [B]As Word
End Structure

Dim [/B]Time [B]As [/B]TTime


[B]Interrupt [/B]TMR_Interrupt()
    [B]Inc[/B](Time.Milli_Seconds)
    [B]If [/B]Time.Milli_Seconds = 1000 [B]Then[/B]
        Time.Milli_Seconds = 0
        Time.Seconds = Time.Seconds + 1
        [B]If [/B]Time.Seconds = 60 [B]Then [/B]
            Time.Seconds = 0
            Time.Minutes = Time.Minutes + 1
            [B]If [/B]Time.Minutes = 60 [B]Then[/B]
                Time.Hours = Time.Hours + 1
            [B]EndIf
        EndIf
    EndIf
End Interrupt[/B]

Setup_Timer
[B]Enable[/B](TMR_Interrupt)
[B]
While True
Wend[/B]
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…