Page 1 of 1

Timer1 and capture function

Posted: Thu May 24, 2012 4:03 pm
by stephan
Hello,

I have some problems to use capture function with timer1. Iwork with matix development board EB006-00-7 and pic 16F877A.
My goal is to mesure square signal of 10Hz ,given by a signal generator on every rising edge.
For this, I chose to use capture fonction to read timer1 values ,I mean ccp1l and ccp1h (2 bytes)
Before to read these two values Istop timer1 and put it on after the reading as it is said in the datasheet, and I reset timer1 for another count.
I use an user interrpt to configure registers.
I configure (internal clock/4) and prescaler=8, so clock count for timer1 =614400Hz or T=1.62µs

frequency=10 Hz -> T=100ms timer1 count=(100ms/1.62µs)=61728
I wait for this result: ccp1h=241 and ccp1l=16 ,but the result is very unstable on my LCD display.

I give you my simple code .

Would you help me to understand where I made an error.

Thank you very much

Stéphan
frequency_counter_10Hz.fcf
(11 KiB) Downloaded 336 times

Re: Timer1 and capture function

Posted: Thu May 24, 2012 4:34 pm
by Benj
Hello Stephan,

If you download the main Flowcode examples pack from here:

http://www.matrixmultimedia.com/lc_index.php?p=18

Then browse to the following folder.

Examples\Interrupts

Then there is a reliable example called Digital Frequency Counter which works very well using a INT pin.

Had a quick look at your program and not sure why you are getting unstable results.

Re: Timer1 and capture function

Posted: Thu May 24, 2012 5:32 pm
by medelec35
I can see a least two issues with your program.

1) Stack exceeds the standard 8, probably because your calling a macro which contain LCD writing routines from within an interrupt! Both issues are not a good idea.

Also you need to make sure there is no timing of pulse going on before writing variable containing value of frequency to LCD.
If not the wrong value could be wrote..

I would have a update LCD flag.
Then
A) Do the capturing and read frequency

B) Stop the capturing, update flag set to 1, write to LCD. set update flag to 0. Go to A

Hope this helps.

Martin

Re: Timer1 and capture function

Posted: Tue Jun 12, 2012 3:36 pm
by stephan
Hello Benj , Medelec35

Thanks for your answer, sorry because mine is late.

First I test "Digital Frequency Counter" ,so it works well .I analyse how this counter was bulid .So I saw that It counts every Hertz .
When you use it alone it' OK, but when you install this code inside another it slows all the program.

So I can't use this for my application.

That why first I choose to use Capture mode with a little calculation to determine Frequency, i think this will be quick and more usuable.

I try to use advices from Medelec35 but I think there is something wrong in my way of using the "user interrupt" .

So I try to solve it and give you the solution ....I hope.

Thanks for all

Stephan