Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

A simple 'pulse counting' PIC program needed!!

Status
Not open for further replies.

HankMcSpank

New Member
Hello There,

I'm trying to make a little hobby coil winder...I was going to use logic gates & switches to control a stepper motor with a 'wire feed helix' (ie traversing back/forward after after x counts - to see what I mean, have a look at this YouTube - Guitar pickup winder, coil winder, automatic traversal 27secs in .... albeit, he's using an AVR). But someone made a good point that I really ought to move with the times & start learning about PICs to do this!

Well, let me declare....wrt to the PIC tutorials, I feel I may as well be reading the Hubble Space telescope service manual... I'm still at "Hello World" & just not getting it!! :eek:

I really do need to push on & get a coil wound (so I can in turn crack on with another project that uses them!), so I'm throwing open an appeal for a program that I can at least make a start with!

I'd imagine for someone PIC literate it won't be a lot of effort (esp if modding another donor program they may already have). It's a hard sell becuase I don't have much to offer....except it'll be cool & I will make a youtube video of the results and credit anyone that helps me! :)

Here are my simple requirements.....

Assume an incoming 'stream of pulses' - for the purposes of me being able to play with/decipher any program it might be handy is you assume the pulses are coming in on pin RA0 on a PIC16F690/P - this is the PIC that came with my PICKit 2. (The pulse stream will actually be from a reed switch opening/closing triggered by magnets attached to the perimeter of a motor - there'll actually be 7 magnets glued around the motor, therefore 7 pulses = main motor has turned one revolution)


First set some states....

Set counter 1 = 0 (this will be used to move the wire feed stepper motor one step)
Set counter 2 = 0 (this will be used to change the direction of the stepper motor - thereby traversing the feed wire in the opposite direction)
Set counter 3 = 0 (this will be used to stop the program)

Set output pin RC1 low (changing this pin from low to high will change the direction of the stepper motor)

Set output pin RC2 low (changing this later to to high will ultimately trigger a relay which will stop & brake the main motor)

Switch 0 (RA0) = incoming pulse simulation
Switch 1 (RA1) = reset all counters
Switch 2 (RA2) = run program

Ok now to the requirements...

1) After each incoming pulse, increment counter 1, counter 2, & counter 3.

2) When counter 1 equals "1", send a short pulse to the output pin RC0 (at this stage I'm not sure of the pulse width needed - like I say, it'll be driving a stepper motor, but for testing it might be worth making the pulse width big enough to visibly see an LED light up - therefore useful if the pulse width can be set easily with a variable)

3) clear counter 1. continue this cycle ad infinitum. (in case you're wondering why I need a one in one out counter, it may well be that at a later stage some wire thickness might need 2 pulses in & one pulse out)

4. When counter 2 equals "700" set output pin RC1 high (this will reverse the direction of the stepper - ie the wire feeding onto the main motor will start traversing the other way)

5. clear counter 2.

6. When counter 3 equals 2,000, halt the program & set output pin RC2 high.


Perhaps I'm asking too much...if so, then if anyone could even give a sample bit of code for setting up even part of ithe above (say RA0 as an input to sense a switch & RC0 as an output going high when switch is closed x times)

Many thanks in anticipation!
 
While these are for stamps the code is easily ported to PBasic Pro,you can learn alot here
and the proton forum is another good site to try
the second link shows how to count rpm on a lath
**broken link removed**

**broken link removed**

**broken link removed**
 
(The pulse stream will actually be from a reed switch opening/closing triggered by magnets attached to the perimeter of a motor - there'll actually be 7 magnets glued around the motor, therefore 7 pulses = main motor has turned one revolution)

That seems a bit complicated. If you are using a stepper motor you can just count the number of pulses you send to it to determine if it has turned a revolution or not.
 
Thanks for the comments & links guys.

That seems a bit complicated. If you are using a stepper motor you can just count the number of pulses you send to it to determine if it has turned a revolution or not.

The reason I'm going this route, is that the feed wire (which is essentially moved by the stepper), has to be in 'synch' the main winding motor as it turns (and at all the main motor's variable RPMs).

The solenoid copper wire is 0.15mm thick, therefore for every turn of the main motor, I need the feed wire to be traversed 0.15mm ...this will allow a neatly wound coil. After 'x' number of main motor revolutions, the direction of the wire (stepper) will be reversed & so on.

The only way i know of acheiving such sync between the main motor & the feed wire (stepper) is to have a sensor on the main motor (in this case, the magnets & a reed switch).

since posting, i've now started off on my voyage of 'PIC discovery'...& it's particularly heavy going! (I'm using PIC Basic)
 
Last edited:
Don't forget to do switch debouncing in your code!

I haven't! (edit: I haven't forgot to build in some debounce code!)

I've since finished my PIC program (with some degree of help from the guys over on the PIC Basic forum!). Just got to knock the breadboard up now (to interface the PIC with the stepper motor driver IC)
 
Last edited:
Any mechanical switch - such as the reed switch you are using, is inherently noisy. Each make or break between the switch's contacts, might be seen as many (rather than one) very fast opens and closes of the switch. This is called switch bounce. In order to avoid erroneous signals, you can use debouncing hardware - such as a schmitt trigger. A better solution is to just wait a few microseconds until the signal has had time to settle (as long as this delay is less than the maximum frequency of anticipated switch state changes).
 
Any mechanical switch - such as the reed switch you are using, is inherently noisy. Each make or break between the switch's contacts, might be seen as many (rather than one) very fast opens and closes of the switch. This is called switch bounce. In order to avoid erroneous signals, you can use debouncing hardware - such as a schmitt trigger. A better solution is to just wait a few microseconds until the signal has had time to settle (as long as this delay is less than the maximum frequency of anticipated switch state changes).


Hi Samuel,

Thanks for the input (as it goes I know about debounce)....when I said "I haven't", I was responding to your "Don't forget to build in debounce"....bad grammar on my part (since edited my reply). I have built in debounce into the code so as to avoid false triggering of the pulse counter. :)
 
Status
Not open for further replies.

Latest threads

Back
Top