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

Pulses Counter...

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



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

Pulses Counter...
PostPosted: Mon Mar 09, 2015 2:26 pm     Reply with quote

Hi,

I need to count pulses (total pulses, not referenced with time) so in my mind I had the INT_EXT/ INT_IC interrupt would fit perfectly...

However, I've already have some others interrupts running (RDA and TIMER2 at low freq)

How can I calculate when the INT_EXT/ INT_IC will take too much overhead to read the pulse?

How I can verify it will not "steal" all the MCU clocks for itself, making the other interrupt problematic?



Code:

int32 pulses;

#int_IC1 //Input Capture Interrupt
void IC1_Interrupt(void)
{
pulses++;
}




The frequency of pulses might reach a maximum of 250 Hz...

Do HIGH_INTS=TRUE will help to not miss any pulses?

Device is PIC24FJ128GA010 @ 32mHz

Any thoughts?

Thank you guys! Very Happy
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Mon Mar 09, 2015 2:51 pm     Reply with quote

Obvious first comment. Why not let a CCP count the pulses?.
This is one of the jobs they are for.
It'll count the pulses with no overhead from the processor, all automatically for you.

Then, all depends on how fast your CPU is clocking?. However 250Hz, is easy with a couple of other interrupts running, even at a slow CPU clock like 4MHz. It does after all represent 4000 instruction times even at this rate. Provided your other interrupts follow the correct guidelines (do what is needed, and exit quickly), each should use less than 100 instructions.

On your chip, at 32MHz, 250Hz, is 64000 instructions. You could handle a dozen interrupts if they are written properly. HIGH_INTS is for a PIC18. On the PIC24, there are separate priorities, however 'not needed'.


Last edited by Ttelmah on Mon Mar 09, 2015 2:59 pm; edited 1 time in total
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Mar 09, 2015 2:57 pm     Reply with quote

Ttelmah wrote:
Obvious first comment. Why not let a CCP count the pulses?.
This is one of the jobs they are for.
It'll count the pulses with no overhead from the processor, all automatically for you.

Then, all depends on how fast your CPU is clocking?. However 250Hz, is easy with a couple of other interrupts running, even at a slow CPU clock like 4MHz. It does after all represent 4000 instruction times even at this rate. Provided your other interrupts follow the correct guidelines (do what is needed, and exit quickly), each should use less than 100 instructions.


From what I've read is the CCP can only tell you how many pulses have occurred during X time (it's using a timer also) and can only store a 16bit value...

I really don't need to know the frequency of the pulses ... just how many have being detected (without missing one) since powerup (pulses value would be read and stored on an FRAM)

My RDA is only the serial buffer from CCS example and the timer increase some values ... no delays or long procedure in them! (KISS)
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Mon Mar 09, 2015 3:00 pm     Reply with quote

Your counter, can only count pulses. Exactly what the CCP does.

If you want 32bit, then you can interrupt on the overflow and increment a larger counter.
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Mar 09, 2015 3:09 pm     Reply with quote

Ttelmah wrote:

It'll count the pulses with no overhead from the processor, all automatically for you.


Thanks Ttelmah for your ideas Smile

Do you have an example how to use no overhead with CCP.

From what I've understood, you setup CCP to trigger on rising edge and then in the CCP ISR you read the value and if the value is close to overflow you transfer it to a int32 but this idea will use the CCP ISR and introduce overhead each time the ISR is called...

Also ain't the CCP ISR called at each pulse too?

Just wondering!
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 09, 2015 3:48 pm     Reply with quote

What about using Timer1 configured for external clock ?
The PIC24FJ128GA010 data sheet says:
Quote:

11.0 TIMER1

The Timer1 module is a 16-bit timer which can serve as
the time counter for the Real-Time Clock (RTC) or
operate as a free-running, interval timer/counter.

Timer1 can operate in three modes:
• 16-Bit Timer
• 16-Bit Synchronous Counter
• 16-Bit Asynchronous Counter
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Mon Mar 09, 2015 4:03 pm     Reply with quote

PCM programmer wrote:
What about using Timer1 configured for external clock ?
The PIC24FJ128GA010 data sheet says:
Quote:

11.0 TIMER1

The Timer1 module is a 16-bit timer which can serve as
the time counter for the Real-Time Clock (RTC) or
operate as a free-running, interval timer/counter.

Timer1 can operate in three modes:
• 16-Bit Timer
• 16-Bit Synchronous Counter
• 16-Bit Asynchronous Counter


Woaa! Thanks for the clue PCM Programmer, I'm new in the PIC24 world and I did miss that in the datasheet!

All the options named are good Smile

They all work, I just have to see which is the more efficient !

EDIT:

Quote:
On your chip, at 32MHz, 250Hz, is 64000 instructions. You could handle a dozen interrupts if they are written properly. HIGH_INTS is for a PIC18. On the PIC24, there are separate priorities, however 'not needed'.


