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 support@ccsinfo.com

timer 2, 4 & 6 issues

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 28, 2011 6:39 pm     Reply with quote

Post a short, compilable test program that demonstrates the problem.
The program should be similar to the one shown below. It's very short.
It only contains code needed to show the problem and no more.
It can be copied-and-pasted into MPLAB and it will compile with no errors.

Also post your compiler version.

Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

// Blink the LED on pin B0 at approximately 1 Hz.
#int_timer1
void timer1_isr(void)
{
output_toggle(PIN_B0);
}

//======================================
void main()
{
output_low(PIN_B0);

setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(0);

clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);


while(1);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19255

View user's profile Send private message

PostPosted: Tue Mar 01, 2011 3:45 am     Reply with quote

Also think about overhead.
You have a 10Mhz clock. 2.5MIPS.
It takes typically about _30_ instruction times to get into the interrupt handler.
So about 12uSec, to actually get into the handler!. It then takes about the same time to get out of the handler again. So with your processor, doing _nothing else_, you could get to about 24uSec pulses....
So, 'no', you will not get "nice pulses slightly over 20uS"...

Also, if the interrupt is continually being called, then you will only get one instruction of the main code executed between each successive interrupt call, so performance of the main code will disintegrate....

If you want pulses at this sort of frequency, you need to let the _hardware_ do it. (PWM).

Then use the third parameter in the timer2 settings (count between interrupts), to give the interrupt rate required.

So if you wanted '20uSec' as the output period, implying 200 cycles of the clock, something like:

setup_timer_2(T2_DIV_BY_1, 49, 10);

Remember the timer clock is already crystal/4.
Then:

setup_ccp1(CCP_PWM);
set_pwm1_duty(100L);

Will give a square wave at 50KHz on the PWM output.
The interrupt will be called every tenth cycle of this clock (200uSec), and you do whatever you need in here (but keep it quick....).

Best Wishes
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

PostPosted: Tue Mar 01, 2011 11:08 am     Reply with quote

Thanks for these two replies, I haven't gotten to write a test project to demonstrate the problem yet and it seems as if I need to give some more information, I'm not looking for something pulsing at 20uS but for single pulses of 20uS appearing in a frequency of ~50-60Hz (can be 100Hz or 120Hz too) but nothing too critical...
thus,
I set e.g. timer1 to kick in every 20ms where i set my output and i also set timer2 to kick in in 20uS to reset my output. Doesn't this sound like something that should be possible with the timers? Or would be better of studying PWM anyways? Also I need 3 pulses that may be synchronized, may be offset to each other or may even have different frequencies.

Thanks for hints!
5440



Joined: 08 Feb 2009
Posts: 11

View user's profile Send private message

PostPosted: Tue Mar 01, 2011 12:28 pm     Reply with quote

I wrote one for a 1ms TMR INT, but now do my interupt handlers (GLOBAL INTS) manually, using ASM where required.

It was more work and lots of re-reading of the data sheet, but I now have a better understanding of the complier and 18F part I'm using. I can also adjust for interupt latency best I can, so I can get close to 1ms TMR events.

One just has to make sure they save all the correct registers in the ISR using this method. It's not 100% predictable as I have other INT's enabled, but works for me.

I have this one set up as a array of timers with a 1ms base (20MHZ, 18F part with TMR as HIGH priority), sort of like how timers are implemented in a PLC (ie Control Logix). Every 1ms int event just increments a counter and exits the ISR. The main program handles the software timer over head. These GP timers work well for debouncing, poors man's PWM with a period =100ms and duty cycles of 10%, 20% etc. I'm about to set up another GP timer to produce a 50ms sample tick for my PID routine. This method of software timers seems to work well assuming your longest/worst case scan time is shorter than the choosen timer base.
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

PostPosted: Tue Mar 01, 2011 2:10 pm     Reply with quote

Uh, I just figured out that I don't need any pulses that are as short as 20uS, they actually won't go shorter than 200uS so I'm probably still gonna stick to my timer solution...
First figure out which one is the smallest and then set the "frequency timers" with delays in order to get the correct offsets... in the "frequency timers" that would kick in every 20ms or similar, I would set the pin and each 8bit timer that would kick in after 200uS (or more) to reset the pin again.... that doesn't seem like a bad solution to me... anyone?
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