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

Timer0 and Stopwatch Mplab6

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



Joined: 15 Dec 2003
Posts: 51
Location: Italy

View user's profile Send private message

Timer0 and Stopwatch Mplab6
PostPosted: Sat Feb 28, 2004 5:19 am     Reply with quote

Use pic16f84, clock 4Mhz and TIMER0
I must obtain interrupt every 1 msec

using stopwatch of mplab sim and controlling BREAKPOINT (inside interrupt TIMER0) i obtain interrupt every 1.030 mSec
there are 30 microsecond ones more

you can help?
thanks


#define TIMER_TICK 131 // 1 ms a 4.00 MHz

int8 centomilli, unsecondo, unminuto;

#int_TIMER0
TIMER0_isr()
{
set_timer0(TIMER_TICK); // ricarica timer0
centomilli--; //****** SET BREAKPOINT for MPLAB SIM
}


void main()
{

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
set_timer0(TIMER_TICK);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);

set_tris_B(0b11111111);
set_tris_A(0b00000000);
centomilli = 100;
unsecondo = 10;
unminuto = 60;

while(1)
{
.....
.....
}
Ttelmah
Guest







Re: Timer0 and Stopwatch Mplab6
PostPosted: Sat Feb 28, 2004 3:47 pm     Reply with quote

Max wrote:
Use pic16f84, clock 4Mhz and TIMER0
I must obtain interrupt every 1 msec

using stopwatch of mplab sim and controlling BREAKPOINT (inside interrupt TIMER0) i obtain interrupt every 1.030 mSec
there are 30 microsecond ones more

you can help?
thanks


#define TIMER_TICK 131 // 1 ms a 4.00 MHz

int8 centomilli, unsecondo, unminuto;

#int_TIMER0
TIMER0_isr()
{
set_timer0(TIMER_TICK); // ricarica timer0
centomilli--; //****** SET BREAKPOINT for MPLAB SIM
}


void main()
{

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
set_timer0(TIMER_TICK);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);

set_tris_B(0b11111111);
set_tris_A(0b00000000);
centomilli = 100;
unsecondo = 10;
unminuto = 60;

while(1)
{
.....
.....
}

This has been answered _many_ times.
The problem is setting the timer to the value, does not occur, till after the global interrupt handler has decided what has generated the interrupt. Several 'ticks' of the timer will have allready occured, when you reach the point where you set the counter.
You can 'improve' on the effect, by using:
set_timer0(get_timer0()+TIMER_TICK);

but even with this, there may be a tick between reading the register and writing it.
Do a search on the archives, and you will find a lot of posts about it.
You would be _much_ better off using a chip that has a second programmable hardware timer. Or using a different crystal, and letting the timer free run (if you use a 4.096MHz crystal, which is common, and is 'in spec' for the 4MHz part, then run the timer at /4, it will time out correctly at 1mSec intervals).

Best Wishes
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