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 CCS Technical Support

Timer0 and timer1 frequency puls

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



Joined: 14 Dec 2022
Posts: 42

View user's profile Send private message

Timer0 and timer1 frequency puls
PostPosted: Mon Nov 18, 2024 1:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 18, 2024 3:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 18, 2024 5:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 18, 2024 7:01 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 18, 2024 2:25 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 19, 2024 1:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Nov 19, 2024 1:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 20, 2024 11:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 20, 2024 3:26 pm     Reply with quote

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: 9224
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Nov 22, 2024 10:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 22, 2024 11:01 pm     Reply with quote

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.
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