PWM to control 8 LEDS using SF Basic

Post here to discuss programming with swordfish basic

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

PWM to control 8 LEDS using SF Basic

Post by bitfogav » Sun Aug 15, 2010 8:17 pm

Heres a way of controlling 8 LEDs connected to one PORT, some of this code was from digital-diy.com but I made it so it controlled 8 leds and added more functions, the code might not be very efficient but its works and it would make a great lighting effect if you was to make Led arrays from it. :)

Code: Select all

Device = 18F4550    // this is what microchip we are using
Clock = 48          // makes the micro work at 12 million instructions per sec (using a 20mhz crystal)

Config  PLLDIV = 5,         // configure PPL and our OSC settings    
        CPUDIV = OSC1_PLL2,
        USBDIV = 2,       
        FOSC = HSPLL_HS 
        
Include "RandGen.bas"     // this module will give us a random numbers between 0 - 255      
 
// Our Variables        
Dim TMR2IE As PIE1.1,           // TMR2 interrupt enable    
    TMR2IF As PIR1.1,           // TMR2 overflow flag    
    TMR2ON As T2CON.2,          // Enables TMR2 to begin incrementing    

    Signal_Pin As PORTD.0           // Signal output to frequency meter (debugging)

Dim one_Pin As PORTB.0,         // Out control pins...
    two_Pin As PORTB.1,    
    three_Pin As PORTB.2,
    four_Pin As PORTB.3,
    five_Pin As PORTB.4,
    six_Pin As PORTB.5,
    seven_Pin As PORTB.6,
    eight_Pin As PORTB.7
           
        
Dim one_Duty As Byte,           // user pwm control 
    two_Duty As Byte,    
    three_Duty As Byte,
    four_Duty As Byte, 
    five_Duty As Byte, 
    six_Duty As Byte, 
    seven_Duty As Byte, 
    eight_Duty As Byte,
    
    all_Duty As Byte,
        
    one_DutyVal As Byte,    
    two_DutyVal As Byte,    
    three_DutyVal As Byte,
    four_DutyVal As Byte,
    five_DutyVal As Byte,
    six_DutyVal As Byte,
    seven_DutyVal As Byte,
    eight_DutyVal As Byte, 
    
    all_DutyVal As Byte,
        
    RandomVal As Byte           // used to store the random number
    
Dim uS As Word,    
    mS As Word            

// Our Interrupt Routine    
Interrupt TMR2_Interrupt()    
    High(Signal_Pin)            // used for debugging  
    Save(0)                     // Back up system variables       
    If TMR2IF = 1 Then          // Check if the interrupt was from TMR2           
        TMR2IF = 0              // Clear the TMR2 interrupt flag        
        uS = uS + 50        
        If uS >= 1000 Then            
            uS = uS - 1000            
            Inc(mS)        
        EndIf               
            Inc(one_DutyVal)        
            Inc(two_DutyVal)        
            Inc(three_DutyVal)
            Inc(four_DutyVal) 
            Inc(five_DutyVal) 
            Inc(six_DutyVal) 
            Inc(seven_DutyVal) 
            Inc(eight_DutyVal)
            
            Inc(all_DutyVal)
                     
    If one_DutyVal > one_Duty Or one_Duty = 0 Then            
        one_Pin = 0        
        Else            
        one_Pin = 1        
    EndIf        

    If two_DutyVal > two_Duty Or two_Duty = 0 Then            
        two_Pin = 0        
        Else            
        two_Pin = 1        
    EndIf        

    If three_DutyVal > three_Duty Or three_Duty = 0 Then            
        three_Pin = 0        
        Else            
        three_Pin = 1        
    EndIf
    
    If four_DutyVal > four_Duty Or four_Duty = 0 Then            
        four_Pin = 0        
        Else            
        four_Pin = 1        
    EndIf 
    
    If five_DutyVal > five_Duty Or five_Duty = 0 Then            
        five_Pin = 0        
        Else            
        five_Pin = 1        
    EndIf 
    
    If six_DutyVal > six_Duty Or six_Duty = 0 Then            
        six_Pin = 0        
        Else            
        six_Pin = 1        
    EndIf 
    
    If seven_DutyVal > seven_Duty Or seven_Duty = 0 Then            
        seven_Pin = 0        
        Else            
        seven_Pin = 1        
    EndIf 
    
    If eight_DutyVal > eight_Duty Or eight_Duty = 0 Then            
        eight_Pin = 0        
        Else            
        eight_Pin = 1        
    EndIf 
    
    If all_DutyVal > all_Duty Or all_Duty = 0 Then            
       Toggle (Signal_Pin)  // used for debugging or can be
        Else
        one_Pin = 1
        two_Pin = 1
        three_Pin = 1
        four_Pin = 1
        five_Pin = 1 
        six_Pin = 1 
        seven_Pin = 1 
        eight_Pin = 1             
        
    EndIf 
                       
    EndIf                        
        Restore             // Restore system variables    
        Low(Signal_Pin)     // used for debugging
End Interrupt 

