|
|
View previous topic :: View next topic |
Author |
Message |
Max
Joined: 15 Dec 2003 Posts: 51 Location: Italy
|
Timer0 and Stopwatch Mplab6 |
Posted: Sat Feb 28, 2004 5:19 am |
|
|
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 |
Posted: Sat Feb 28, 2004 3:47 pm |
|
|
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 |
|
|
|
|
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
|