PWM Module for Swordfish
Download
Variable Frequency PWM Example
// import PWM module...
Include
"PWM.bas"
// local duty variable...
Dim Duty As
Word
// main program...
If PWM.SetFreq(5000)
Then
While true
Duty = 0
Repeat
PWM.SetDuty1(Duty)
Inc(Duty,10)
DelayMS(10)
Until Duty >
PWM.MaxDuty
Wend
EndIf
As you can see with the above code, it's
very easy to use. When you call SetFreq(),
the prescale, PR2 and max duty value
are automatically calculated. Note that
SetFreq() returns true if the frequency could be set,
false otherwise. If it returns false, you need to check your
datasheet (or use
PIC MultiCalc to verify). The chances are
the frequency you have entered is not supported at the clock
frequency you are using.
The call to
SetFreq() also has an optional percentage parameter to
set the initial duty. For example,
PWM.SetFreq(5000, 50)
will set the PWM module at 5KHz with an
initial duty cycle of 50%. In the code example above, the
repeat...until loop iterates through until Duty >
PWM.MaxDuty. However, rather than call
PWM.SetDuty() you can use
PWM.SetDutyPercent() to range the
duty cycle from 0 to 100. For example,
// import PWM module...
Include
"PWM.bas"
// local duty variable...
Dim Duty As
Byte
// main program...
PWM.SetFreq(5000)
While true
Duty = 0
Repeat
PWM.SetDuty1Percent(Duty)
Inc(Duty)
DelayMS(10)
Until Duty > 100
Wend
Written by
David John Barker & Warren Schroeder. A simple module for fixed and variable frequency
PWM.
Link:
What is a Swordfish Library?

 | Site Tutorial Index |
|  | 16F PIC Examples |
|  | 18F PIC Examples |
|  | Handy Tips |