Shop OBEX P1 Docs P2 Docs Learn Events
r/c circuit problem — Parallax Forums

r/c circuit problem

I'm using an r/c circuit to read a pot, and output pwm on a different pin. The circuit diagram I started with shows a 1uf cap and 10k ohm pot. In the serial terminal I see the pot value go up and voltage rise on the pwm pin as the pot goes from zero to 10k ohm. When the pot is almost to the mechanical limit, the pwm pin voltage goes to zero and stays there. I changed the capacitance to .8uf and this problem went away. It works fine now, but I would like to understand why the output pin quit working. I do have a 100ohm resistor between the pot and ground so at the zero end it won't be a short, but the problem occurs at the higher resistance end. The r/c pin goes to +capacitor, and one side of pot. Wiper goes to -capacitor through 100ohm res. to ground.

Comments

  • I should add, this happens with no load on the pwm pin.
  • AribaAriba Posts: 2,690
    Without seeing the code, it's just guessing:

    Your measured poti value may go higher than the PWM range, and you don't have it limited to the max possible PWM value. In this case the PWM uses just the lower bits and starts again at zero for higher values.

    Say your PWM works with 8 bits and goes therefore from 0 to 255. If you give it a value of 256, it's the same as 0, a value of 257 is the same as 1 and so on.

    Andy
  • That makes sense, except to fix it the code stayed the same and the capacitance had to go down a little. Here's the code, Pub go method is from the book "programming the propeller with spin". I used the r/c circuit from the book also with the exception (added after I noticed the problem) of the 100 ohm resistor in series with the pot so it can't be a direct short while at the zero end. But the problem occurs toward the 10k end of the pot.
    CON
            _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
            _xinfreq = 5_000_000
    potline=15
    VAR
      long  startcnt
      long endcnt
      long delay
      long potvalue
      long spstack[50] 
    OBJ
      pst      : "parallax serial terminal"
      
    PUB go
    cognew(motorsp,@spstack)
    pst.start(115_200)
    waitcnt(clkfreq/10+cnt)                       '10k pot...1uf capacitor
    repeat                                        'pinout pg 114 prop program book
                                                  'value scale 1-255
        dira[potline]~~
        outa[potline]~~
        waitcnt(4000+cnt)
        dira[potline]~
        startcnt:=cnt
        repeat
        while ina[potline]~~
        endcnt:=cnt
        delay:=((endcnt-startcnt)-1184)
        if delay>630_000
           delay:=630_000
        potvalue:=(delay/2220)     '2220
        potvalue<#=255
        potvalue#>=1
        pst.str(string("pot_value="))
        pst.dec(potvalue)
        pst.str(string(13))
        pst.str(string("delay ="))
        pst.dec(delay)
       
    
    pub motorsp|p,t,wt,o
    dira[14]~~  'pwm pin
    
    
    
    ctra[30..26] := %00100 ' Configure Counter A to NCO
    ctra[5..0] := 14
    frqa := 1
    
    p:=313        '313*255=80_000
    t:=80_000     '10ms
                          
                
      repeat
        phsa := -o         'output pwm                          
        o:=(potvalue*p)                              
        wt:=(t-o)          'time-pwm=wait time 
        wt+=cnt                      
        
        waitcnt(wt)        'wait output plus remainder of 10 ms
                          
      
    
  • AribaAriba Posts: 2,690
    mikea wrote: »
    That makes sense, except to fix it the code stayed the same and the capacitance had to go down a little.
    ...
    [/code]

    With lower capacitance the max. poti value you get will be lower.

    But I see you limit the potivalue anyway to 1..255. There is a little chance that the potivalue gets picked by the PWM cog before it is limited, so it would be better to limit it in the same line as you do the scaling:
      potvalue:=(delay/2220) <#255 #>1
    

    I think the actual problem is in your PWM code. You modify the PWM frequency according the potivalue, which is not how this kind of PWM normally is done. The pwm periode and therefore your wt value should be constant, and the puls modulation is done by the counter phsa values. Something like:
      t := cnt
      repeat
        phsa := -potvalue*p         'output pulse
        t += 256*p                          
        waitcnt(t)                  'wait pwm periode
    
    With lower p values you can make the PWM much faster, if you need it. Or you can improve the resolution of the PWM.

    Andy
  • JonnyMacJonnyMac Posts: 9,104
    edited 2018-04-21 13:06
    At EFX-TEK we have a small audio player that uses RC pot circuits to set the volume; I did a bit of emperical testing so that I could scale the return value from 0..100. You may want to do that for your project so that you always get back 0..255. Here's my code.
    pub read_pot(p)
    
    '' Reads RC circuit on pin p
    '' -- assumes 10K pot + 0.01uF cap
    '' -- takes about 2ms
    
      ctra := (%01000 << 26) | p                                    ' ctra in pos detect mode
      frqa := 1
    
      io.high(p)                                                    ' charge the cap
      time.pause(1)
    
      phsa := 0                                                     ' clear accumulator
      dira[p] := 0                                                  ' start discharge
      repeat                                                        ' wait for discharge to finish
      while (ina[p])
      ctra := 0                                                     ' disable ctra
    
      ' scaled for AP-8+ at 80MHz
      ' -- for volume, 0 to 100
    
      return (0 #> ((phsa >> 4) - 37) <# 400) >> 2                  ' return scaled value
    
  • In case it's helpful, I've attached a simple 2-channel PWM object. Like your code it's written in Spin, but allows you to control two channels by using both counters (you don't have to use both). It also let's you set the resolution. For lighting apps I use 255 which is DMX compatible. For motor control, I use 1000 which gives me 0 to 100% in 0.1% increments. I recently used this in a pan/tilt head controller for video cameras.
  • ErNaErNa Posts: 1,752
    Why not use the Sigma Delta ADC to measure the pot's voltage?
  • Thanks for the help everyone. Can sigma delta adc be done without an ic on the prop? In the obex it seems to always reference a chip.
  • jmgjmg Posts: 15,173
    mikea wrote: »
    Can sigma delta adc be done without an ic on the prop? In the obex it seems to always reference a chip.

    Yes, but it does need 2 pins, - one IP sense pin, and a Charge Balancing output pin + sundry passives.]
    A Sigma Delta does not have the Cap C value, or Pot R value as part of the timing equation - that makes it more repeatable, especially in production volumes.

    SDM is also linear with voltage, and thus linear with 3 terminal wired pot rotation.

Sign In or Register to comment.