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

Timers - what you should know
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
harshanahnd



Joined: 22 Nov 2008
Posts: 11

View user's profile Send private message

PostPosted: Wed Jan 14, 2009 9:47 am     Reply with quote

That keyword does not work. Actually there is no keyword as such. I learn that just by not specifying the bit value you get a 16bit timer by default.

Code:

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
c0de



Joined: 14 May 2007
Posts: 14

View user's profile Send private message

Re: Timers - what you should know
PostPosted: Wed Apr 01, 2009 7:47 am     Reply with quote

Hello.
I have a question regarding this tutorial.
In this case:
Code:
void main()   {
   int i;
   setup_timer_0(RTCC_INTERNAL|RTTC_8_BIT|RTCC_DIV_4);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   ...
   i=0;
   while(TRUE);                              // loop forever
}


the first interrupt occurs after 1024us and using timer0low = timer0low + 6; in the interrupt function the next interrupts occur after 1000us (if I'm not wrong).
Is it possible to have something like in the next code, in order to make the 1st interrupt occur after 1000us? (Or, can we change the timer value while it counts?)

Code:
void main()   {
   setup_timer_0(RTCC_INTERNAL|RTTC_8_BIT|RTCC_DIV_4);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   timer0low = timer0low + 6; // <-----
   while(TRUE){ // loop
   ....
   }                         
}


Thank you.
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

this is absolutely super bro
PostPosted: Mon Sep 14, 2009 6:01 am     Reply with quote

Really I am new to PIC and this gave me clear cut picture of timer based interrupts. Now I am very good at the timer based interrupt concept.

I have a doubt. Can I enable multiple timers such as timer 0, timer 1, and timer2 in a single program?

Could you please write a tutorial on step by step implementation of counter. Cool

I will be very glad if concept known persons start posting tutorials like this with their individual topics (PWM, ADC, ext interrupt, counter, about flags etc) in the CCS. Rolling Eyes (too much)

It will be very much useful for newbee like me. Cool

Thanks in advance. If you post a new thread on counters or any other. Surprised
Baesy



Joined: 06 Nov 2009
Posts: 4

View user's profile Send private message

Timer0 Interrupt with PIC18F45K20
PostPosted: Fri Nov 06, 2009 4:27 am     Reply with quote

Hello

I work with the "MPLAB IDE" development environment, the CCS C Compiler and the "PIC18F45K20" (from the PicKit3). I want to program a example project with the Timer0. He should give an interrupt, when the timer overflows. I look in the CCS description and try different syntaxes, but it doesn't work. I know how an interrupt works, but I don't know how I have to write it. I have just a few experience in c-programming, but I haven't work with the CCS Compiler. I hope anybody can help me.
Code:

#include "18F45K20.h"

#fuses INTRC,NOWDT,NOPROTECT,NOMCLR   

#int_timer0
timer0_isr()
{
   OUTPUT_BIT(PIN_D0, 1);
   clear_interrupt(int_timer0);
}

void main(void)
{
   setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_2);
   enable_interrupts(GLOBAL);

   set_tris_d(0);                  
   set_tris_b(1);
   
   set_timer0(0);

   while (1);

} //main

PS: Sorry for my english, I'm a Student from Switzerland.
rhaguiuda



Joined: 05 Sep 2007
Posts: 46
Location: Londrina - Brazil

View user's profile Send private message

PostPosted: Fri Nov 06, 2009 6:40 am     Reply with quote

Hi Baesy

There are some considerations about your code.

You don`t need to clear_interrupt. CCS compiler does that auto for you.

And your code isn`t working because you must enable timer0 interrupt, not only the Global Interrupt. PIC microcontroller has individual interrupt switches.

Use:
Code:
enable_interrupts (INT_TIMER0);


Hope that helps.
_________________
Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll feed him for a lifetime.
Baesy



Joined: 06 Nov 2009
Posts: 4

View user's profile Send private message

PostPosted: Fri Nov 06, 2009 7:19 am     Reply with quote

Thanks a lot. It works now. Razz
Charliem



Joined: 01 Mar 2010
Posts: 12

View user's profile Send private message

PostPosted: Wed Mar 10, 2010 8:11 pm     Reply with quote

About the tutorial at the start of this post.... what pic has a 16bit timer0?
_________________
Regards
Charlie M
newguy



Joined: 24 Jun 2004
Posts: 1491
Location: Edmonton, Alberta

View user's profile Send private message

PostPosted: Thu Mar 11, 2010 8:19 am     Reply with quote

A lot of them do. Timer 0 of the 18F4520 is 8/16 bit selectable.
Charliem



Joined: 01 Mar 2010
Posts: 12

View user's profile Send private message

PostPosted: Thu Mar 11, 2010 6:15 pm     Reply with quote

Ok I was thinking of 16F pics.
_________________
Regards
Charlie M
pacman91



Joined: 17 Jun 2011
Posts: 28
Location: Malaysia

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 17, 2011 7:11 am     Reply with quote

I also using PIC18f4520 now, got 4 timer, timer0,1,2,3, alot make confuse eh, but after i saw this post, make me learn much more than i read in the text book i borrow=.=......good job, luv this guy have passion to explain all the things Razz
steve.term@yahoo.co.uk



Joined: 09 Nov 2012
Posts: 1

View user's profile Send private message

Internal Clock Divide by 4
PostPosted: Fri Nov 09, 2012 12:13 pm     Reply with quote

Hi Guys,

Great topic, thanks for all the info. Can someone explain why the internal clock divides the primary osc by 4? What's the thinking behind that?
Also if I then use a 4x prescaler does that mean its actually a divide by 16 relative to the external osc?

Thanks,
Steve
Mike Walne



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

View user's profile Send private message

PostPosted: Fri Nov 09, 2012 12:21 pm     Reply with quote

Quote:
Great topic, thanks for all the info. Can someone explain why the internal clock divides the primary osc by 4? What's the thinking behind that?

Probably because it make the timers advance once per instruction cycle which for many processors is 4 cycles of the crystal (or whatever).

Quote:

Also if I then use a 4x prescaler does that mean its actually a divide by 16 relative to the external osc?
Yes

Mike
newguy



Joined: 24 Jun 2004
Posts: 1491
Location: Edmonton, Alberta

View user's profile Send private message

Re: Internal Clock Divide by 4
PostPosted: Fri Nov 09, 2012 12:53 pm     Reply with quote

steve.term@yahoo.co.uk wrote:
Hi Guys,

Great topic, thanks for all the info. Can someone explain why the internal clock divides the primary osc by 4? What's the thinking behind that?
Also if I then use a 4x prescaler does that mean its actually a divide by 16 relative to the external osc?

Thanks,
Steve


Consult your PIC's datasheet. The 16F- and 18F- series PICs I'm familiar with have this internal divide by 4 but the dsPICs (the ones I've played with) have a divide by 2.
Mike Walne



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

View user's profile Send private message

PostPosted: Fri Nov 09, 2012 4:09 pm     Reply with quote

It seems to be consistent.

With the 16 & 18 series devices the master clock divides by 4 for the timers and the instruction cycle.

For the dsPICs the master clock divides by 2 for both.

Mike

EDIT The (I believe now defunct) Harris RTX2000 devices executed most instructions at a rate of one instruction per clock cycle.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 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