Shop OBEX P1 Docs P2 Docs Learn Events
RCTime and PWM? — Parallax Forums

RCTime and PWM?

Kirk FraserKirk Fraser Posts: 364
edited 2011-06-20 18:06 in Propeller 1
My program testing RCTime and PWM fails on both. RCTime result is always zero at all settings of the 10K pot. PWM sets both FETs to 3.3 volts instead of pin8 to 3.3 and pin9 to 0. Please tell me what's wrong. Thanks.
var
  long us, ms
OBJ
  Debug: "FullDuplexSerial"     
con
  _clkmode = xtal1+pll16x     'For Debug
  _clkfreq = 100_000_000      'For Debug
  cntMin     = 400      ' Minimum waitcnt value to prevent lock-up
  
PUB CodeTest   | x
    Debug.start(31, 30, 0, 115200)
    waitcnt(clkfreq*3+cnt)  'For Debug Startup
    dira[15]~~             'set pin to out  
    repeat
      dira[13]~~             'set pin to out 
      outa[13] := 1          ' charge the cap HIGH
      PAUSE(1)               '   for 1 ms
      x := RCTime(13,1)
      Debug.Str(string(13,"Test dec: "))
      Debug.dec(x)
      Debug.Str(string(13,"Test hex: ")) 
      Debug.hex(x,1)
      Debug.Str(string(13,"Test bin: ")) 
      Debug.bin(x,12)
      Debug.Str(string(10))
      outa[15] := 1
      PAUSE(1)
      outa[15] := 0 
      PWM(8,255,1000)
      PWM(9,0,1000) 

PUB RCTIME (Pin,State):Duration | ClkStart, ClkStop
{{
   Reads RCTime on Pin starting at State, returns discharge time, returns in 1uS units
     dira[Pin]~~                 ' Set as output
     outa[Pin]:=1                ' Set high
     Pause(10)                  ' Allow to charge
     x := RCTime(Pin,1)          ' Measure RCTime
     DEBUG(x)                   ' Display
}}

   DIRA[Pin]~
   ClkStart := cnt                                         ' Save counter for start time
   waitpne(State << pin, |< Pin, 0)                        ' Wait for opposite state to end
   clkStop := cnt                                          ' Save stop time
   Duration := (clkStop - ClkStart)/uS                    ' calculate in 1us resolution

PUB PAUSE(Duration) | clkCycles
{{ Causes a pause for the duration in mS
     BS2.Pause(1000)   ' 1 second pause
}}

   clkCycles := Duration * ms-2300 #> cntMin               ' duration * clk cycles for ms
   waitcnt( clkCycles + cnt )                              ' wait until clk gets there

PUB PWM(Pin, Duty, Duration) | htime, ltime, Loop_Dur
{{Produces PWM on pin at 0-255 for duration in mS
   BS2.PWM(5,128,1000)
}}
  PWM_Set(Pin, Duty, 8)
  Pause(Duration)
  PWM_Set(Pin, 0, 8)     
                                                                 
Pub PWM_Set(Pin, Duty, Resolution) | Scale
{{Produces constant PWM on the specified pin.
   Resolution is the bit of resolution desired, for 0 to 2n-1
      8,  duty is 0 to 255
      12, duty is 0 to 4096  etc

   Repeat x from 0 to 4096
      BS2.PWM_Set(2,x,10)  ' pin 2, x value, 10-bit
      BS2.Pause(10)
}}
  if duty == 0                                  ' freq = 0 turns off square wave
     ctra := 0                                  ' Set CTRA/B to 0
     dira[pin]~                                 ' Make pin input
  else
     Scale := 2_147_483_647 / (1<< (Resolution-1))  ' Calculate scale
      ctra[30..26] := %00110            ' Set ctra to DUTY mode
      ctra[5..0] := pin                 ' Set ctra's APIN
      frqa := duty * scale              ' Set frqa register
      dira[pin]~~                       ' set direction

{{   MIT License included by reference.
     http://obex.parallax.com/objects/30
}}

Here's my board
100_2035.jpg
so the pins can be verified.

The extra parts are:
RCTime from 10K pot / ,01uf / 220 ohm to pin.
PWM to IRF3708 FETs with 1N4001's to control a valve.
LED's to show state in next program.

