Shop OBEX P1 Docs P2 Docs Learn Events
LED flickers. That's a problem — Parallax Forums

LED flickers. That's a problem

lardomlardom Posts: 1,659
edited 2012-11-15 11:54 in Propeller 1
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.
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


Potentiometer for pwm.JPG
340 x 271 - 18K

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-15 08:25
    Sounds like a job for "parallel processing".

    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?
  • lardomlardom Posts: 1,659
    edited 2012-11-15 09:08
    Thanks Mike, Cognew! I missed it. I almost always search the OBEX first. I had trouble finding the complete solution I was looking for so I tried to reinvent the wheel. I will do another search.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-15 10:05
    Here's one such object. There are others.
  • lardomlardom Posts: 1,659
    edited 2012-11-15 11:10
    An answer from Mike Green forces me to 'hit the books'. I've reached the point where I have to move beyond Spin! I've had to work hard learning a ton of new concepts to make progress in programming and electronics . Crossword puzzles don't come close. Thank you Mike Green.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-11-15 11:23
    Actually, you can do all this in Spin, you just need to get another cog involved. The reason for using assembly is to allow for higher frequencies. For LED brightness, you don't need high frequencies.
  • lardomlardom Posts: 1,659
    edited 2012-11-15 11:54
    I'm ready to try my hand at assembly. With pasm you read a 32 bit register. The counters are similar in that regard. I was limited to 7.14kHz in Spin by using waitcnt(clkfreq/1_000_000 + cnt). Using waitcnt(8_000 + cnt) I got the 10kHz I was after. The big thing is a sensorless bldc program could require several instructions between commutations. I don't want that possibility to stop me.
Sign In or Register to comment.