Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

A Timed counter is it possible!?

Status
Not open for further replies.

L3wis

New Member
Hey all im still plugging away with my project. Basically i can't see how it's possible to create a timed counter! It's doing my nut trying to work something out. Basically im doing a tachometer, it counts the pulse input from the function generator (only simulation) for 0.5 seconds and then outputs the count of pulses to the output pins as binary. How can i do the 0.5 second count? Atm it seems impossible! Please help me. Oh and im using a PIC 16F628. Any example programs in Assembly would be helpful too!

Thanks in advance!
 
Depending on the frequency of the input pulses you could use the external interupt input to count the pulses. The interrupt routine just increments a counter for each pulse. This will only work for relatively slow pulse rate which but will probably be fine for a tachometer.

I don't know that much about the PIC's timers but some timers you can configure the timer to time the time between pulses to calculate the speed. This is a bit more complicated software-wise and doesn't work well at slow speeds.

The last option is to find a microcontroller with two timers.

Brent
 
thanks for your suggestions m8! The pulse rates in simulation would be:

Maximum = 16 pulses in half a second,

Minimum = 2 pulses in half a second.

The first option sounds good, but i've never used interupts before. I do briefly know how they work though. i.e. They stop the program doing what ever it's doing and process the interupt routeen. But im not to sure how i'd activate the interupt or when to :?: i'll write out step by step what i need the program to do:

1) Activate the half second counter.

2) While the counter hasn't reached the half second.

3) Count the number of pulses on a certain input pin.

4) If the timer has reached the half second stop the counting.

5) Send to the output pins the binary number (4bit)

6) Wait half a second.

7) Restart.

Am i biting off more than i can chew :?: Some more detail about how to implement an interupt would be totally great!

Thanks again in advance!
 
This is a good project to learn a bit about interrupts.

Your description of interrupts is right on. When an interrupt occurs the CPU jumps to a specific spot in program memory and starts executing form there. You should be able to find that memory location in the PIC data sheet and put your interrupt routine there (or a jump to the routine). I'm not an assembly or a PIC expert but thats the basic idea. If you're using C or basic you'll have to look up how the compiler handles interrupts. Other than that you have to set up the interrupt control special function registers to turn interrupts on (the PIC datasheet will tell you how to do this).

Your description of how your program works will work. The pulses arrive slow enough that you probably don't even need interrupts. Interrupts will make programing easier though. You will be able to get rid of step 3 and let the interrupt routine handle it. The best way to find out about interupts is to have your interrupt routine toggle a LED to make sure the interrupt routine is happening. Idealy I'd have the interrupts count the pulses and another interupt when the timer overflows. You just set the initial timer value so the timer will overflow in .5 sec. This lets your program do useful stuff in the mean time and not worry about polling the inputs or the timer.

Learning about interrupts is well worth you time your programs will be much better for it.

Check out Nigel's tutorials I'll bet he has an example for setting up interrupts.

Brent
 
Basically a tachometer is a frequency counter, and there are generally two different ways of designing them.

1) Count the number of pulses in a set time, this works best for higher frequencies, at low frequencies (like your tachometer!) resolution is very poor, and display updating is very slow.

2) Measure the time between pulses, this works best at low frequencies, and the display updates MUCH!! faster as you only have to wait for two pulses. You convert the time to frequency by dividing one bu it (if the time is in seconds), or by dividing 1000 by it, if the time is in milliseconds.
 
God i don't think u guys realise how helpful those 2 answers were! Thanks alot! nigel do you think you could expand on your second answer a little as im still unsure of how to do this in programming language. Or if u could link me to a tutorial that would be super helpful. I'm feeling a little more confident that i might actually be able to do this. What languages can i program a pic 16f628 with? I am planning on using nigels winpicprog.
 
L3wis said:
God i don't think u guys realise how helpful those 2 answers were! Thanks alot! nigel do you think you could expand on your second answer a little as im still unsure of how to do this in programming language. Or if u could link me to a tutorial that would be super helpful. I'm feeling a little more confident that i might actually be able to do this. What languages can i program a pic 16f628 with? I am planning on using nigels winpicprog.

Assembler is fairly easy, and it's completely free!.

If you look on the MicroChip website they have an application note for a frequency counter - this works up to 50MHz, it uses pulse counting for the higher frequencies, and timing measurement for the lower frequencies.
 
:idea:

Hello, if you use timer 1 to generate your timed intereupt, say every 100MS, you can then decrease a reg in the int sub, start at 5, until 0, this will give you your 1/2 sec timer.

Use tmr0 to count the pulses on the pin (tach in) when tmr1 times out (after 500ms) move tmr0 value into "pulse_cnt" reg, re-clear tmr0, reset tmr1 counter (to 5) and your "pulse_cnt" reg will be updated every 500ms automatically.

I did a rev counter many moons ago, I think I timed the period between each pulse, and worked out the rpm from that time period. Just another way to do it, this gives faster results than waiting 1/2 sec for rpm's

You will probably want to add the result you get from "pulse_cnt" to "av_cnt" and divide this by 2, this will average out the pulses counted.

For example if the pulse count is "almost" 3, you will get 2 on 1 count and 3 and another......etc....


Hope this helps!
 
hey, both replies are very helpful thanks! Matt i think im just going to simulate the pulses because i don't have time to work interupts out. Thanks for ur reply though. Basically what my system does is counts the number of times u press one switch, (simulating the pulse counting) and then when u press switch 2 it uses a table to look up the value and sets a binary value across the output pins. Basically everything is there but all the timing and interupts. I intend on learning interupts soon in the future but i don't have time now as the project is in tommorrow! Thanks for all the help!
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top