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:

be80be

Well-Known Member
When I draw out the circuit I'll post it

mazur50

New Member
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.

be80be

Well-Known Member
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

• 1.2 KB Views: 187
• 154 bytes Views: 158

be80be

Well-Known Member
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.

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:

dumboy

New Member
Hay I'm going to try that out looks real cool

be80be

Well-Known Member
Have fun I like it playing with it.

Status
Not open for further replies.