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

how to use timer0

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



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

how to use timer0
PostPosted: Thu Oct 18, 2012 7:29 am     Reply with quote

i did programming using delays. looking forward to do with timer0.
compiler - CCS c compiler
pic - 18F2520.
internal oscillator - 4MHz

Please help how to start with? how to calculate time?

thanks in advance Smile
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Oct 18, 2012 9:27 am     Reply with quote

It's all in the data sheet.

Timer0 advances on each instruction cycle that's 4 oscillator cycles or 1us, with your 4MHz clock.

Timer0 can be either an 8 or 16 bit counter timer.

You use Timer0 overflow to trigger an interrupt.

So in 8 bit mode it will trigger every 256us with no prescaler.

In 8 bit mode and 1:2 prescaler interrupts at 512us etc.

Mike
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

PostPosted: Thu Oct 18, 2012 9:33 am     Reply with quote

Thanks for the information.
I'm blinking the led with 5 sec delay. please tell that I'm doing it right?
Code:

#include "18F2520.h"
#fuses HS, NOPROTECT, INTRC_IO
#use delay(clock=4000000)   // 4MHZ internal clock

//   fout = fclk/(4*prescaler* (256-TMR0)*count)
//    for prescaler = 1 : 256 and TMR0 = 0
//   if need 5sec delay
//   Tout = 1/fout = 1/0.2Hz = 5sec
//   count = 4Mhz/(4*256*(256-0)*0.2Hz) = 76.29 = 76 counts(approxi)

unsigned int count;

#int_RTCC
void clock_isr()
{
set_timer0(0);
count++;
if(count==76)    // 76 for 5sec  delay // 15 for 1 sec delay
  {
   output_high(PIN_B7);
   count=0;
  }
}


void main()
{
set_tris_b(0x00);
setup_oscillator(OSC_4MHZ);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);

while(1)
  {
   output_high(PIN_B6);
  }

}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Oct 18, 2012 11:09 am     Reply with quote

I'm assuming you've got an LED connected to pin B7.

As it stands you're turning the LED on after 5s then no more changes.

If you want it to flash you can use the toggle function.

If you have a real board in front of you, it should be obvious by now.

You don't need the set_timer(0) line. When timer0 overflows its value will be zero. By the time you get into the interrupt routine timer0 will have advanced, so resetting it to zero again screws up your timings.

Mike
juan



Joined: 21 Apr 2015
Posts: 3
Location: argentina

View user's profile Send private message AIM Address ICQ Number

PostPosted: Tue Apr 21, 2015 3:08 pm     Reply with quote

Mike Walne wrote:
I'm assuming you've got an LED connected to pin B7.

As it stands you're turning the LED on after 5s then no more changes.

If you want it to flash you can use the toggle function.

If you have a real board in front of you, it should be obvious by now.

You don't need the set_timer(0) line. When timer0 overflows its value will be zero. By the time you get into the interrupt routine timer0 will have advanced, so resetting it to zero again screws up your timings.

Mike


me sirvio el ejemplo, gracias
juan



Joined: 21 Apr 2015
Posts: 3
Location: argentina

View user's profile Send private message AIM Address ICQ Number

Re: how to use timer0
PostPosted: Tue Apr 21, 2015 3:11 pm     Reply with quote

hemnath wrote:
i did programming using delays. looking forward to do with timer0.
compiler - CCS c compiler
pic - 18F2520.
internal oscillator - 4MHz

Please help how to start with? how to calculate time?

thanks in advance Smile


Can you give me an example ? Thanks.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sat Apr 25, 2015 12:33 pm     Reply with quote

maybe this will point you in a helpful direction,

http://www.ccsinfo.com/forum/viewtopic.php?t=53857
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Sun Apr 26, 2015 2:28 am     Reply with quote

Let me add a couple of comments.

In the code library, there is a routine 'Timer based Real Time Clock (RTC)', which shows a way to generate long term accurate times by actually effectively counting clock cycles between the interrupts. This approach can be used without the complexity of actually updating a set of clock variables, to give nice accurate times from any timer. Worth studying.

The start of this post also mentions the other approach. Basically if you have a timer like timer2, which can be programmed for a particular count, then you can generate an interrupt that occurs at a much nicer interval. So (for instance), if you set timer2 to use 249 (so counts 0 to 249), (Fosc/4)/4, and interrupt every ten cycles, then this interrupt will give:

4000000/(4*4*250*10) = 100Hz.

Advantage is it makes counts really easy to work out!. Also generally it is useful to have a system 'tick' at a nice regular interval like this. Downside is the limited number of timers supporting this, hence the other approach if needed.

Generally it'd be much easier to see what was going on, if the interrupt did something like toggling the LED....
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