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
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