The worst programmer ever to grace this forum - ME!


Closed Thread
Results 1 to 40 of 50

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Places to get help. for PBP.

    Micro Chip web site for data sheets, other places may not have the current one.
    Melabs web site, can not beat the source for examples.
    rentron.com - Bruce's web site. Very good examples.
    This forum, Darrel has a google search in the FAQ section.

    When you are ready for advanced stuff go to Darrel's web site.

    Any place else and who knows...
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    I've just realised that my little program which counts the number of time a switch has been closed (a magnetic switch, which is closed as 10 successive magnets glued to the perimeter of a DC fan motor pass it by!)...isn't counting correctly - & I don't know why (though I suspect it';s either down to the debounce paramters or perhaps the length of time taken to 'do stuff' in between successive magnets).

    here's my simple code (stripped down for clarity)...

    RB7pin10 VAR PortB.7 ;explicity name the switch pin for button command.
    RA3pin4 VAR PortA.4 ;explicity name the switch pin for button command.
    Temp VAR BYTE ; this is used for debounce/delay of the fan switch)
    total_counter VAR WORD ; this will count the total pulses rx'ed from magnets.
    pitch_counter VAR BYTE ; this will utlimately derive the pitch thread (a divider)
    direction_counter VAR word ; this will changed traversal direction word for counts bigger than 255
    pulse VAR PortC.0 ; stepper feed pin 16 (to pin 11 on stepper IC)
    direction var PortC.1 ;direction level pin 15 (to pin 14 on Stepper IC)


    stepper_enable VAR PortC.3 ; Pin 7 this logic level 0 enables the stepper IC (stepper IC Pin 15)

    Start:

    total_counter = 0 ; make sure we start from zero
    direction_counter = 0 ; make sure we start from zero
    direction = 1 ;we want the feed stepper to move left/right first.
    low stepper_enable ;enable the stepper chip.

    Main:
    Button RB7pin10, 0, 255, 0, Temp, 1, magnetic_switch_closed ; monitor the magnetic switch
    goto Main ;if switch not closed, loop until it does close.

    magnetic_switch_closed: ;ok, so switch is now closed - let's count!
    total_counter = total_counter +1 'increment counter to track number of magnets passing switch.
    IF total_counter = 500 THEN
    goto finish ' if using 10 magnets, after 50 turns of the motor exit the program
    endif
    pulse_the_stepper:
    pulsout Pulse, 20 ; pulse the stepper on Port C.0 with 20us pulse.
    Goto main ;return to start & monitor for the switch being closed again

    finish:
    HIGH stepper_enable ;disable the stepper chip
    End




    So to my question....

    Is there a more scientific way to approach this?

    The motor starts off slow, & then increases under manual control up to a speed somewhere between 150RPM & 350RPM. So the rate of magnets passing the switch can be anywhere between 0 per second & 58 passes per second (350/60 = 5.8 turns per second...there are 10 magnets)

  3. #3
    Join Date
    Apr 2009
    Location
    Boise, Id
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    I see a couple of things that make me wonder, your "Temp" Var should be set to 0 at the beggining before you call "Button", if not "Button" won't work correctly.

    Also, you are looking for a switch being closed at rates up to 58 times per second. 1/58 = 17 ms, so ever 17 milli seconds a magnet will sail past your switch, closing it for a moment, then it will re-open. If your magnets cover 50% of the circumference that would give you a 50% duty cycle. So the switch would be closed for 17/2 = 8.5 ms and open for 8.5 ms. In reality, your switch is probably closed about 10 % of the time or 1.7 ms.

    In your code :

    Code:
    Button RB7pin10, 0, 255, 0, Temp, 1, magnetic_switch_closed
    You have delay set at 255, this will perform debounce, but no auto repeat.

    Default debounce is 10 ms, so the switch needs to be closed for 10 ms before you go to "switch_closed"

    You might try a delay of 0, this shuts off the debounce.

    Also you can change the debounce delay:

    DEFINE BUTTON_PAUSE 1 'set button debouce to 1 ms

    1 ms is still cutting it close.

    Or you can skip the button all together and just read the port, something like:

    Code:
    If RB7pin10 = 0 Then switch_closed
    You can read the state of the switch again in the "switch_closed" code, if it's still closed continue, if not jump back out. That performes a high speed debounce. You can also use PAUSEUS before you double check, so you can wait a couple of micro seconds then double check the switch is really closed.

    Just some idea's.

    Shane

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Hank,
    Why not just use count statement, store count in a variable ?
    Looks like you want to find the end of each revolution and then increment the feed over so the wire does not overlap, yes?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Apr 2009
    Location
    Boise, Id
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    just another thought on my last post. If you check the state of "magnetic_switch_closed" and jump into you're code where you increment your counter. Before you jump out and look for it being closed again, you should look for it to be open.

    If not then you could count the same swith close multiple times all for the same instance.

    something like:

    Code:
    if RB7pin10 = 1 then main ' make sure the switch opens before looking for another close
    Else stay in loop...
    Shane

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Firstly ....Shane/Joe, thank you for taking the trouble to respond (esp to a basket case of a kludger like me!)

    Ok, to your points...

    Shane you are right about the temp variablle needing to be set to zero...I didn't post up that bit (my program has a lot of variables going on, but I stripped it back to basics so as not to cloud the issue...but I can see it's caused probs!)

    Next....I think you've nailed the issue (my head was fogged last night after realising my program wasn't counting the revs right!)...essentially, my program traps the switch being closed (sort of!), but not the corresponding open. Therefore in reality, my program could see the switch being closed, head off to do some stuff elsewhere in the program, come back to check the switch & by bad luck/timing the next magnet could be passing so it thinks the same (original) magnet is closing the switch. therefore I need to wait for the switch to close, then open, then have my program act.

    Joe... re the counter/variable melarkey - remember, I'm very new to this. I've almost certainly done a tutorial somewhwere & simply applied what I learnt there to my program! (I haven't a lot of time to dedicate to being a slick programmer, so I just throw into the program the little bit that I know!). Could you be so kind as to outline the tighter code for that little increment you've honed in on as being a bit 'first lesson at PICBASIC evening class'!). BTW ...I use this magnet/switch 'count' to derive when to send a pulse to a stepper. This stepper I'm sending the pulse to, moves in synchronicity with the main motor, back & forward (the idea being to neatly feed coppoer wire on to the main turning motor). It's really cool on the odd (seemingly fluke) times I've had it working!

    Shane... the magnets are actually glued around the circumference of a wooden ring (& this ring itself is glued to the fan blades!). The diameter of this ring is about 10cm (4" if you're still in imperial) which makes for a circumference of 31cm ...the magnets themselves are about 1cm wide & there are 10 of them. So the magnets take up a third of the circumference - meaning a duty cycle of 33%?

    Therefore, at a maximum of 350RPM, the magnets will pass by the switch at about 58 times per second. This works out at every 17ms....because of the magnet width, this yields a duty cycle of 33% - therefore the switch will actually be closed for about 5.6ms, every 17ms

    Now my head still spins at the thought of building in the debounce aspect, so
    with these 'knowns' I have, which would be the best approach to ensure an accurate switch 'closing' count?


    (BTW, I'm thinking now that maybe an optical pickup would be a much better solution, as this removes the debounce aspect & I can have more resolution -I will look into this, but for now I'd really like to get the cheap 'n dirty solution working!)

  7. #7
    Join Date
    Apr 2009
    Location
    Boise, Id
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Building a debounce isn't too hard, if it's even needed. It just depends on the magnetic switch you are using, if it's solid state you won't need to do much more than check if it's closed, being the way I am, I'd double check it again before actually stepping the moter etc. If it's a mechanical relay, that will need a "real" debounce and maybe some additional external hardware, like a cap.

    To debounce you need a loop that checks if the switch is closed. If it is then increase a debounce var. and check it again, if it's not closed set your debounce var = 0 and keep checking. Determine the amount of time / number if times you want to increase your debounce var. you can put a pauseus 100 and recheck if it's still closed, and when your debounce var = 10 then you know the switch has been closed for at least 1 ms ( 100 us * 10 = 1ms)

    Joe S. had the idea of using count, I've used it and it's simple.

    Code:
    count porta.1, 150 , freqval
    The above code counts the pulses on porta.1 over 150 ms and stores the value in the Var "freqval".

    In your case you would count for 10 ms? something like this:

    Code:
    Counting_Mags:
    count portx.x, 10, mag_count ' count port x pin x for 100 ms
    Num_Mags = Num_Mags + mag_count
    
    if Num_Mags > 9 then One_Rev ' counted at least ten magnets so one revolution has occured, so step motor.
    
    goto Counting_Mags
    
    One_Rev:
    Num_Mags =0 ' reset count
    'do the rest of you stuff here
    
    Goto Counting_Mags ' go back and count another revolution
    Joe S. may have a better idea, and better code for you, above is where I would start, I'm no pro at this myself. I'd use the above code as an example, I just typed it out, so it still needs more work.

  8. #8
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    Joe... re the counter/variable melarkey - remember, I'm very new to this. I've almost certainly done a tutorial somewhwere & simply applied what I learnt there to my program! (I haven't a lot of time to dedicate to being a<b> slick programmer</b>, so I just throw into the program the little bit that I know!). Could you be so kind as to outline the tighter code for that little increment you've honed in on as being a bit 'first lesson at PICBASIC evening class'!). BTW ...I use this magnet/switch 'count' to derive when to send a pulse to a stepper. This stepper I'm sending the pulse to, moves in synchronicity with the main motor, back & forward (the idea being to neatly feed coppoer wire on to the main turning motor). It's really cool on the odd (seemingly fluke) times I've had it working!
    <b>HA HA HA, ME EITHER !</b> I wrote a Tachometer program, using count, look it over as an exemplar of using count. The code is in post #9. http://www.picbasic.co.uk/forum/showthread.php?t=9037

    Quote Originally Posted by HankMcSpank View Post
    Now my head still spins at the thought of building in the debounce aspect, so
    with these 'knowns' I have, which would be the best approach to ensure an accurate switch 'closing' count?


    (BTW, I'm thinking now that maybe an optical pickup would be a much better solution, as this removes the debounce aspect & I can have more resolution -I will look into this, but for now I'd really like to get the cheap 'n dirty solution working!)
    Optical can be cheap and reliable, magnetic is good too, thinking hall effect switch. Do you really need 36 degree resolution (10 magnets)? Using an interrupt might be your solution to all this. I do not think I would use a magneto mechanical (reed) switch as a pickup, but using a hall effect would free you of all that debounce nonsense. Sam used my code with 1 magnet for his Tach. and says it works well. As for optical, tear open an old mouse and you will find a PIC and 2 optical pickups using shutter wheels. People use printed encoders for tabletop robots all the time. A coil of wire, magnets, and a comparator input will work too, that is what works most auto ignitions, in fact I used an old car distributor while working out my tach. code. A 555 timer can be used as a wave shaper if you are not ready to try comparators yet.
    Last edited by Archangel; - 28th April 2009 at 17:20.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Melabs U2 Programmer Command Line Options
    By Robert Wells in forum General
    Replies: 5
    Last Post: - 3rd July 2009, 02:11
  2. problems with USB programmer
    By malc-c in forum General
    Replies: 7
    Last Post: - 10th May 2007, 20:14
  3. USB programmer problems
    By uiucee2003 in forum USB
    Replies: 2
    Last Post: - 15th August 2006, 23:47
  4. General Programmer Questions
    By mslaney in forum General
    Replies: 1
    Last Post: - 17th December 2004, 18:16

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts