View previous topic :: View next topic |
Author |
Message |
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
timer2 sample program |
Posted: Fri Mar 04, 2011 1:49 pm |
|
|
Hi There,
I wrote a little sample program to display the problem I have:
Code: | #include <18F87K22.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HSM //Hi-Speed crystal oscillator
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPLLEN //No PLL enabled
#FUSES BBSIZ1K //1K words Boot Block size
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
float fixval=0;
int16 tim0val=0;
int8 tim2val=0;
int16 time=261;
int1 out=0;
int main (void){
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_4 );
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
tim0val==65560-(625000*(1/50));
fixval=1/((float)10/4/4);
tim2val=time/fixval;
setup_timer_2(T2_DIV_BY_4, tim2val, 1);
set_timer0(tim0val);
while(1);
}
#int_TIMER2
void TIMER2_isr()
{
output_low(PIN_D1);
}
#INT_TIMER0
void TIMER0_isr() {
set_timer0(tim0val);
output_high(PIN_D1);
set_timer2(0); // set timer6 to reset sync pulse
} |
Time is the time in uSec how long a pulse should be HI for. Timer0 is acting as a "frequency timer" where Timer2 is acting as a "pulse timer". I'm not worried about timer0 but timer2. The time is now set to 261 and everything looks fine on my scope, the pulse is approx 261uS long but if I set it to 260, the pulse only has a length of ~50uS... why is that? The tim2val value passed to setup_timer_2() looks fine... any clues?
Thank you! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19541
|
|
Posted: Fri Mar 04, 2011 1:59 pm |
|
|
What's the difference in C, between '==', and '='?.
Study your code. What will tim0val be set to?.
Best Wishes |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Fri Mar 04, 2011 2:09 pm |
|
|
Ttelmah wrote: | What's the difference in C, between '==', and '='?.
Study your code. What will tim0val be set to?.
Best Wishes |
Hoops yes, typo however, one "=" sign removed doesn't change anything on the actual Timer2 problem... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 04, 2011 2:58 pm |
|
|
You give us a timing problem but you leave out the #use delay() and the
crystal frequency. (And the compiler version). |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Fri Mar 04, 2011 3:02 pm |
|
|
PCM programmer wrote: | You give us a timing problem but you leave out the #use delay() and the
crystal frequency. (And the compiler version). |
Yes, you're right. Sorry:
The clock frequency is 10MHz, the delay is set to #use delay(clock=10000000) and the compiler is 4.119 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 04, 2011 5:55 pm |
|
|
One of your problems is that you're letting it run (ie, enabling interrupts)
before you have setup all parameters and loaded your timers.
Move the 3 lines of interrupt enable code down lower, so they're just
before the while() statement at the end. |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Fri Mar 04, 2011 6:09 pm |
|
|
So enabling the interrupt comes after setup_timer_X() and set_timerX()? Okay... I'm having a whole bunch of other issues with my timers too that will keep me busy next week probably...
But Thanks for this hint, that's a beginning...! |
|
|
|