NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Need help with frequency counter
February 27, 2012 by nismotony |
Hi guys, I'm trying to build a frequency counter that will measure 10Hz - 30kHz. I have yet to experiment with the timers and counters so this is going to be a good learning project for me. I have read the tutorial and have seen the app note from Atmel. It seems that I need to be able to use the T0 or T1 pins on the ATMEGA168. This are occupied by the nerdkits LCD though. Any suggestions? This project has really been kicking my butt, I'm an analog design engineer. Any help or code examples will be much appreciated. Tony |
---|---|
February 27, 2012 by 6ofhalfdozen |
Hiya Tony, I made something similar a while back for a readout on a pulse-out flow meter. I don't have my code on this computer, else I would post it for you. I think I started with the real time clock code and removed most of the commands sending data to the LCD. From there, I added pin change interrupt code for PC4 (?, one of the unused pins near the temp sensor). Since my pulses could be less than .1hz, I let the real time clock run for 15 seconds before dumping the variable to the calculations (pulses/timercounts) and LCD output. I needed a little tweeking with my timing and calculation to get it where I needed it, but it was pretty quick and easy. I used a 555 to double check my higher frequency results, and it made life a lot easier than spinning that flow meter all the way up. I don't know if that will work for you, but hopefully points you in a right-ish direction. |
February 27, 2012 by nismotony |
Thank you for the advice. So maybe I should look into the nerdkits lcd.c and see if I can modify it? I'd like to see your code too. Thanks |
February 28, 2012 by nismotony |
Ok, here is where I am now. I've taken code from the nerdkits real time clock and code from the obdII scanner and hacked them together. At this point I'm just trying to have a running real time clock and then an external interrupt that will measure pin change. Maybe with this I can measure the time between cycles of the pin. Problem is my code isn't working, I can't get "count" to increment when I manually short pin PC5 to ground. Here is my code:
|
February 28, 2012 by 6ofhalfdozen |
Tony, I still can't find my code, it might be on my other other computer. sigh. Hopefully I can post it tonight. Anyhow, your cut and paste code looks decent. The only major thing that I see is that you don't setup PC5 as an input in the DDR. I am fairly certain this is why your count is not changing. Also, just toggling to ground might not be enough. I would suggest running a 1k resistor from PC5 to an empty spot on the breadboard and using a jumper wire from that row to toggle back and forth from Vcc and ground which should show up better as a clear toggle to the mcu. |
March 01, 2012 by nismotony |
6, thanks. I'm going to give that a try tonight. |
March 02, 2012 by 6ofhalfdozen |
Tony, I am having a nightmere of a time trying to find where I put that code. I will try to spend a bit more time looking for it, or just recreating it from my notes this weekend. I haven't forgotten, just no luck so far.. All the same, it looks like you are making a decent bit of progress with your project! |
March 05, 2012 by 6ofhalfdozen |
Tony, Finally, I found it!! 3 computers and 6 flash drives later.. anyhow, below is my code for the collective viewing. A couple tidbits in case you decide to try and use it. A. To fit into the enclosure for this, I had to cut back to a 8x2 character LCD. So the LCD lines are very short and only two. If I could have used a 20x4 LCD, I would have added more info, but not enough space in the box. Adjust your code accordingly. B. I hooked this up to a roots flow meter which output 2 pulses per rotation, so in line 116 there is a " / 2" which is used for this specific application and might not be needed in yours. Adjust your code accordingly. C. Because the roots meter can send outputs less than 1 pulse per second, I have the NK counting the number of pulses in 15 seconds. This is the whole point of the if/then loop starting on line 114. This let me measure low flows which the system sometimes sees. Anyhow, this is setup specifically for this device and if you are expecting drastically faster or slower pulse rates you will need to adjust this for your needs or just remove it. D. While it may not work well in the higher 35kHz and up range, from 0.02-5000 pulses per second it performed very well.
|
Please log in to post a reply.
Did you know that you can use printf and scanf functions to talk to your computer from your USB NerdKit? Learn more...
|