Also, is there a way to increase the resolution on PWM from 8 bits to 12 or from 256 to 4096? I saw other PWM programs that use the video circuitry, should I be using one of those? Thanks.
1024 x 771 - 134K

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-06-17 11:32
    I assume you're using the code from the PEK? I just took a closer look and it doesn't look like it.

    I'd suggest using the code from the PEK.

    I don't see anything connected to the wiper of your POT. Is there supposed to be?

    I also notice you're using really thin wire for a breadboard. It looks like the wire inside of phone lines of Cat5 cable? I have a bunch of wire like that myself but I find it doesn't work well in breadboards. I think you ought to try some thicker wire with your circuits.

    I haven't use PWM much myself but I think the only factor that would limit the resolution would be the frequency. The higher the frequency you use the less you'll be able to increase the resolution.

    Duane
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-06-17 12:23
    Duane,

    Thanks, I just ordered a PEK book yesterday. The software I copied from the Object Exchange, BS2 functions. I'll look more into the PEK.

    The pot wiper has two connectors, the one in the middle of three is connected to a 220 ohm resistor which goes to the prop pin. (It's an unusual pot but it works for position sensing. Many other pots introduce trouble when mounted, with varying resistance due to shaft or resistor plate deformations due to torque or ground problems.)

    You are correct, my wire is Cat 5. It did cost me one error on this prototype and probably more. I can get 22 Gauge stranded wire at the local auto shop. Will stranded work?

    I can lower the frequency but I'll look into the PEK software first, it may have something to give higher resolution.

    Thanks,
    Kirk
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-06-17 12:35
    The PEK book is also available in the help section of the Prop Tool.

    No, stranded wire will not work. You really need to use solid wire of the correct gauge (22 AWG). If you have a Radio Shack near by, they sell jumper wire in a little plastic box. It is more expensive to buy in the little box than to buy a couple of spools of like the one I linked to but it might be a faster way to get some jumper wire.

    Duane
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-06-17 14:56
    Duane,

    The PEK book in the Prop Tool has nothing searchable on RCTime but there may be enough on time measuring to figure something out with the other code. Fortunately it does have PWM code which works which I should be able to make work for me. Also, I'm thinking there was some demo code I tried which loops to slowly raise the output from 0 to 1 which may also help get the FET to work in a proportional manner, I hope.

    Kirk.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-06-17 15:05
    The PEK uses the term "RC decay." See page 122.
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-06-17 16:19
    The variety of PWM code presents a problem. What is actual PWM defined as? The PWM produced by a BS2 seemed to work to vary my valve coils as requested but for the Prop I have to dig in to the nuts and bolts of PWM and I don't know how they did it in BS2 as a pre-RC signal, where you have to add an RC circuit to get a measurable voltage variation. It wasn't really measurable as presented from the BS2 pin, even with an OScope. So how do I produce that BS2 output with a Prop? It isn't simple On for a while then Off for a while like the PEK code produces.

    Thanks for the RC decay tip.
  • SarielSariel Posts: 182
    edited 2011-06-20 12:29
    Duane Degn, Post #4
    No, stranded wire will not work. You really need to use solid wire of the correct gauge (22 AWG).

    Absolutely will work. I ahve been doing it this way for quite some time, and have had no problems at all. Just put a thin tin to the ends, so that it is covered, but the strands are still visable. Check out this page for a good couple pictures.

    [HTML]
    http://store.curiousinventor.com/guides/how_to_solder/prepare/
    [/HTML]

    The key is uniformity. Make sure it is evenly covered, no icicles, and be carefull of wicking up into the insulation. this can cause more problems. I find doing it this way gives a bit more flex to your work, in case you are like me, and ahve certain "Modules" that are on PCB's that you recycle during development of different projects. Maybe one with a Shift register for quick and dirty IO expansions... one with a RTC and battery (also eliminates problems with XTALs, etc.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-06-20 16:33
    Sariel,

    The link you provided is for using stranded wire with a circuit board not a breadboard.

    It would be nice if one could use stranded wire with breadboards. Standed wire generally flexes better than solid wire. When I looked for information about using stranded wire with breadboards I found this site which says:
    Do not insert stranded wire or soldered wire into the breadboard socket. If you must have stranded wire (as with an inductor or transformer lead), solder (or use a wire nut to connect) the stranded wire to a short length of solid hookup wire, and insert only the solid wire into the breadboard.

    Duane
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-06-20 16:48
    So how do I produce that BS2 output with a Prop?

    I don't know enough about the BS2 to answer this. Hopefully someone else here does and will.
    It isn't simple On for a while then Off for a while like the PEK code produces.

    Then I really don't understand what the BS2 does. I thought that that's what PWM did; control the time on and time off depending on the duty cycle and frequency.

    Again, I hope some other forum member can answer your PWM questions.

    Duane
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-06-20 18:06
    The BS2's "PWM" produces a fixed pulse-width with a frequency that changes, low duty has a slower repetition rate than high duty.

    "True PWM" is a pulse-width that varies with a constant repetition rate [frequency] (like servo control pulses),
    I like pulses coming at regular intervals (clockwork).
Sign In or Register to comment.