Hi all, newbie again. I added a HPWM output to the filter code in the previous thread so I could watch the rise of the filter response. In this example I had to push a swith to activate the filter.
If I placed the PWMon command at the begining of the code it would cause the PWM to output a 50% duty cycle at 37.5 kHz at start up. Since the PWM code was in the loop after switch closure it would go to the correct response after the switch was on.
The question is why does turning the pWM on before it was need cause this? And were does it get the idea it wants to run at 50% at 37.5 kHz?
Regards, Ed.
Here is the code:
'A first order lag with PWM output
'Tested 02-16-09
'Chip model
#chip 16F88, 20
'Set the pin directions
dir portA.4 in 'input start signal pin #3
dir portA.0 in 'input test signal pin #17 (an0)wire to +5
dir portB.4 out 'output signal pin #10
dir portB.5 out 'set start pulse on pin #11
dir portB.0 out 'set pwm output
'init on power up
set pulse off
dim Input as word
dim outputn as word
dim output as word
'PWMon 'Placing here causes PWM to output 50% at 37.5kHz!
outputn = 0 'Initial condition
PWMDC = 0 'Initial condition
tp = 10 'time constant in seconds (ratio of tp/dt in this case tp=1 dt=.1sec)
ready: 'Waiting for inputs
if trig on then 'must keep pulse on
set indicator on
gosub FirstLag
end if
wait 1 ms 'set sample rate to 1000Hz
goto ready
sub FirstLag 'reads input and computes output
do
Input = readad10(an0) 'get new data
output = (input-outputn)/tp + outputn 'compute output
outputn = output 'set n-1 value of output
PWMon 'Placing here fixes the problem
PWMDC = output/4 'scale PWM
HPWM 1, 10, PWMDC 'output PWM
wait 100 ms 'set sample rate = dt
'diagnostic
if output >= 646 then set pulse on '1-e^-1
end sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Per the help file, you would only use PWMOn if the PWM_Freq and PWM_Duty were constants. I think the PWM header file sets a default if no prior initialization is done.
Now that you have your filter on the PWM, I like the extra kick it gives you on start up.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Kent, thanks again!!!! well DAHH! in the help file it says: "In situations that do not require a specific PWM frequency, the PWM frequency should equal approximately 1 five-hundredth the clock speed of the PIC (ie 40 Khz on a 20 MHz chip)."
So that is were the it gets the number for frequency, I guess it likes 50% if nothing is specified
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all, newbie again. I added a HPWM output to the filter code in the previous thread so I could watch the rise of the filter response. In this example I had to push a swith to activate the filter.
If I placed the PWMon command at the begining of the code it would cause the PWM to output a 50% duty cycle at 37.5 kHz at start up. Since the PWM code was in the loop after switch closure it would go to the correct response after the switch was on.
The question is why does turning the pWM on before it was need cause this? And were does it get the idea it wants to run at 50% at 37.5 kHz?
Regards, Ed.
Here is the code:
'A first order lag with PWM output
'Tested 02-16-09
'Chip model
#chip 16F88, 20
'Set the pin directions
dir portA.4 in 'input start signal pin #3
dir portA.0 in 'input test signal pin #17 (an0)wire to +5
dir portB.4 out 'output signal pin #10
dir portB.5 out 'set start pulse on pin #11
dir portB.0 out 'set pwm output
'name I/O
#define trig portA.4
#define pulse portB.4
#define indicator portB.5
'init on power up
set pulse off
dim Input as word
dim outputn as word
dim output as word
'PWMon 'Placing here causes PWM to output 50% at 37.5kHz!
outputn = 0 'Initial condition
PWMDC = 0 'Initial condition
tp = 10 'time constant in seconds (ratio of tp/dt in this case tp=1 dt=.1sec)
ready: 'Waiting for inputs
if trig on then 'must keep pulse on
set indicator on
gosub FirstLag
end if
wait 1 ms 'set sample rate to 1000Hz
goto ready
sub FirstLag 'reads input and computes output
do
Input = readad10(an0) 'get new data
output = (input-outputn)/tp + outputn 'compute output
outputn = output 'set n-1 value of output
PWMon 'Placing here fixes the problem
PWMDC = output/4 'scale PWM
HPWM 1, 10, PWMDC 'output PWM
wait 100 ms 'set sample rate = dt
'diagnostic
if output >= 646 then set pulse on '1-e^-1
end sub
Per the help file, you would only use PWMOn if the PWM_Freq and PWM_Duty were constants. I think the PWM header file sets a default if no prior initialization is done.
Now that you have your filter on the PWM, I like the extra kick it gives you on start up.
Kent, thanks again!!!! well DAHH! in the help file it says: "In situations that do not require a specific PWM frequency, the PWM frequency should equal approximately 1 five-hundredth the clock speed of the PIC (ie 40 Khz on a 20 MHz chip)."
So that is were the it gets the number for frequency, I guess it likes 50% if nothing is specified