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

timer2 period program
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
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

timer2 period program
PostPosted: Tue Sep 14, 2010 8:57 am     Reply with quote

hi

I need have an interrupt every 12.4ms, for that I try program timer1 but i don't know how I can make that manually...

I use CCS wizard and it program this "setup_timer_2(T2_DIV_BY_16,242,16);"

Is correct? every 12.4ms I have an call to timer1 routine?

What is formula to program timer manually?

Is possible use timer1 to have an interrupt every 12.4ms?

best regards
Geps



Joined: 05 Jul 2010
Posts: 129

View user's profile Send private message

PostPosted: Tue Sep 14, 2010 9:30 am     Reply with quote

Which PIC and crystal combination are you using?
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Tue Sep 14, 2010 12:27 pm     Reply with quote

hi

sorry....

PIC18F252 with crystal 20Mhz

best regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Tue Sep 14, 2010 1:20 pm     Reply with quote

Master clock/4

Divide by prescaler (/16).

Divide by second value+1 (/243)

Divide by postscaler (/16).

So interrupt frequency in your case, is 20000000/(4*16*243*16) = 80.37Hz

1/80.37=0.0124416
12.4416mSec

To calculate for yourself, work the other way.

1/0.0124=80.645

5000000/80.645 = 62000 (master clock is divided by 4)

So the prescaler*postscaler*(count+1) needs to be 62000.
However you have limited values for all the values (data sheet), so the automatic values look to be as close as possible.

Rermember it is the timer2 interrupt that is called (you refer to timer1....).

Best Wishes
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Tue Sep 14, 2010 3:34 pm     Reply with quote

hi

Thanks for this excellent explanation...

Where I can find more information like that to calculate timer0, timer1 and timer3?

Best regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu Sep 16, 2010 4:55 pm     Reply with quote

Hi

I need join on my program other pulse with 12.4ms, so I need use other timer, for example timer1...

What is process for I can calculate timer1 with 12.4ms?

best regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu Sep 16, 2010 7:21 pm     Reply with quote

Hi

I read read CCS manual but I can get one way to make timer1 with interrupts every 12.4ms....

Someone can explain me?

best regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Thu Sep 16, 2010 7:37 pm     Reply with quote

Hi

Sorry for my insistence.

If I have my timers 1 and 2 configured like that:
Code:

enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);

setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);   // periodo de 12.4ms
set_timer1(7750);

enable_interrupts(INT_TIMER2);
setup_timer_2(T2_DIV_BY_16,242,16);       // periodo de 12.4ms

For timer1 to generate an interrupt every 12.4ms I add "set_timer1(7750)".

This stay correct?
I have two pulses with different timers?

best regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Fri Sep 17, 2010 3:50 am     Reply with quote

Seriously, _read the data sheet_.
The timer operation, is nothing to do with CCS. It varies between different chips. The 'bible' for this, is the data sheet of the chip, which tells you where the clock can come from, what pre-scalers, post-scalers, and division ratios are available.
There is also an application note on the Microchip site, giving an 'overview' of configuring/using the timers, on a 'mid range' PIC, and most of this applies to the other families as well.

Best Wishes
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Mon Oct 18, 2010 9:32 am     Reply with quote

Hi

I need program my timers during program run but I don't know how I can configure timer2.

My actual configuration:
Code:

setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);   // periodo de 13.1ms
enable_interrupts(INT_TIMER1);

setup_timer_2(T2_DIV_BY_16,243,16);       // periodo de 12.4ms
enable_interrupts(INT_TIMER2);

On timer1 routine I need configure it to 1ms and I make this:

65535 - (1ms/(4/20Mhz))

and put result on set_timer1(xxxx);

But I don't know how I can make this to timer2...

Someone can help?

best regards
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Oct 18, 2010 12:57 pm     Reply with quote

Thelmah gave you a practical way regarding how to calculate the interrupt period, sorry I do not understand
what is your real problem right now:
- the timer do not behave as you expected.
- the timer is rolling back but the time is not the expected.
- the timer is rolling back but the interrupt is not generated.

I would like to know how are you testing the timers code, in real hardware or in a simulator way.

Please read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=22467&highlight=timers

Regards
MiniMe



Joined: 17 Nov 2009
Posts: 50

View user's profile Send private message

PostPosted: Mon Oct 18, 2010 2:55 pm     Reply with quote

You can use application with nice user interface to have a idea about effect of different values (prescalers, counters, exact time values... )


http://pictimer.picbingo.com/
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Mon Oct 18, 2010 3:05 pm     Reply with quote

hi

Thanks for sugestion...

I find on CCS manual and formula is: 256 - (temp/(4/clock)) without prescale...

But I use prescale and postscale... setup_timer_2(T2_DIV_BY_16,242,16);

Is correct I make this calcs: 256 - (temp /((242*4)/20Mhz))

Or I need change 256 to 242? or need make other thing?

Or stay wrong?
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Oct 18, 2010 4:20 pm     Reply with quote

Quote:

Or I need change 256 to 242? or need make other thing?

It depends of the interrupt handler overhead, this value is used to fine trim the period in your real application.
Thats why it is important to know 'how' are you testing your project.

Regards
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Mon Oct 18, 2010 4:37 pm     Reply with quote

You can see my timer configuration and I think which my timer only go at 242.

So on my formula I think correct use 242 to subtract or I need use always "256 - (temp......"?

I need this configuration to generate pulses...
Code:

#int_TIMER1
void TIMER1_isr(void)
{
   if (!pause_cycle)
   {
      output_high(S1SP);
      set_timer1(57535);      //1.6ms -->> 65535 - (1.6ms/(4/20Mhz))
      pause_cycle = 1;       
   } else
   {
   output_low(S1SP);
   pause_cycle = 0;
   }
   
}

#int_TIMER2
void TIMER2isr(void)
{
   if (!pause_cycle)
   {
      output_high(S2DR);
      [b]set_timer2(???????);      //1.4ms --->> 242 - (temp /((242*4)/20Mhz)) ???[/b]
      pause_cycle = 1;
   } else
   {
   output_low(S2DR);
   pause_cycle = 0;
   }
}


How I can make timer2 pulse with 1.4ms???

best regards
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