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
Timer1 and capture function
Moderator: Benj
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Timer1 and capture function
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Timer1 and capture function
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
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
Martin
-
- Flowcode v5 User
- Posts: 12
- Joined: Wed Nov 17, 2010 8:04 am
- Has thanked: 1 time
- Been thanked: 2 times
- Contact:
Re: Timer1 and capture function
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
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