Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pwm and swordfish basic

Status
Not open for further replies.
Here the whole code and working good thank's to 3v0
Code:
SetAllDigital 

// main program...
SetFreq(5000)
Duty = 0
While true
       PWM.SetDutyPercent(Duty)  
      If ( (sw0 = 1) And (Duty < 10) ) Then     
        Inc(Duty)
        DelayMS(10)
      EndIf
      If ( (sw1 = 1) And (Duty >0) ) Then     
        Dec(Duty)
        DelayMS(20)
      EndIf   
   Wend
End
 
This is what I was saying about switches it works but not to good it's like the denounce is not working all the time
Code:
Device = 18F1220
Clock = 8
Config OSC = INTIO2

Include "INTOSC8.bas"
Include "Utils.bas"              // local duty variable...
Dim sw0 As PORTB.0
Dim led As PORTB.3
Sub Debounce()

    DelayMS(10)                     // Small delay to stop "bouncing"
    
    While sw0 = 1                // Wait for the switch to be de-pressed    
    Wend                            //
    
End Sub
SetAllDigital
Input (sw0)
Output (led)
Low (led)
 While true
               If ( (sw0 = 1) And (led = 0) ) Then //check switch and led 
               led =  1  // turn on led
       ElseIf( (sw0 = 1) And (led = 1) ) Then //check led to see if on ans switch
               led = 0  // turn off led
       EndIf
 Wend
 
Last edited:
You need to start thinking like a computer when you look at code. Also a comment to tell what it does would be nice. You have morphed the PWM program into blinking an LED here.

your main loop is

Code:
 While true
               If ( (sw0 = 1) And (led = 0) ) Then
               led = 1 
       ElseIf( (sw0 = 1) And (led = 1) ) Then
               led = 0  
       EndIf
 Wend

You never call the debounce subroutine. Why would it do anything ?

And because you use a specific switch in the debounce sub you have to write two of them in the PWM code. Given that you can not use each sub more then once just put the debounce code inline.

Code:
While true
               If ( (sw0 = 1) And (led = 0) ) Then
               led = 1 
               DelayMS(5) // Small delay to stop "bouncing"
               While sw0 = 1 
       ElseIf( (sw0 = 1) And (led = 1) ) Then
               led = 0  
               DelayMS(5) // Small delay to stop "bouncing"
               While sw0 = 1 
       EndIf
 Wend
 
I was just trying it out to see if i could get it to work right. Been tryng to think like computer ha ha. I said if I was a computer and some 1 put a sub for switches like that how would I no if they had more switches. dum sub. That's the part i been leaving out
While sw0 = 1
 
hot dam this is it use one switch to turn on or off a led
Code:
Device = 18F1220
Clock = 8
Config OSC = INTIO2

Include "INTOSC8.bas"
Include "Utils.bas"              // local duty variable...
Dim sw0 As PORTB.0
Dim led As PORTB.3
Sub debounce()
    DelayMS(10)                     // Small delay to stop "bouncing"
End Sub
SetAllDigital
Input (sw0)
Output (led)
Low (led)

 while true 
      if ( (sw0 = 1) And (led = 0) ) then   // check switch
       led = 1            // turn on led
       debounce
       While sw0 = 1       // make sure switch reads as good press
       wend
 endif
 
     if ( (sw0 = 1) And (led = 1) )  then  // check switch
      led = 0 
      debounce            // turn off led
      while sw0 = 1
      wend               // make sure switch reads as good press
 endif
 wend
:D
 
Good deal. You've just made an alternate action switch function from a momentary switch. One of the handiest functions that can come in really handy!
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top