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 led Dimmer 18f1220

Status
Not open for further replies.

be80be

Well-Known Member
A simple PWM led dimmer with the 18F1220 here the code you can use it with and 18fxxx just need swordfish basic. And Thanks to 3v0
Code:
Dim Duty As Word               // local duty variable...
Dim sw0 As PORTA.0
Dim sw1 As PORTA.1
Input (sw0)
Input (sw1)

SetAllDigital 

// main program...
If PWM.SetFreq(5000) Then
Duty = 0
 While true
      PWM.SetDutyPercent(Duty)
      If ( (sw0 = 1) And (Duty < 10) ) Then     
      Inc(Duty)
      DelayMS(10)
      While sw0 = 1 Wend
      EndIf
      PWM.SetDutyPercent(Duty)
      If ( (sw1 = 1) And (Duty >0) ) Then     
      Dec(Duty)
      DelayMS(10)
      while sw1 = 1 wend 
      EndIf
      
   Wend
EndIf

Now the video not that good it better live hard to film a led


And here the same code but I'm dimming a ac light bulb

 
Last edited by a moderator:
Can you post the full code and all the modules. Becasue when i compile your code with the swordfish PWM.BAS i get a few errors.
 
Here the whole code lol I didn't post the chip and osc you may want to use a 18f1230 I just happen to have 18f1220. But any way I added that to just to make it easy.
Code:
 Device = 18F1220
Clock = 8
Config OSC = INTIO2

Include "INTOSC8.bas"
Include "PW.bas"            // import PWM module...
Include "Utils.bas"
Dim Duty As Word               // local duty variable...
Dim sw0 As PORTA.0
Dim sw1 As PORTA.1
Input (sw0)
Input (sw1)

SetAllDigital 

// main program...
If PWM.SetFreq(5000) Then
Duty = 0
 While true
      PWM.SetDutyPercent(Duty)
      If ( (sw0 = 1) And (Duty < 10) ) Then     
      Inc(Duty)
      DelayMS(10)
      While sw0 = 1 Wend
      EndIf
      PWM.SetDutyPercent(Duty)
      If ( (sw1 = 1) And (Duty >0) ) Then     
      Dec(Duty)
      DelayMS(10)
      While sw1 = 1 Wend 
      EndIf
      
   Wend
EndIf
you need to add 2 files to your user User Library the IntOSC8 and PW I uploaded them
 

Attachments

  • PW.zip
    1.2 KB · Views: 258
  • IntOSC8.zip
    154 bytes · Views: 220
I thought some of you may want to see how you do this so i took some
Pictures and some more just to walk you thew it.

**broken link removed**

**broken link removed**

**broken link removed**

**broken link removed**

**broken link removed**

**broken link removed**

Some are not to clear but after 4 sets of battery
I stopped trying to get a clear shot. Maybe some one will have as much fun as I did with
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top