View previous topic :: View next topic |
Author |
Message |
petu65
Joined: 05 Jan 2011 Posts: 8
|
Timer1 frequency and pulse count |
Posted: Thu Oct 08, 2015 2:32 am |
|
|
PCWH4.112
Pic18F4523
Hi,
I am using Timer1 as counting external pulses.
Code: | setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); |
Can I somehow measure frequency at same time in timer1 input using interrupt?
Pulse frequency is 0-100Hz. 1Hz resolution is enough.
CCP1 and CCP2 pins are free. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Thu Oct 08, 2015 3:13 am |
|
|
You don't need an interrupt....
There are two basically different ways of measuring frequency. The first is to simply count pulses/time, the second is to measure the 'period' between pulses to give the 'interval', and then reciprocate this. The advantage of the latter, is that you only need to measure the time between two edges to have an immediate measure of frequency, but it involves extra maths, and has trouble coping with signals that are not stable.
The former, is down to deciding 'how long' you want to measure the pulses 'for', and just having a clock locally to give you 'time'.
Simplest way is just:
Code: |
int16 count1, count2, frequency;
count1=get_timer1();
delay_ms(1000); //count for one second - this is the 'clock'
count2=get_timer1();
frequency=count2-count1;
|
Since you wait for one second between readings, this is the frequency. If the timer has overflowed between the readings, the maths will wrap, and still give the correct answer. Ideally, if you have a system clock 'tick' the counter values could be read using this, rather than the delay (allowing the code to be doing other things...). |
|
|
petu65
Joined: 05 Jan 2011 Posts: 8
|
|
Posted: Thu Oct 08, 2015 4:05 am |
|
|
My code is reading multiple adc ports and counter inputs, averaging adc readings and sending them to serial port at interval 500mS. Actually I need to know how many pulses is coming per minute to timer1 port. That is why I thought only way is use interrupt. I am using serial interrupt for buffering serial communication.
How do I define interrupt for timer1 at rising edge?
Is this correct:
Code: | #define INT_EXT1_L2H 0x5001F008 |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Thu Oct 08, 2015 6:58 am |
|
|
Problem is the interrupt won't help you.
If you already have a tick at 500mSec, just take the readings in this.
You can do the maths outside, but just record the timer counts, and work from these.
The timer will not interrupt on an edge. It interrupts when the timer _wraps_ (overflows) only.
The define is for the EXT INT pin, not for the timer. |
|
|
|