|
|
View previous topic :: View next topic |
Author |
Message |
sifirzero1
Joined: 14 Dec 2022 Posts: 43
|
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: 19529
|
|
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: 43
|
|
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: 19529
|
|
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: 43
|
|
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: 19529
|
|
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: 43
|
|
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: 19529
|
|
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: 43
|
|
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. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9232 Location: Greensville,Ontario
|
|
Posted: Fri Nov 22, 2024 10:35 am |
|
|
You should 'google' ... PIC based metal detector.
There are several pages of them,though most use a more powerful PIC , at least you'll see what others have done, and why. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Fri Nov 22, 2024 11:01 pm |
|
|
As a comment, you don't need an EEPROM. The frequency will always shift
a little just from changes in the weather, so you will just wake the unit, lift
the coil into free air, push a button to say 'this is the base', and
then compare to this reading.
A very basic unit is doable on your PIC, but you can have a lot more
features with a better chip. Also a lot will depend on the oscillator and
coil design you use. These are what determines how well the unit can
really work.
What is puzzling is why you were talking about pulse width?. The
presence of metal modulates the frequency not the pulse width, except
in much more sophisticated systems with double coils, where you change
the coupling.
You are going to be tight on pins. You need some form of output, a pin
for the signal, but also a button to set the zero, and adjust the
sensitivity. Perhaps use a voltage input for this. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9232 Location: Greensville,Ontario
|
|
Posted: Sat Nov 23, 2024 7:21 am |
|
|
Hmm, could eliminate the 'calibrate' pin/button. Instead when unit is turned on , it 'auto calibrates'(saves the base information) ,say for 4 seconds, then flashes LED to say 'calibration complete.
To get more input from a single pin, 'tip&tricks' book from Microchip show how to read THREE ( 1, 0, hi-z ). Used it a few times when I'd run out of pins......
if you have small PICs ,you HAVE to get creative and think 'outside the box'. |
|
|
sifirzero1
Joined: 14 Dec 2022 Posts: 43
|
|
Posted: Sat Nov 23, 2024 8:59 am |
|
|
temtronic wrote: | Hmm, could eliminate the 'calibrate' pin/button. Instead when unit is turned on , it 'auto calibrates'(saves the base information) ,say for 4 seconds, then flashes LED to say 'calibration complete.
To get more input from a single pin, 'tip&tricks' book from Microchip show how to read THREE ( 1, 0, hi-z ). Used it a few times when I'd run out of pins......
if you have small PICs ,you HAVE to get creative and think 'outside the box'. |
thank you. Can you follow my social media account?
https://www.instagram.com/sifirrzero/ |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Sat Nov 23, 2024 2:11 pm |
|
|
For example, 18f46k22 costs less than 5€. If this is a one-off thing, it will be way, way cheaper to invest those 5€ in it and not into 200 hours of development time just to go around the limitations of your PIC. 'tip&tricks' is a phenomenal collection of code and techniques. One learns a lot from it and if that is the goal of this project, cool. But why go there for 2€ difference in price between your chip and something really capable? Think about some future expansion, too. |
|
|
|
|
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
|