I've seen your edit now, makes sense 250 hz is nothing but I was more afraid to interrupt an interrupt with those unpredictable pulses interrupt... since the PIC can only process one interrupt at time! (whoosh, many interrupt word Razz)

Counting pulses accurately is critical...

Big thank you guys!


EDIT2:

After digging through some posts both CCS and MC forums, some says it's better to poll the interrupt flag if possible and use that information to increment the pulses...
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Tue Mar 10, 2015 1:47 am     Reply with quote

It takes less instructions to poll a flag, but it does mean your code has to be sitting in the 'poll loop'. Only needed if you really do require really high speed.

Thing to understand about interrupts, is that they set the interrupt flag, and this _remains set till the interrupt is serviced_. So if (for instance) an event triggers while the code is inside another interrupt, the flag is still set, and the event will be handled as soon as the first interrupt handler exits. This is why the golden rule is to get out of handlers quickly. A serial interrupt handler, that just reads the character, stores it into a buffer, and exits, takes perhaps 100 instructions in total (including all the interrupt overhead). Even happening at 2000 times a second, it leaves loads of time for other events to be handled. As a 'typical' example, I've got code here, with two serial receive interrupts (9600 & 38400bps), two transmit interrupts, USB interrupts, and a timer, and all get handled without problems on a chip executing less MIPS than yours....
If you make the interrupt _just_ handle what the event implies, there is no problem. However imagine making the chip wait in a serial handler, for another character, or delay for a few uSec. Suddenly instead of 100 instructions for the handler, you are up into tens of thousands of instructions, and then problems appear.

On the timer, setup to use an external clock, it'll just count the pulses. Nothing else. It will generate an INT_TIMER1, when the counter wraps from 65535, back to 0, so if you have a handler for this, and in this increment a counter for the upper 16bits, then you have a 32bit counter, that only uses any processor time every 65536 counts. 65000 times better than trying to handle it with the external interrupt!.....

On the PIC24's, the functions are split up differently to the older PIC's. So the things that were done with the CCP (counter, compare, PWM), are now done by separate modules in the chip.

It's worth perhaps getting your head round just how powerful these PIC's are. Though 'tiny' by the standard of GHz PC's, a chip like yours is executing code about 30* faster than the first desktop computers (Things like the PET, TRS-80, and the first Apple), and has more ROM (the basic TRS-80, just had 16K), and as much RAM. Yet these managed to handle keyboard, serial, display, sounds, software generation/decoding of audio for data storage, etc. etc..
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Mar 10, 2015 7:08 am     Reply with quote

Ttelmah wrote:
It takes less instructions to poll a flag, but it does mean your code has to be sitting in the 'poll loop'. Only needed if you really do require really high speed.

Thing to understand about interrupts, is that they set the interrupt flag, and this _remains set till the interrupt is serviced_. So if (for instance) an event triggers while the code is inside another interrupt, the flag is still set, and the event will be handled as soon as the first interrupt handler exits. This is why the golden rule is to get out of handlers quickly. A serial interrupt handler, that just reads the character, stores it into a buffer, and exits, takes perhaps 100 instructions in total (including all the interrupt overhead). Even happening at 2000 times a second, it leaves loads of time for other events to be handled. As a 'typical' example, I've got code here, with two serial receive interrupts (9600 & 38400bps), two transmit interrupts, USB interrupts, and a timer, and all get handled without problems on a chip executing less MIPS than yours....
If you make the interrupt _just_ handle what the event implies, there is no problem. However imagine making the chip wait in a serial handler, for another character, or delay for a few uSec. Suddenly instead of 100 instructions for the handler, you are up into tens of thousands of instructions, and then problems appear.

On the timer, setup to use an external clock, it'll just count the pulses. Nothing else. It will generate an INT_TIMER1, when the counter wraps from 65535, back to 0, so if you have a handler for this, and in this increment a counter for the upper 16bits, then you have a 32bit counter, that only uses any processor time every 65536 counts. 65000 times better than trying to handle it with the external interrupt!.....

On the PIC24's, the functions are split up differently to the older PIC's. So the things that were done with the CCP (counter, compare, PWM), are now done by separate modules in the chip.

It's worth perhaps getting your head round just how powerful these PIC's are. Though 'tiny' by the standard of GHz PC's, a chip like yours is executing code about 30* faster than the first desktop computers (Things like the PET, TRS-80, and the first Apple), and has more ROM (the basic TRS-80, just had 16K), and as much RAM. Yet these managed to handle keyboard, serial, display, sounds, software generation/decoding of audio for data storage, etc. etc..



Thanks for such deep explanation...

Just like this... Did you ever though of publishing a book on PIC?

Your book will fit with the two others I have (Flying the PIC24 and the new CCS book)

I'll buy you a beer anytime Ttelmah


Smile
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
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