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

timer2 sample program

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



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

View user's profile Send private message

timer2 sample program
PostPosted: Fri Mar 04, 2011 1:49 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 04, 2011 1:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 04, 2011 2:09 pm     Reply with quote

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... Sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 04, 2011 2:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 04, 2011 3:02 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 04, 2011 5:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 04, 2011 6:09 pm     Reply with quote

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 Shocked that will keep me busy next week probably... Rolling Eyes
But Thanks for this hint, that's a beginning...!
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