NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Sensors, Actuators, and Robotics » Codewheel and RPM
June 02, 2012 by DaveGriff |
First of all this is my first ever post so you'll have to bear with me :) I have recently bought a codewheel that has 500 CPR (Counts Per Revolution) Here's a link to the Data Sheet: http://www.farnell.com/datasheets/20523.pdf At the moment I've set it up attached to a motor, It has 2 channels; one is the indexing channel that pulses once per revolution and the other pulses 500 times per revolution. Here is the current set-up: I have set-up a pin change interrupt on PC5 that increments a counter by 1 every time it fires, then I look at how many counter ticks have occurred between the pulses and from that I can calculate the RPM: The problem I'm having is that I can only use the indexing channel as the other channel pulses too fast at high RPM and the microprocessor cannot count fast enough (3000 RPM * 500 CPM = 1500000 CPM) or 25000 counts every second! I think the best war to solve this problem is to use the 16-bit counter and set it up so that it fires an interrupt when the count reaches 100, that way I might have a chance of counting RPM when it is high, but I don't know how to set up the counter. If anyone could point me in the right direction I would appreciate it. |
---|---|
June 02, 2012 by pcbolt |
Hi Dave - Cool gizmo and a nice set-up you have there. The 16-bit timer works the same as the 8-bit, except it can top out at 65,536 counts and you can set a compare value at any range in between. However, it doesn't count any faster than the 8-bit timer. The resolution of 25000 counts per second can easily be clocked by the MCU, in fact it is sittting around waiting >500 CPU cycles before the next input comes in. What I would do is set the 8-bit timer up in "normal" mode which simply counts from 0 to 255 then "overflows" and starts counting again from 0. The good part though, is when it overflows it can trigger an interrupt where you can just increment a count variable to track the number of overflows. To get the number of CPU "ticks" you just get the current timer count (from TCTN0) and add the overflow count times 256
Of course now you need to know how long a "tick" is in seconds. If you set the timer "prescaler" to 1, the time per tick is 1/14,745,600 seconds or 67.817 nanoseconds (assuming you are using the oscillator from the Nerdkit). Using the timer in "normal" mode, most of the settings you need are already set by default. You only need to set the "prescaler" value and enable the "overflow" interrupt like this:
All you need is an interrupt service routine like this one:
Couple of things to keep in mind. Since the counter is incrementing so fast, you will be dealing with large numbers. If you use floating point division, the MCU can't deal with more than about 7 significant digits so you will have to code accordingly. Also, I would reset the timer variables to 0 at the start of each timing sequence. |
June 03, 2012 by DaveGriff |
hi, Thanks for the reply, I just started to code this when I came across a problem, are all the pins for counter 0 being used by the LCD? I don't know if I'm being silly here but how can I get around this? (Very nice solution BTW) |
June 03, 2012 by pcbolt |
Dave - The pins for timer/counter 0 (OC0A/OC0B => PD5/PD6) are used for the LCD. They are enabled when you set them as output (i.e. DDRD |= (1<<PD5)) and also when you set up the timer/counter 0 as anything BUT "normal" mode. So in "normal" mode you will not have to worry. The timer itself operates internally and only outputs when needed. It is an interesting question because when the LCD is initialized, both PD5 and PD6 are set as output. Using timer/counter 0 in "clear on compare match" mode (like in the "realtimeclock" program) PD5 is being set or cleared every 100th of a second. PD5 turns out to be an LCD data pin but it won't be transferred to the LCD memory until the PD6 pin (enable) goes high. It's possible, but not likely, you could write data to PD5, the timer changes it, then PD6 gets enabled, and your data would be corrupted. The situation would be far worse if you enabled OC0B as a timer/counter output, then data would be sent on every compare match. If you want the output mode, you could switch to timer/counter 2 which is also 8-bit and has an output on PB3 (OC2A). Far from a silly question. |
June 03, 2012 by esoderberg |
Dave, PCBolt almost surely has the best advice, to use timer/counter 2 and leave the LCD running off PORTD. However, it is not too hard to change the lcd pins. Below is code I used to change the output pins for the LCD from the Nerdkit standard PD2-7 to PD0-4, PB5, and PD7 (PD5,6 on the pololu controller I was working with used those pins to control a built in H-bridge so they weren't available for the LCD). If you follow the format used to make those changes you can see that pretty much any mix of I/O pins can be used. The updated LCD.C file below would go into the libnerdkit file in place of the original version if you wanted to change your LCD I/O pins.
|
June 05, 2012 by Ralphxyz |
So DaveGriff, where did you get the codewheel from? Ralph |
June 06, 2012 by DaveGriff |
I got it from Farnell there are a few different types on there Dave |
June 06, 2012 by Ralphxyz |
What a strange site Farnell: They do not appear to have a USA distributor. I see a Great Britain source for 18 euros but I do not know what the shipping might be! Ralph |
June 06, 2012 by Rick_S |
I believe Newark is the US Farnell. |
June 06, 2012 by Ralphxyz |
Well if so I cannot find a listing for any codewheels, guess I'll just do a google search to see what shows up. Ralph |
June 06, 2012 by Rick_S |
Encoder sold separately. Here I think |
June 06, 2012 by Ralphxyz |
Darn those prices are a bit steep. I think I have a codewheel drawing that I could print on a transparency and paste on a clear vinyl disk. I don't know what the resolution would be and I don't think I could determine rotation direction but I could possible get rpm. Ralph |
June 07, 2012 by Rick_S |
Tear apart an old ball mouse, or get an old arcade trackball or atari tempest controller. They all have code wheels and pickups. It's a much cheaper alternative. |
July 06, 2012 by bluestang |
I think You will also find them in printers, so pick one up alongside the road tear it apart and you will find a code wheel and chipset, I like Hp they usually have 2 of them in their with a motor as well. |
July 07, 2012 by Ralphxyz |
I took apart a ball mouse, there is a code wheel but I would be really pressed to figure out a way to use it. Ralph |
July 11, 2012 by pcbolt |
Ralph - Break out the old O-scope. Or, if you haven't torn the mouse completely apart, check out the PS2 interfacing. I wrote up a library article on it....at your request mind you :-) |
July 11, 2012 by Ralphxyz |
:-) Thanks pcbolt. That is a great PS2 interfacing article. The problem I have with using a ball mouse code wheel is with how to physically connect one to say a motor shaft. The code wheel is about 3/8" with a little -1/8" stub for mounting to a shaft and then aligning the led would be a challenge. Does any one have any pictures of actually doing this? Ralph |
Please log in to post a reply.
Did you know that using an alternating excitation voltage can reduce the noise in many sensor measurements? Learn more...
|