buy hot The Fixer cool video Year One download Kasabian album nice Terminator Salvation hit cool track Jordin Sparks USA Whatever Works video download Check My Brain melodies nice video Finding Nemo nice Pop - Various Artists hit cheap DIVX Year One

digital-diy.com

Swordfish Code Snippet - Structures Print E-mail
( 1 Vote )
  
Saturday, 04 April 2009 10:09

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 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,

 

Dim Time As TTime

 

Access To an individual field within the variable Time is achieved by using the dot (.) 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

 

Comments (3)
  • BobbyD  - Dare I say that I can follow this code?

    Is Swordfish that much more user friendly as this appears to be so logical in format.
    Keep up the Great Work Graham!

  • Pegasus

    I never knew I could do that with pic basic (well with Swordfish anyhow)

    @ BobbyD,
    I’ve just compiled a program with the above program, but you will need to include a TMR setup routine as Graham has re-moved it to keep the program focused toward the use of structures

  • digid

    Thanks for your help. As a newbie on programming, like me , you give just the right information to get start on this great hobby. :lol:

Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
Security
Please input the anti-spam code that you can read in the image.
Last Updated ( Tuesday, 14 April 2009 10:52 )
 

Whos Online

  • dha
  • ta1dr
  • Jon Chandler

Forum Activity