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

Pulse Counting
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

Pulse Counting
PostPosted: Tue Sep 12, 2023 1:18 am     Reply with quote

Hello everyone,

I want to capture and count PWM signals that have a duration of 150 nanoseconds and process them. What approach should I follow in this regard?

I am aware that the choice of method for pulse counting can vary depending on your application requirements and design goals. Both interrupt-based and timer-based pulse counting approaches have different advantages and disadvantages.

**1. Pulse Counting Using Interrupts:**

Counting pulses using interrupts requires the microcontroller to enter an interrupt routine for each incoming pulse. This can potentially halt the normal operation of the processor depending on the interrupt process, making it challenging to count high-speed pulses accurately. However, this method allows you to count pulses very precisely and obtain the pulse count instantly.

**2. Pulse Counting Using Timers:**

Using a timer for pulse counting allows you to count a specific number of pulses within a time interval, rather than waiting for the arrival of each pulse. This can be more efficient when faster pulse counting is required because it avoids delays associated with interrupt processing. However, this method may result in a loss of accuracy at lower pulse frequencies.

The choice of method depends on your application requirements and the pulse rate. If you need to count and process high-speed pulses accurately, an interrupt-based approach might be preferred. For lower speeds or less demanding applications, a timer-based approach could be more suitable.

Additionally, I understand that factors such as hardware resources and connections need to be considered. The available hardware resources and connections on the microcontroller can also impact your design.

However, it's important to note that the period of the incoming pulse is 150 nanoseconds, which is a very high frequency, and you are receiving many pulses in a very short time. Counting and processing such high-speed pulses accurately may be challenging. Therefore, I believe that an interrupt-based approach may be necessary for counting such high-speed pulses. What are your thoughts on this?

Choosing the right processor for this task depends on various factors. I did not write down the processor model I will use. Because I don't know either. Which one would be more suitable for this job? I would be happy if you help. I am using CCS C version 5.115.
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Sep 12, 2023 2:47 am     Reply with quote

Approach 1, wouldn't be achievable, even by processors running much faster
than the fastest PIC's. Forget it.

You miss one critical bit of data for approach 2. The minimum pulse width?
Problem is that timers all have minimum times for their input signals.
On (for example) a typical PIC33, the minimum high time for a pulse
to be recorded by a timer is Tcy+20nSec. So assuming a chip running at
140MHz, and this is the master oscillator for the timer, just over 27nSec.
Potential problems....

Do you really need to count every pulse?. If not look at an external divider.

There are external chips that give SPI interfaces supporting pulse counting
at frequencies well above this (GHz). Consider one of these.
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Tue Sep 12, 2023 3:17 am     Reply with quote

Will this work when I set the PWM signal to 1us? With 50% duty cycle. 500ns on, 500ns off.Yes, I need to count every pulse.
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Tue Sep 12, 2023 3:46 am     Reply with quote

Using timer1 with a PIC18 series processor, I capture the incoming pulse at 150ns without missing it. But I can't say it works 100% correctly. But it works pretty well.
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Sep 12, 2023 7:08 am     Reply with quote

Yes, that will work fine. The point is that the minimum pulse width and
period depend on which PIC you are using and what clock rate it is
running, so if the period was set down to only a few nSec pulses would
be missed. Most PIC's will count pulses up to perhaps 25MHz fine, and
quite a few will go to about 40MHz.
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Tue Sep 12, 2023 7:44 am     Reply with quote

Now I noticed the SMT (Signal Measurement Timer) feature. It has PIC18F57Q43 processor. I want to try this. But I'm looking at how to use it in CCS C. Do you have any information about this?
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Sep 12, 2023 9:18 am     Reply with quote

Try here:
https://www.ccsinfo.com/newsdesk_info.php?newsPath=ALL&newsdesk_id=195

and this:
http://ww1.microchip.com/downloads/en/appnotes/90003129a.pdf
_________________
Google and Forum Search are some of your best tools!!!!
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Tue Sep 12, 2023 1:44 pm     Reply with quote

Yes, I made a simple application for testing. But I'm thinking out loud now. Is it more logical to use SMT or timer? Which one is more sensitive and better? Another thing I'm worried about is that I will process it according to pulse numbers. For example, it detects the incoming signal in 500ns and I will activate an output. There are such processes. However, when I examine the command set, some commands consist of states such as 1,2,3,4 and 6. For this reason, would it be better to use a 16bit or 32bit processor? PIC18 tcy 62.5ns. On dsPIC33EP series processor it is 14.28ns at 140MHz.
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Sep 13, 2023 8:30 am     Reply with quote

The SMT is very nice, since it has 24bit counters. It can be clocked directly
off Fosc, so gives a resolution of 15.625nSec, It can be programmed to
record both the high time and period.
The data sheet carefully does not specify the clock high and low times
for this but says the timer is similar to the normal timers, which have
a high time requirement of 0.5Tcy + 20, so 51.25nSec at this clock.
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Thu Sep 14, 2023 7:40 am     Reply with quote

Well, it will constantly capture and count the position where the PWM signal is off, for example, its period is 1us and 500ns off, 500ns on. It will then perform operations on certain numbers. Can he do this? Or should 16 bit processors be used for such tasks?
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Sat Sep 16, 2023 12:49 am     Reply with quote

This code getenv("INSTRUCTION_CLOCK"); Returns the MPU instruction clock. Is it true? I want to right click on the compiler and go to where it is defined. But he can't. Where is this command defined? Is it inside the IDE? and what is it worth? Otherwise, I'm talking about dsPIC33CH128MP506 for example. It is calculated from the formula Fcyc = Fosc/2. Is it true?
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sat Sep 16, 2023 4:44 am     Reply with quote

It is not an instruction.

It is a preprocessor directive. Just like when you use a #defined value.
The value for it is generated for you by the compiler (not the IDE).
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Sat Sep 16, 2023 7:11 am     Reply with quote

What is the INSTRUCTION_CLOCK value produced by the compiler? How can I see this? Actually, wouldn't it be great if the IDE printed this value as info in the output section? The processor I use is dsPIC33CH1328MP506 and 180MHZ. Then the INSTRUCTION_CLOCK value is 180 million? Am I thinking correctly?
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Sep 16, 2023 7:47 am     Reply with quote

Instruction clock is usually 'clock' /2 or /4. depends upon which PIC.
This is covered in the datasheet of the PIC.

In your case it'll be 180/2 or 180/4.....
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sat Sep 16, 2023 8:13 am     Reply with quote

and if you want to see what it is, then just ask your program to display
it as a message; So:
Code:

#WARNING/information IPS is getenv("INSTRUCTION_CLOCK")
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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