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.

Counting number of pulses in a time period.

Status
Not open for further replies.

phaedrus

Member
Hello ,

I have an application where I have to count the number of pulses on a pun in a time period ( 100mS). The pulses would have a maximum frequency of 10kHz.
A simple way would be to :
a)Attach the pulses to an rising edge interrupt pin on the micro.Increment the number of pulses in the ISR and store in variable.
b)Setup a timer for 100mS.Fire an interrupt every 100mS.Check the counter variable.

Is there any better way to do this with lesser software overhead ?Maybe the CCP module offers a better solution ?
TIA.
 
Ermm...
Did not quite get that.The input pulses would "not be" periodic.
From what I understand :
1) Setup tmr0 as 100ms timer.
2) Setup tmr1 as timer with external clock input.
So after 100ms the value in tmr1 would be the number of pulses ?
Easiest way is to set up your 100ms delay... Set up tmr0 or tmr1 for an external clock..... Clear timer....Wait time....Read timer... Job done
 
It would have to be triggered when on the rising edge of the input pulse.It is for counting the pulses from a GM tube.The number of pulses within a period (100ms) is used to calculate the radiation.
Would a CCP module be more "elegant" than using a timer and counter as mentioned in first post ?
If your pulses are not periodic then when would need to trigger the event OR as you say, use the CCP module..

What is the application?
 
1) Setup tmr0 as 100ms timer.
2) Setup tmr1 as timer with external clock input.
So after 100ms the value in tmr1 would be the number of pulses ?
You have the idea correct: this is an implementation of what Ian suggested. Whether the pulses are periodic or not does not matter. In this implementation TMR1 only counts on a rising or falling edge (depends on how you configure it) of the external input (in this case the pulse you're monitoring). You are using TMR0 with the internal (or PIC) clock to gate your measurement. When TMR0 expires, you check TMR1 and you have how many pulses were received in your specified time. You need to set the TMR1 prescaler so as to not overflow it - I doubt you would even with a 1:1 prescaler ratio though (if the closest the pulses can be is 10kHz, and you have a 100ms gate time, the most pulses you could have are 1000 - well within the 16-bit resolution of the TMR1).
 
Thanks for the reply. I will try this out !!

You have the idea correct: this is an implementation of what Ian suggested. Whether the pulses are periodic or not does not matter. In this implementation TMR1 only counts on a rising or falling edge (depends on how you configure it) of the external input (in this case the pulse you're monitoring). You are using TMR0 with the internal (or PIC) clock to gate your measurement. When TMR0 expires, you check TMR1 and you have how many pulses were received in your specified time. You need to set the TMR1 prescaler so as to not overflow it - I doubt you would even with a 1:1 prescaler ratio though (if the closest the pulses can be is 10kHz, and you have a 100ms gate time, the most pulses you could have are 1000 - well within the 16-bit resolution of the TMR1).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top