LED flickers. That's a problem
lardom
Posts: 1,659
I trying to design a stable potentiometer app to control pulse width to use with a BLDC. The problem is that the LED flickers between calls to the potentiometer object. If I shorten the waitcnt in the object called "TestRcDecayLarry" the LED gets dimmer. In the object "pwmTestMethodWTicks C" I tried changing 'repeat 20'. I'm making wild guesses.
Can this be corrected with PASM (which I've never coded) or is there a basic flaw in the design?
This is the top object.
This is the "TestRcDecayLarry" object.
This is "pwmTestMethodWTicks C"
Can this be corrected with PASM (which I've never coded) or is there a basic flaw in the design?
This is the top object.
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
OBJ
TP : "pwmTestMethodWTicks C"
POT : "TestRcDecayLarry"
PUB Main | M
POT.init
TP.TestPwm
waitcnt(clkfreq + cnt)
repeat
M := POT.Main * 400
TP.TestTwo(M)
This is the "TestRcDecayLarry" object.
PUB Init
ctra[30..26] := %01000 ' Set mode to "POS detector"
ctra[5..0] := 6
frqa := 1
PUB Main : result | time
dira[6] := outa[6] := 1
waitcnt(clkfreq/100_000 + cnt) ' The shorter the wait time gets the dimmer the led
phsa~
dira[6]~
waitcnt(200_000 + cnt) ' This is the waitcnt that I modified
time := (phsa - 624) #> 0
Case time
0..3210 : time := 0
3211..6420 : time := 1
6421..9630 : time := 2
9631..12840 : time := 3
12841..16050 : time := 4
16051..19260 : time := 5
19261..22470 : time := 6
22471..25680 : time := 7
25681..28890 : time := 8
28891..32100 : time := 9
32101..35310 : time := 10
35311..38520 : time := 11
38521..41730 : time := 12
41731..44940 : time := 13
44941..48150 : time := 14
48151..51360 : time := 15
51361..54570 : time := 16
54571..57780 : time := 17
57781..60990 : time := 18
60991..64200 : time := 19
result := time
This is "pwmTestMethodWTicks C"
PUB TestPwm
ctrb[30..26] := %00100 ' Configure Counter A to NCO
ctrb[5..0] := 7
frqb := 1
PUB TestTwo(pulse) : result | tc, tHa, t, tInc, I
dira[7]~~
tInc := 80 ' time increment
tC := 8_000 * tInc ' 10kHz cycle time
tHa := pulse * tInc ' Use time increment to set up high time
t := cnt ' Mark counter time
result := pulse
repeat 20 ' Repeat PWM signal
phsb := -tHa ' Set up the pulse
t += tC ' Calculate next cycle repeat
waitcnt(t) ' Wait for next cycle


Comments
You're trying to do two different operations ... read a potentiometer and generate a PWM signal. You're using a cog counter to generate a single pulse, but have to reinitialize the counter with each pulse. When you're measuring the pot value, you're not generating pulses. You need to split off the PWM generation into a separate cog so it doesn't have to stop when your main cog does something else.
How about just using one of the PWM objects from the object exchange that does this for you?