|
|
View previous topic :: View next topic |
Author |
Message |
sifirzero1
Joined: 14 Dec 2022 Posts: 42
|
Timer0 and timer1 frequency puls |
Posted: Mon Nov 18, 2024 1:47 am |
|
|
Hello, can you help me get an idea on a small issue and guide me? How can I count the pulses with timer0 and the pulse duration with timer1? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Mon Nov 18, 2024 3:10 am |
|
|
You need to tell us what chip?.
The first, most PIC's can do. However details vary with the chip.
The second may not be possible, and will need to use the CCP together
with the timer. Which timers are supported by the CCP depends on the
PIC. |
|
|
sifirzero1
Joined: 14 Dec 2022 Posts: 42
|
|
Posted: Mon Nov 18, 2024 5:43 am |
|
|
Ttelmah wrote: | You need to tell us what chip?.
The first, most PIC's can do. However details vary with the chip.
The second may not be possible, and will need to use the CCP together
with the timer. Which timers are supported by the CCP depends on the
PIC. |
12f683 ccp kullanmadan. Timer0 ve timer1 kullanilmasi gerekli |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Mon Nov 18, 2024 7:01 am |
|
|
You are making things very hard for yourself. One of the least capable chips
in existence.
Timer 0 can count a pulse arriving on T0CKI. Pin 5. No other pin.
Code: |
setup_timer_0(T0_EXT_L_TO_H | T0_DIV_1);
|
At any time you can read timer0 with get_timer0(), however it is only an 8bit
timer on this chip.
The CCP is on the same pin, so can be used to measure a pulse width, but
only of a relatively low frequency pulse. Since this chip has only one CCP, you
have to program it to record a count on the rising edge of the pulse, and
as soon as this is read, reprogram it to record a count on the falling edge.
This means there must be quite a large number of CPU instruction times
between the edges. So a signal at 100Hz, yes, can be done, but at the
8MHz maximum internal clock rate of this chip (you have not got the pins
to spare to use an external clock), any pulse width below perhaps 50uS
would not be possible.
Now there are threads here on how to use a single CCP to measure a
pulse width. Some of the CCS examples also show how to do this.
Look at:
Code: |
c:\program files\picc\drivers\em4095.c
c:\program files\picc\drivers\adxl210.c
c:\program files\picc\examples\ex_react.c
|
You really would find this much easier using a more powerful chip.
A lot though depends on what frequencies are involved and what you
want to actually do with the values. |
|
|
sifirzero1
Joined: 14 Dec 2022 Posts: 42
|
|
Posted: Mon Nov 18, 2024 2:25 pm |
|
|
Ttelmah wrote: | You are making things very hard for yourself. One of the least capable chips
in existence.
Timer 0 can count a pulse arriving on T0CKI. Pin 5. No other pin.
Code: |
setup_timer_0(T0_EXT_L_TO_H | T0_DIV_1);
|
At any time you can read timer0 with get_timer0(), however it is only an 8bit
timer on this chip.
The CCP is on the same pin, so can be used to measure a pulse width, but
only of a relatively low frequency pulse. Since this chip has only one CCP, you
have to program it to record a count on the rising edge of the pulse, and
as soon as this is read, reprogram it to record a count on the falling edge.
This means there must be quite a large number of CPU instruction times
between the edges. So a signal at 100Hz, yes, can be done, but at the
8MHz maximum internal clock rate of this chip (you have not got the pins
to spare to use an external clock), any pulse width below perhaps 50uS
would not be possible.
Now there are threads here on how to use a single CCP to measure a
pulse width. Some of the CCS examples also show how to do this.
Look at:
Code: |
c:\program files\picc\drivers\em4095.c
c:\program files\picc\drivers\adxl210.c
c:\program files\picc\examples\ex_react.c
|
You really would find this much easier using a more powerful chip.
A lot though depends on what frequencies are involved and what you
want to actually do with the values. |
I was going to have it processed according to frequency. I needed to read a value between 20 and 25khz. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Tue Nov 19, 2024 1:34 am |
|
|
The frequency is no problem, but the pulse width you may not be able to do.
Problem is that even a 50:50 pulse at 20KHz, is only 25uSec long. Because
this chip only has a single CCP, to measure the 'width', means programming
this to record first the rising edge, and then reprogramming to record
the falling edge. This reprogramming takes time. You potentially could
handle this for a single pulse, by polling the CCP rather than using the
interrupt. The reprogramming then could possibly be done in perhaps
10uSec.
However I also have to ask what you intend to do with the result?. You
only have a total of six signal pine. One is needed for this measurement,
leaving five. One of these is input only, so very few pins to actually do much
with.
A chip with multiple CCP's makes this much easier. Choosing a chip with more
pins also opens the door to doing more with the results. Then many of the
newer chips support faster oscillators, increasing the resolution possible,
and giving more operations to actually do things.
It's a bit like trying to move house, using a FIAT500. You may well be
able to do it, but it will be very hard. |
|
|
sifirzero1
Joined: 14 Dec 2022 Posts: 42
|
|
Posted: Tue Nov 19, 2024 1:43 pm |
|
|
Ttelmah wrote: | The frequency is no problem, but the pulse width you may not be able to do.
Problem is that even a 50:50 pulse at 20KHz, is only 25uSec long. Because
this chip only has a single CCP, to measure the 'width', means programming
this to record first the rising edge, and then reprogramming to record
the falling edge. This reprogramming takes time. You potentially could
handle this for a single pulse, by polling the CCP rather than using the
interrupt. The reprogramming then could possibly be done in perhaps
10uSec.
However I also have to ask what you intend to do with the result?. You
only have a total of six signal pine. One is needed for this measurement,
leaving five. One of these is input only, so very few pins to actually do much
with.
A chip with multiple CCP's makes this much easier. Choosing a chip with more
pins also opens the door to doing more with the results. Then many of the
newer chips support faster oscillators, increasing the resolution possible,
and giving more operations to actually do things.
It's a bit like trying to move house, using a FIAT500. You may well be
able to do it, but it will be very hard. |
Thank you for the valuable and nice information. I need to memorize the measured frequency value to be used in the same subject when it is fixed and then compare the changes. How can I do this without eeprom? I have no knowledge of algorithms |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Wed Nov 20, 2024 11:52 am |
|
|
If you need the comparison value to be remembered when you turn the
system off, you need an external battery backed RAM, or an EEPROM.
Goes back to my comment about choosing a better chip. Go for one with
EEPROM and you have somewhere the store the value.
What do you want to 'do' when the values match or differ?. If anything
beyond simply changing an LED, then again you need a chip with more
pins.
You seem to only be talking about testing a frequency. What was the point
of the pulse width measurement?. |
|
|
sifirzero1
Joined: 14 Dec 2022 Posts: 42
|
|
Posted: Wed Nov 20, 2024 3:26 pm |
|
|
Ttelmah wrote: | If you need the comparison value to be remembered when you turn the
system off, you need an external battery backed RAM, or an EEPROM.
Goes back to my comment about choosing a better chip. Go for one with
EEPROM and you have somewhere the store the value.
What do you want to 'do' when the values match or differ?. If anything
beyond simply changing an LED, then again you need a chip with more
pins.
You seem to only be talking about testing a frequency. What was the point
of the pulse width measurement?. |
My goal is to make a metal detector. |
|
|
|
|
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
|