// Our Timer2 setup
Private Sub TMR2_Initialize()       // setting up TMR2 for a 15micro-sec interrupt
        TMR2ON = 0                  // Disable TMR2    
        TMR2IE = 0                  // Turn off TMR2 interrupts        
        
        PR2 = 149                   // TMR2 Period register PR2    
        T2CON = %00000001           // T2CON 0:1 = Prescale                              
                                    //        00 = Prescale is 1:1                              
                                    //        01 = Prescale is 1:4                              
                                    //        1x = Prescale is 1:16                                                               
                                    //      3:6 = Postscale                                            
                                    //     0000 = 1:1 postscale                              
                                    //     0001 = 1:2 postscale                             
                                    //     0010 = 1:3 postscale...                              
                                    //     1111 = 1:16 postscale     
        
        
        TMR2 = 0                  // Reset TMR2 Value       
        TMR2IE = 1                // Enable TMR2 interrupts    
        TMR2ON = 1                // Enable TMR2 to increment    
        Enable(TMR2_Interrupt)
End Sub 

// Our Initializations
Low(one_Pin)
Low(two_Pin)
Low(three_Pin)
Low(four_Pin)
Low(five_Pin)
Low(six_Pin)
Low(seven_Pin)
Low(eight_Pin)

one_Duty = 0
two_Duty = 0
three_Duty = 0
four_Duty = 0
five_Duty = 0
six_Duty = 0
seven_Duty = 0
eight_Duty = 0

all_Duty = 0

one_DutyVal = 0
two_DutyVal = 0
three_DutyVal = 0
four_DutyVal = 0
five_DutyVal = 0
six_DutyVal = 0
seven_DutyVal = 0
eight_DutyVal = 0 

all_DutyVal = 0

uS = 0
mS = 0 

RandGen.Initialize(128)             // Initialize the Random Number generator (set to 128 but can be changed)
TMR2_Initialize                     // Setup and enable TMR2 


// Start Of Program...

While True                          // Create an infinite loop    
        RandomVal = RandGen.Rand    // Grab a random number from 0 to 255      
            Select RandomVal        // Find out what colour to increase/decrease       

            // Led One
                Case 0 To 15            
                    While one_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim              
                        Dec(one_Duty)            
                    Wend
            // Led Two        
                Case 31 To 46            
                    While two_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim                
                        Dec(two_Duty)            
                    Wend 
            // Led Three       
                Case 62 To 77            
                    While three_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim                
                        Dec(three_Duty)            
                    Wend   
            // Led Four      
                Case 93 To 108            
                    While four_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim                
                        Dec(four_Duty)            
                    Wend  
            // Led Five      
                Case 124 To 139            
                    While five_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim                
                        Dec(five_Duty)            
                    Wend  
            // Led Six     
                Case 155 To 170            
                    While six_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim                
                        Dec(six_Duty)            
                    Wend  
            // Led Seven       
                Case 186 To 202            
                    While seven_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim                
                        Dec(seven_Duty)            
                    Wend  
            // Led Eight        
                Case 218 To 233            
                    While eight_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 19  // this it the time it takes for the led to get to full dim                
                        Dec(eight_Duty)            
                    Wend
            // All Leds
                Case 250 To 255         // if we come here then all leds slowly dim (one by one)
                    If (PORTB.0) = 1 Then
                        While one_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(one_Duty)            
                        Wend
                    EndIf
                    If (PORTB.1) = 1 Then 
                        While two_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(two_Duty)            
                        Wend
                    EndIf
                    If (PORTB.2) = 1 Then        
                        While three_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(three_Duty)            
                        Wend
                    EndIf
                    If (PORTB.3) = 1 Then
                        While four_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(four_Duty)            
                        Wend
                    EndIf
                    If (PORTB.4) = 1 Then
                        While five_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(five_Duty)            
                        Wend
                    EndIf
                    If (PORTB.5) = 1 Then
                        While six_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(six_Duty)            
                        Wend
                    EndIf
                    If (PORTB.6) = 1 Then
                        While seven_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(seven_Duty)            
                        Wend
                    EndIf
                    If (PORTB.7) = 1 Then
                        While eight_Duty > 0                
                            mS = 0                
                            Repeat                
                            Until mS = 19  // this it the time it takes for the led to get to full dim                
                            Dec(eight_Duty)            
                        Wend
                    EndIf

                    // now we turn on all leds to full brightness and then dim them
                        DelayMS(100)
                    While all_Duty <255> 0                
                        mS = 0                
                        Repeat                
                        Until mS = 22  // this it the time it takes for the led to get to full dim                
                        Dec(all_Duty)
                    Wend           
                         
            End Select           
Wend

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Sun Aug 15, 2010 9:36 pm

Nice piece of code, It looks like you are zooming ahead with SF basic programming :)

Actually one thing I didn't know you could do is use case statements in the way that you did. e.g.

Code: Select all

Case 93 To 108
I thought you could only specify one case for each statement. Thanks for that!

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Sun Aug 15, 2010 9:39 pm

Yeah I found that you can use CASE in this way, rather than using something like this:

Code: Select all

If counter >= 100 And counter <= 200 Then

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 15 guests