CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Timer1 frequency and pulse count

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
petu65



Joined: 05 Jan 2011
Posts: 8

View user's profile Send private message

Timer1 frequency and pulse count
PostPosted: Thu Oct 08, 2015 2:32 am     Reply with quote

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: 19448

View user's profile Send private message

PostPosted: Thu Oct 08, 2015 3:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 08, 2015 4:05 am     Reply with quote

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: 19448

View user's profile Send private message

PostPosted: Thu Oct 08, 2015 6:58 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group