View previous topic :: View next topic |
Author |
Message |
rlait
Joined: 18 May 2004 Posts: 2
|
Best way to count switch transitions |
Posted: Fri Jul 02, 2004 3:18 pm |
|
|
I am working on a project that requires keeping track of how many times a reed switch is closed over a predetermined period of time. The expected frequency of switch activations is anywhere from 1hz to 10Khz or so. In between counting the switch activations the PIC will be updating an LCD display with the activation count as well as RTC data.
I was planning to use a 16F84.
Any suggestions or snips of code would be appreciated.
Thanks,
RussL |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Fri Jul 02, 2004 5:40 pm |
|
|
Take a look at the input capture module of the PIC. There are also a few examples included with the CCS compiler showing you how to do something similar. |
|
|
Gabriel Caffese
Joined: 09 Oct 2003 Posts: 39 Location: LA PLATA, ARGENTINA
|
|
Posted: Fri Jul 02, 2004 7:32 pm |
|
|
I would use INT_EXT.
You say time is predetermined.
So, open a "timed window", and count how many interrupts you receive in that time.
One thing I don�t know, is if INT_EXT would detect 10000 interrupts/sec.
Anyway, if you�re using a reed switch, this mechanical device won�t let you switch 10000 times a second.
Gabriel.- |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jul 02, 2004 7:34 pm |
|
|
The 16F84 has no capture module and is not recommended by Microchip for new designs as there is an updated version, the 16F84A, which is even a bit cheaper.
Are there specific reasons why you proposed an 16F84? There are many other PICs out there that are cheaper and have more functionality. What are your other design requirements, like:
- max. number of pins.
- How is the RTC connected? SPI or I2C?
- How is the LCD connected?
- How much memory do you need for storing your display texts?
- etc.
The counting of the 10kHz switching is easy to do. Easiest is to choose a pic with a capture module that will do all the counting for you in hardware, but implementing it in software under interrupt control is also easy. |
|
|
Ttelmah Guest
|
Re: Best way to count switch transitions |
Posted: Sat Jul 03, 2004 2:06 am |
|
|
rlait wrote: | I am working on a project that requires keeping track of how many times a reed switch is closed over a predetermined period of time. The expected frequency of switch activations is anywhere from 1hz to 10Khz or so. In between counting the switch activations the PIC will be updating an LCD display with the activation count as well as RTC data.
I was planning to use a 16F84.
Any suggestions or snips of code would be appreciated.
Thanks,
RussL |
Realistically, a 16F628, would be a _much_ better choice.
This chip (like most of it's larger brethren), has a hardware CCP module, designed to count pulses. If you only need to count one edge, use it 'as is', otherwise add external logic to give a pulse from each edge. It is possible to count at this sort of rate using the 16F84, but you would have to be very careful if pulses are not going to be missed.
Are you sure about the 'rate'?. No reed switch will normally oprate at this sort of rate, and even those that will, have very short lives. If this is something like a tacho pulse, remember that motor speeds are in rpm, not rps. Even then, the long term reliability of such a device would be very suspect, it would be wise to consider something like a Hall effect switch instead....
Best Wishes |
|
|
ninjanick
Joined: 25 May 2004 Posts: 25
|
|
Posted: Tue Jul 06, 2004 12:58 pm |
|
|
Using the CCP or INT_EXT on a PIC16, how would one count both edges as fast as possible? I was thinking of toggling the edge select bit from L_TO_H and back to H_TO_L repeatadly, but wasn't sure of the latency this requires. Any suggestions? |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Tue Jul 06, 2004 2:31 pm |
|
|
I would think about using timer1 input to count pulses and another timer to keep time. The main drawback is that it does not perform debouncing. If you setup the timebase correctly you can get a direct reading. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jul 07, 2004 7:18 am |
|
|
10kHz is awful fast for a reed switch. Plus you are going to have switch bounce problems. Is there any chance you can use a hall effect semiconductor sensor instead? They are faster and don't bounce. See if you can put a scope on your reed switch when it is running at 10kHz and see how badly it behaves. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
rlait
Joined: 18 May 2004 Posts: 2
|
|
Posted: Thu Jul 08, 2004 10:36 am |
|
|
Thanks for the responses. All were helpful.
At this point I have a rough h/w and s/w prototype operating on the bench.
I ended up using the edge trigger on the B0/Int pin.
To those that commented about the operating frequency of the reed switch you are certainly correct regarding the inability of a reed switch to operate at 10khz. As I do not have the actual sending unit in hand I will have to wait for that piece to see what the real rates are, and/or how they implemented the design. Based upon the actual application I think the pulses are more than likely going to be in hz as opposed to khz.
For now I have an asynchronous pulse generator that I am using in place of the actual sending unit. Now that I have the unit functioning I will have to crank up the generator to see just how fast the whole thing can go.
Thanks again for the suggestions. |
|
|
|