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

pic16f688 + RTCC (Timer0 Overflow)

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



Joined: 04 Feb 2009
Posts: 31

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

pic16f688 + RTCC (Timer0 Overflow)
PostPosted: Thu Feb 27, 2014 11:26 am     Reply with quote

Hello everybody. Well, I've been struggling with this for a while.

I want to turn on and off a pin with the timer0 interruption without luck, the time is about 1us, however, when I configure the time in the program, the result it's not the desired. Here is the code.

Code:
#include <16F688.h>


#fuses XT, NOWDT, NOBROWNOUT, NOPUT,  INTRC     
#use delay(clock=4000000)


#int_RTCC
void  RTCC_isr(void)
{
     
     clear_interrupt(INT_TIMER0);             
     output_toggle(PIN_c2);                   
     set_rtcc(255);

}



void main()
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   clear_interrupt(INT_TIMER0);
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RTCC);
   set_timer0(0);
   
   while(true){
 
   }
}


According to my math, with the prescaler of 1, a clock of 4MHz, I can get an overflow every 256us with a resoultion of 1us. If I set the timer0 with a value of 255 I will interrupt it every 1us. However, the oscilloscope shows that the interruption is executed every 62us, I've tried to change with known values without getting something consistent with the math.

Thank you guys.
_________________
everything that has a beginning, has an end
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Feb 27, 2014 12:43 pm     Reply with quote

Hi,

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

I hope that helps you.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
quicksilver



Joined: 04 Feb 2009
Posts: 31

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

PostPosted: Thu Feb 27, 2014 1:26 pm     Reply with quote

Thanks. It helped a lot. But to be honest, my question is not regarding how to set up a TIMER0. My questions is referred why my PIC can't toggle a pin in 1us using the TIMER0. I'm configuring my TIMER0 to overflow every 256us, I set the TIMER0 with a value of 255, in that way the next interruption will be at 1us, right?. Is there any change that the hardware is not capable of interrupting that fast?.

Regards... and thanks for your help.
_________________
everything that has a beginning, has an end
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 27, 2014 1:30 pm     Reply with quote

Quote:
I set the TIMER0 with a value of 255, in that way the next interruption will be at 1us.

Read this thread about the maximum timer interrupt rate:
http://www.ccsinfo.com/forum/viewtopic.php?t=43399
quicksilver



Joined: 04 Feb 2009
Posts: 31

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

PostPosted: Thu Feb 27, 2014 1:45 pm     Reply with quote

Got it. I have to speed up my clock source. damn!!!!! Thank you guys. It's a shame that everything works on paper.
_________________
everything that has a beginning, has an end
Mike Walne



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

View user's profile Send private message

Re: pic16f688 + RTCC (Timero Overflow)
PostPosted: Fri Feb 28, 2014 3:23 am     Reply with quote

quicksilver wrote:
Hello everybody. Well, I've been struggling with this for a while.

I want to turn on and off a pin with the timer0 interruption without luck, the time is about 1us, however, when I configure the time in the program, the result it's not the desired. Here is the code.


According to my math, with the prescaler of 1, a clock of 4MHz, I can get an overflow every 256us with a resoultion of 1us. If I set the timer0 with a value of 255 I will interrupt it every 1us. However, the oscilloscope shows that the interruption is executed every 62us, I've tried to change with known values without getting something consistent with the math.

Thank you guys.

Depends on exactly what you're trying to do.

What you are seeing is about the best you can do using the interrupts.
It takes so many machine cycles to get in and out of the interrupt routine.
So this forces a limit on what you can/can't do.
Anything above this limit can work, anyting below will be clamped as you've found out the hard way.
(We all have to learn.)

If you want to generate a well controlled square wave with us precision in the range 1 to 255us then look at using the hardware PWM.
Your existing 4MHz clock will do the job for you.

Mike

EDIT

You will, of course, have to change to a different PIC.
Either a 14pin equivalent with PWM or a bigger PIC.


Last edited by Mike Walne on Fri Feb 28, 2014 6:59 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Fri Feb 28, 2014 6:00 am     Reply with quote

Just as a comment, _one clock only_...

Currently the poster has both internal RC, and external crystal selected....
quicksilver



Joined: 04 Feb 2009
Posts: 31

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

PostPosted: Fri Feb 28, 2014 8:30 am     Reply with quote

After setting up several TIMER0 values, I came up with the solution. There is a limit in the value that you can set up. In this case that the it's 8 bit timer, there is no way to load a value of 255 or even 254, the maximum value that you can set is 250. I have tried and I'm pushing the PIC to its limit.

My advice, by the time to work with timer, it is necessary to on hand several clocks, and try setting several timer values. if there is no way to get time desire value, try changing the prescaler and the clock.

I appreciate all your effort and comments, it helped a lot. Razz
_________________
everything that has a beginning, has an end
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