PDA

View Full Version : Can someone help me get started with a counter



kitcat
- 20th March 2005, 18:08
Hi there.
I am trying to get my head round counters. I think that I can use a counter to count pulses while performing other tasks wihtout losing the pulses soming in.
What I want to do is count how many pulses I get every 852 ms (can I be more acurate than a millisecond?) and then take that figure and do a small amount of maths with a few if statements etc. while the next count is happening. Can someone outline what needs to happen?
I guess I need to reset the counter figure to zero every 852 milliseconds or some such thing but I havent fully understood the procedures

Thanks

Matt

Bruce
- 21st March 2005, 22:25
Depending on the PIC you're using, you probably have several onboard timer/counters.

Here's one example: http://www.picbasic.co.uk/forum/showthread.php?t=1029&highlight=pulse+counter

Enter pluse counter in the forum search box to turn up several more.

srspinho
- 22nd March 2005, 13:51
Hi Matt,

I have been using the method posted by me some time ago (see link below) without problems.

I think you´d need to change the TMR0 value to set it to 852 ms.

I´m using this code to count pulses coming form my car´s wheel and storage the total mileage of a trip and calculate the average speed and the average comsumption of gasoline. Also, I can show the instant speed.

This method is working fine. I did some stress tests under Proteus and it is working very well.

If you have some good advice in order to improve the performance, let us know !

http://www.picbasic.co.uk/forum/showthread.php?t=550&highlight=pulse+counter

Regards.

Sérgio

kitcat
- 23rd March 2005, 07:38
Ha! That is exactly what I am doing.
At the moment I have two pics, one doing the counting and the other doing the working out of distance etc. I have two pics because I don't want to lose incoming pulses while the numbers are being worked on otherwise my distance total will under read. I was told that I should be able to do it with 1 pic using the timer working in the background.
I worry that I might lose pulses anyway when the timer is reset but since at 852 ms 1 pulse = 1 mile an hour then presumably at 20 mhz it shouldn't be a problem. (Hmmmn any comments on that?)

My other problem is that I understand that it takes a small amount of time to write to eeprom. I therefore only write the distance travelled to eeprom when the speed is 0 mph. trouble is I keep swithching off the ignition too quickly (clearly within 852 ms of stopping) and I lose the most recent trip. I need some form of back up power supply that can keep the pic going long enough to wrtie to its memory. (about 5 seconds?) I think in total I am drawing about 300 ma including the backlit LCD display. Any ideas on this?

Thanks.

srspinho
- 23rd March 2005, 12:15
Hi Matt,

well, some time ago, Melaine told me to do the pulse counting using two pics.
One for the counting task and another one to do the "hard" work (Math, LCD output etc)
I tried to figure out how to do that using two Pics but I could no find a good solution. In my tests the system did not work very well.
How are you doing that with two pics ?

Talking about the mileage storage, I´m just writing to the eeprom if the user press the "Finish" button. In other words , my system just writes the data if the user want.
I´m building my odometer system just for fun, it is not a commercial product.

I was reading an article about odometers and they say that some commercial versions just write the values when we have another new 1/10 th of a kilometer or Mile.

If we consider a high speed as 180 Km /h we will have a new 1/10 th of a kilometer (100 meters) on every 2 seconds. And if you storage this new value using this method, I think you will not loose data.

Waht dou you think ?

regards

Sérgio

kitcat
- 24th March 2005, 16:30
Yes. Mine is just for fun too.
I started with a basic stamp and realized I was losing miles because of the time it takes to do the maths.
I decided to use 2 pics to bypass the problem and I am getting on just fine.
Basically I run the counter pic at 20 mhz. At 100 mph I figure that 1 pulse comes in every 8.52 ms. I am not sure how long the Pic takes between finishing the count and starting the new one but basically what I do is this:

Pic 1:
Count for 852 ms (1 pulse = 1 mph at this count time with my tyre size and speed sensor pulses per revolution)
Send the result out to porta (Actually it is a 14 pin pic so I only have 6 bits per port so I have to send the 7th bit to portc.0...i guess I am limited to 126 mph!)
toggle portc.1 to show that a new count has taken place.

Pic 2:
Running at 8 mhz I poll the input port (again 14 pin device so I have to split the byte between two ports). When the toggle pin has changed I simply read the byte into my mph variable and then do the maths.

The way I figured it is this:
There is a new count every 852 ms
The second pic is running at 8 mhz and so 852 ms must be more than enough for it to do everything (just a guess..but surely!).
So all the first pic has to do is latch the result on it's output port and leave it there for the second pic to pick up when it is ready.
The second pic finishes doing its thing way before the new count so it is always ready to pick up the new byte. That way neither pic has to care what the other one is up to.

I am hoping that the first pic can latch it's output, toggle the status change pin and then get back to counting before a new pulse has come in.

When the speed is 0 mph I write the distance to eeprom. I keep forgetting to wait till the speed is zero before writing and so I have lost a few KM. If I could just keep the pic running for a few seconds after the ignition is off then that would do it. Perhaps I need a really big capacitor in there but this does seem a little bit mad. I quite like the idea of a write button...but then I would have to remember to do that wouldn't I! I guess I shouldn't live life in such a hurry.
I'll post my code if you want.

Matt