Swordfish Program - LED Patterns

A simple program that generates some LED patterns. I wrote this to help someone else out on another site, and thought it would be worth sharing here?

The patterns are (in order of sequence):

  • Left to right
  • Right to left
  • Center to outside
  • Outside to center
  • All flashing

The Result

Results are usually more interesting then how-you-got-there, so here they are:

FlashingLEDs

(A simple circuit. 2N2222 NPN transistors could be used to switch banks of LEDs)

 

The Code

The user module "InternalOscillator.bas" can be downloaded from the Swordfish User Module Pack.

LED Patterns
Device = 18F2620
Clock = 32
Config MCLRE = Off
 
Include "InternalOscillator.bas"
 
// Connect each set of LEDs to PORTB (from RB0 to RB4)
 
Sub RightToLeft()
 Dim i As Byte
 For i = 0 To 4
 PORTB.Bits(i) = 1
 DelayMS(500)
 PORTB.Bits(i) = 0 
 Next
End Sub
 
Sub LeftToRight()
 Dim i As Byte
 For i = 4 To 0 Step -1
 PORTB.Bits(i) = 1
 DelayMS(500)
 PORTB.Bits(i) = 0 
 Next
End Sub
 
Sub CenterToOut()
 Dim i As Byte
 For i = 2 To 4
 PORTB.Bits(i) = 1
 PORTB.Bits(4-i) = 1
 DelayMS(500)
 PORTB.Bits(i) = 0
 PORTB.Bits(4-i) = 0 
 Next
End Sub
 
Sub OutToCenter()
 Dim i As Byte
 For i = 0 To 2
 PORTB.Bits(i) = 1
 PORTB.Bits(4-i) = 1
 DelayMS(500)
 PORTB.Bits(i) = 0
 PORTB.Bits(4-i) = 0 
 Next
End Sub
 
Sub AllFlash()
 Dim i As Byte 
 For i = 0 To 7
 PORTB = %00011111 Or PORTB
 DelayMS(500)
 PORTB = %11100000 And PORTB
 DelayMS(500) 
 Next
End Sub
 
// program start...
TRISB = %11100000 And TRISB // make RB0-RB4 outputs
PORTB = %11100000 And PORTB // make all LED outputs low
 
While True
 LeftToRight
 RightToLeft
 CenterToOut
 OutToCenter
 AllFlash
End While

 

Forum Activity

Member Access