PWMPal problem during Ramping
jbalat
Posts: 96
Hi just hoping someone can help me debug..
I am reading an incoming square wave (3.9v 30Hz-120Hz) which varies in frequency (on Pin7)
I modify the frequency by a factor (mfactor)
Then I output it with the PWMPal on Pin12
I connected and oscilliscope and while ramping sometimes I get 2 lows or 2 highs stuck together?
==============================================================
' PULSIN2.BS2
' This program uses PULSIN to measure an incoming pulse
' and outputs the pulse with a lower frequency
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
PpPin PIN 0 ' PWMPAL Serial I/O
Pulse PIN 7 ' pulse input pin
'
[noparse][[/noparse] Constants BS2 Only ]
W CON 2 ' -- 2.0 us per unit
F CON 0 ' -- 0 fractional units
T9600 CON 84
T19K2 CON 32
T38K4 CON 6
PpBaud CON T38K4
MotorNum CON 1 ' PWMPAL motor output
'
[noparse][[/noparse] Variables ]
time VAR Word
freqIN VAR Word 'Frequency of incoming pulse
mfactor VAR Word 'Output factor 0-100
OnTime VAR Word 'On/Off time 25us units
prevOnTime VAR Word 'compare with current on time
status VAR Byte 'motor control status
'
[noparse][[/noparse] Initialization ]
mfactor = 90 'Default 100%
OnTime = 556
prevOnTime = 555
Setup:
SEROUT 0, PpBaud, [noparse][[/noparse]"!PWMM0"] ' reset all pwm pins
PAUSE 10
SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMSP", %00010001] ' set phase, counter 1 enabled
SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMSS", %00010000] ' enable PWM1 software control
Main:
PULSIN Pulse, 0, time ' measure positive pulse
IF (time > 0) THEN ' if not 0
'DEBUG HOME,
'DEC time, " units ", CLREOL ' display raw input
time = (time * W) + (time ** F) ' adjust for Stamp
'DEBUG CR,
'DEC time, " us " ' display microseconds
freqIN = 1000 /(2 * time / 1000) ' Only on Time calculated (x2)
'DEBUG CR,
'DEC freqIN , " Hz Input " ' display Total Input freq
OnTime = time / 25
OnTime = OnTime * 100 / mfactor
'DEBUG CR,
'DEC (mfactor * freqIN / 100) , " Hz Output " ' display Total Output freq
IF prevOnTime <> Ontime THEN GOSUB SetPWM
ELSE
'DEBUG CLS, "Out of Range" ' else error message
ENDIF
PAUSE 1
GOTO Main
SetPWM:
'DEBUG CR, "CHANGE FREQ ", DEC OnTime , " "
'Set for 50% duty cycle
SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMM1", OnTime.BYTE0, OnTime.BYTE1, OnTime.BYTE0, OnTime.BYTE1]
prevOnTime = OnTime
RETURN
END
I am reading an incoming square wave (3.9v 30Hz-120Hz) which varies in frequency (on Pin7)
I modify the frequency by a factor (mfactor)
Then I output it with the PWMPal on Pin12
I connected and oscilliscope and while ramping sometimes I get 2 lows or 2 highs stuck together?
==============================================================
' PULSIN2.BS2
' This program uses PULSIN to measure an incoming pulse
' and outputs the pulse with a lower frequency
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
PpPin PIN 0 ' PWMPAL Serial I/O
Pulse PIN 7 ' pulse input pin
'
[noparse][[/noparse] Constants BS2 Only ]
W CON 2 ' -- 2.0 us per unit
F CON 0 ' -- 0 fractional units
T9600 CON 84
T19K2 CON 32
T38K4 CON 6
PpBaud CON T38K4
MotorNum CON 1 ' PWMPAL motor output
'
[noparse][[/noparse] Variables ]
time VAR Word
freqIN VAR Word 'Frequency of incoming pulse
mfactor VAR Word 'Output factor 0-100
OnTime VAR Word 'On/Off time 25us units
prevOnTime VAR Word 'compare with current on time
status VAR Byte 'motor control status
'
[noparse][[/noparse] Initialization ]
mfactor = 90 'Default 100%
OnTime = 556
prevOnTime = 555
Setup:
SEROUT 0, PpBaud, [noparse][[/noparse]"!PWMM0"] ' reset all pwm pins
PAUSE 10
SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMSP", %00010001] ' set phase, counter 1 enabled
SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMSS", %00010000] ' enable PWM1 software control
Main:
PULSIN Pulse, 0, time ' measure positive pulse
IF (time > 0) THEN ' if not 0
'DEBUG HOME,
'DEC time, " units ", CLREOL ' display raw input
time = (time * W) + (time ** F) ' adjust for Stamp
'DEBUG CR,
'DEC time, " us " ' display microseconds
freqIN = 1000 /(2 * time / 1000) ' Only on Time calculated (x2)
'DEBUG CR,
'DEC freqIN , " Hz Input " ' display Total Input freq
OnTime = time / 25
OnTime = OnTime * 100 / mfactor
'DEBUG CR,
'DEC (mfactor * freqIN / 100) , " Hz Output " ' display Total Output freq
IF prevOnTime <> Ontime THEN GOSUB SetPWM
ELSE
'DEBUG CLS, "Out of Range" ' else error message
ENDIF
PAUSE 1
GOTO Main
SetPWM:
'DEBUG CR, "CHANGE FREQ ", DEC OnTime , " "
'Set for 50% duty cycle
SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMM1", OnTime.BYTE0, OnTime.BYTE1, OnTime.BYTE0, OnTime.BYTE1]
prevOnTime = OnTime
RETURN
END
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don't worry. Be happy
Debugs been disable to increase program execution
Hey I was thinking instead of using PULSIN could I use Hardware counter on the PWM to count the incoming pulse.. I guess this would only help if I could calculate the frequency from the counter ?
Not sure if it would fix the ramp issue.
And not sure how the "PWMSP" and "PWMSS" commands actually does
Ideally if the frequency is changing quick you probably want to take up the slack during the off time of the pulse ?
Post Edited (jbalat) : 7/14/2009 11:41:27 PM GMT