NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Pulse width measurement(Please Help!)
May 12, 2012 by reddog30jen |
I have been trying to write what I would think to be a fairly simple prog for Wind speed sensor. http://www.inspeed.com/anemometers/Vortex_Wind_Sensor.asp I have scoured nerdkit forums and the internet for hours and hours and can't find anything very helpful. This is the final part to my Weather Station project. I made separate prog from main to simplify for now. I know this code is prob not close to what I really need because I may need to average over several samples and maybe debounce, but I can't even get it to compile so I can't get anywhere. Please Help! Errors are realtimeclock1.c:77: warning: 'main' is normally a non-static function realtimeclock1.c:102: error: expected declaration or statement at end of input I have never got errors like this before and don't know how to fix. Think it has to do with time variable but no clue how to fix. Here's the code
Also, Am I going in the right direction here or should I read timer value and reset every time pulses come or something else? Thanks |
---|---|
May 12, 2012 by esoderberg |
Reddog, You are missing a closing bracket on line 72 also Try "ISR(TIMER0_COMPA_vect)" in place of "SIGNAL(SIG_OUTPUT_COMPARE0A)" It should compile with those changes; however, there's a problem with you pulse width calculation "pulse_width = (t2-t1);" It is only valid after t2 is found and prior to the next t1 being marked. If you make this calculation just after t1 is found you will be calculating the difference between the new t1 and the old t2(a negative number), which is probably not what you want. A simple solution would be to make pulse_width volatile and put that line of code inside the interrupt vector after your "else" statement. This way pulse_width will only be calculated when it has the correct values to do so. |
May 12, 2012 by pcbolt |
Reddog - Two things - if you take esoderberg's sound suggestion and make "pulse_width" volatile you may need to declare it up higher in the code where the other volatile variables are (I'm not sure the compiler will like it where it is now). Second, if your pulse widths are expected to be less than 10 ms (1/10 of a second), you'll just get 0 for the answer. So your code may be working correctly and it'll seem like it isn't. You may need to change your timer settings for better resolution. |
May 13, 2012 by pcbolt |
Correction to last post --> that should read 1/100 of a second. Ooops. |
May 13, 2012 by reddog30jen |
Yes that sure was the problem, a silly bracket. Always is, lol. It actually makes sense to me now too because compiler thought my static main was part of my ISR. I had been staring at my code for so long that I was blinded to look for such a simple mistake. Thank You so much esoderberg and pcbolt for your responses. They were extremely helpful. I believe this timer has accurate enough resolution for this sensor because the smallest pulse width I am getting is about .08 sec and that is with it really humming. I tried averaging over several samples but it didn't seem to make much of a difference. Since I didn't find anything else like this in the forums here's the fixed parts of code. Program works good to measure mph from 3-Cup wind sensor with one magnet.
Let me know if this code doesn't look right. Just one quick question if someone doesn't mind answering. Anyone know why when I stream variable like this one to the lcd and the size or how many significant digits changes the lcd gives character lead ons or copies such as mphh or for pressure kpaa. It seems to happen randomly. Is there a fix?? |
May 14, 2012 by Rick_S |
Print a blank space or two after the "mph" or "kpa" when you drop from double digit speeds to single, the string shifts to the left one position leaving the last character from the previous on the display. Printing the blank space with your string will "erase" a trailing leftover. Rick |
May 14, 2012 by reddog30jen |
One thing to add to thread. There was a small bug in the program where after anemometer was not spinning for a bit and then started spinning mph would report "inf" for a second before operating as normal. I looked it up to be an infinite error, overflow or "bad math" lol. Maybe cause of big pulse number?? Not sure exactly why but taking mph formula out of ISR and moving into while(1) section fixed bug. Changed variable to regular double too. Hey Rick S.,Thanks, your trick worked for the lcd. Can you or anyone else explain why that happens. Is it a bug in the lcd file or something you need to work out with individual progs. Thanks again everyone |
May 14, 2012 by pcbolt |
Reddog - It looks like you may be dividing by zero if the variable "new_pulse_width" becomes 0. This could happen if the interrupt triggers twice within the 1/100th of a second clock "tick", or at the beginning of the program when the variable is first used. You could just add a simple "if" clause to test for zero before dividing to get mph. As for the LCD, if you write "1234.56" at the first line and first position, then write "123.45" in the same spot, the "6" from the first write will never get "written over" and will persist until it is either written over or cleared. You could clear the display before writting to it each time but this sometimes produces a flicker. Rick's idea works because it overwrites the previous data with blank spaces. It's not really a bug, the LCD is just doing what its told. |
Please log in to post a reply.
Did you know that many systems in nature can be described by a first order response? Learn more...
|