Counting pulses with the SX48 hardware counters
![Bean](https://forums.parallax.com/uploads/userpics/855/n505WZVEIPHT1.jpg)
Awhile back Ken posted about counting magneto pulses and driving a servo. I had said at that time that the SX48 hardware timers could easily do both. Well, I spoke too soon...
One little quirk of the SX48 counters is that you cannot read the counter. What the... No, I'm serious. If the counter is used in timer mode, there is little need to read the counter value, it just does something when it reaches R1 or R2. In capture mode, we can read the timer in a round-about way. When you trigger the capture pin, the current count is copied into the capture register (which we CAN read). But the capture pin doesn't do anything in "external counter" mode.
So, being a crafty little programmer I thought. Well I'll just count, then switch to capture mode, toggle the capture pin, and volia I will have the count. And that works with one little problem. The counter is incrementing from the instant you put it into counter mode. So you end up with some extra counts until the code toggles the capture pin. Well, it's better than nothing...
Then I was playing with the external counter mode and discovered something interesting. The prescaler does not affect the external counter mode (it always counts 1:1). Hmmm, what if I set the prescaler to 1:128, then when I put the counter in capture mode, 128 cycles could go by without the counter getting incremented.
So, I tried it...And it works like a charm. I have attached the program I was using to test it out. Simply connect the pulses to the RC.3 pin, and make sure nothing is connected to RC.0 (the capture pin).
Comments welcome.
Bean.
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap used 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
SX-Video Display Modules www.sxvm.com
Don't mistake experience for intelligence. And vis-vera.
Post Edited (Bean (Hitt Consulting)) : 10/12/2006 12:11:06 AM GMT
One little quirk of the SX48 counters is that you cannot read the counter. What the... No, I'm serious. If the counter is used in timer mode, there is little need to read the counter value, it just does something when it reaches R1 or R2. In capture mode, we can read the timer in a round-about way. When you trigger the capture pin, the current count is copied into the capture register (which we CAN read). But the capture pin doesn't do anything in "external counter" mode.
So, being a crafty little programmer I thought. Well I'll just count, then switch to capture mode, toggle the capture pin, and volia I will have the count. And that works with one little problem. The counter is incrementing from the instant you put it into counter mode. So you end up with some extra counts until the code toggles the capture pin. Well, it's better than nothing...
Then I was playing with the external counter mode and discovered something interesting. The prescaler does not affect the external counter mode (it always counts 1:1). Hmmm, what if I set the prescaler to 1:128, then when I put the counter in capture mode, 128 cycles could go by without the counter getting incremented.
So, I tried it...And it works like a charm. I have attached the program I was using to test it out. Simply connect the pulses to the RC.3 pin, and make sure nothing is connected to RC.0 (the capture pin).
Comments welcome.
Bean.
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap used 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
SX-Video Display Modules www.sxvm.com
Don't mistake experience for intelligence. And vis-vera.
Post Edited (Bean (Hitt Consulting)) : 10/12/2006 12:11:06 AM GMT
SXB
![](/plugins/FileUpload/images/file.png)
1K
Comments
It appears that this counter can only be used at an instant in time. Is there a way to make an accumulating counter that can be polled and zeroed? I am not using a motor encoder but would like to keep track of position knowing how many pulses have been sent. I know the accuracy will not be exact but close enough for what I am doing.
I don't know of any way around that. But it should keep counting if you put the timer back into external count mode. If you wait for the input to toggle you should be able to read it without missing any counts (I haven't tried that, but in theory it should work).
Here are some other ideas you might try:
Use the RTCC input (only 8 bits, you can get a 9th bit on the SX48)
Use a port B external interrupt.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.iElectronicDesigns.com
·
This is for a diagnostic instrument for debugging things like SD card drivers and floppy disk controllers, to show whether, and if so, exactly how many pulses have passed some point in a circuit.
I already have a pulse counter with LED display made completely out of hardware, and it works great, except that it only counts up to 999, so I was hoping to use the SX to extend the count length almost arbitrarily, hopefully without losing too much speed response.
It seemed like the timer in counter mode would be ideal for this, because it looked like it could accumulate the first 16 bits completely in hardware, giving a software routine plenty of time to cascade overflows to additional bits, and to show a snapshot of the counts on an LCD. I've been reading the timer documentation over and over, trying to figure out how to get a reading out of the counters, watching all my program efforts fail to work, and wondering why nothing in the documentation described how to do it. I guess I know now - becasue it can't be done, directly.
But if this trick to read the counter may result in losing pulses then I don't think it will work for my project, because the entire purpose of this circuit is to show an accurate count of pulses.
However, the RTCC external input may work almost as well, if it's configured to interrupt on overflow, and the ISR cascades the counts.
What is the ninth RTCC bit in the SX48? I can't find any reference to it in the documentation.
David
· Using RTCC with interrupt on overflow will get you the fasting counting speed. Because the interrupt only happens every 256 counts. And all the interrupt routine has to do in increment a variable.
· The "ninth" bit of RTCC is at T1CNTB register, bit 7. It gets set when RTCC overflows, but does NOT get cleared unless you do it manually. But it provides an easy way for the interrupt to determine that RTCC caused the interrupt. Or if you are NOT using RTCC interrupts, then it is a way to increment the variable in a simple program loop. If the bit is set, then increment the variable and clear the bit.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.iElectronicDesigns.com
·
Use a port B external interrupt."
My max input frequency is 1,000 hz down to 1 Hz.
I am using a 4Mhz osc to get my timer1 output frequency low enough.
Can I still use Serin and Serout in my main code with these 2 interrupt methods?
Thanks, Bean.