18F Code Snippet - Structures

    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

 Skip Navigation Links.

Collapse Site Tutorial IndexSite Tutorial Index
Expand 16F PIC Examples16F PIC Examples
Collapse 18F PIC Examples18F PIC Examples
LED's
Switches
Expand 7 Segment Displays7 Segment Displays
LCD's
Expand 7 Segment Displays7 Segment Displays
ADC
ADC (Another Example)
EEPROM's
DS1307
Expand RS232 and UARTRS232 and UART
DS18B20
External ADC
Hall Effect Sensor
Pulse Width Modulation (PWM)
Infrared UART
Swordfish Modules
Collapse Code SnippetsCode Snippets
SF Libraries
18F Transition Guide
Internal Oscillator
PLL
Structures
Multi Tasking
Expand Internal TimersInternal Timers
Expand InterruptsInterrupts
Expand Handy TipsHandy Tips