|
|
View previous topic :: View next topic |
Author |
Message |
Dam Guest
|
Timers not working properly |
Posted: Sun May 08, 2005 3:47 am |
|
|
Hello everybody,
I have a problem using the timers. I've tested the example file "EX_STWT2.C", but it counts too slowly (about 1/2 s / minute too slow). It seems that the timers begins to count again only when the interruption has been executed. (Nb: I have correctly adapted INTS_PER_SECOND; the problem is the same when using the other timers).
Do you know where's the problem ??
Code: | #define INTS_PER_SECOND 125 // (20000000/(4*16*250*10))
int8 seconds; // A running seconds counter
int8 int_count; // Number of interrupts left before a second has elapsed
#INT_TIMER2 // This function is called every time
void clock_isr() { // timer 2 overflows (250->0), which is
// approximately 125 times per second for
if(--int_count==0) { // this program.
++seconds;
int_count = INTS_PER_SECOND;
}
}
void main() {
int_count = INTS_PER_SECOND;
setup_timer_2(T2_DIV_BY_16, 250, 10);
set_timer2(0);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(TRUE) {
printf("Press any key to begin.\n\r");
getc();
int_count = INTS_PER_SECOND;
seconds = 0;
printf("Press any key to stop.\n\r");
getc();
printf("%u seconds.\n\r", seconds);
}
} |
|
|
|
Damn Guest
|
|
Posted: Sun May 08, 2005 4:09 am |
|
|
Note: I tested it on a 16F876, and my compiler version is 3.205 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun May 08, 2005 8:15 am |
|
|
One error is that the timers are already running before the user has pressed a key, worst case this will give you an offset error equalt to a single interrupt-time. In your case this equals 1/125 second error, not close to the error you have meassured.
I don't see any other obvious errors.
How are you meassuring the time? An error of 0,5 seconds is impossible to clock accurately on a stopwatch. Have you set up some automated testsystem using the serial port to a PC? If yes, then you'll have to correct for the delay introduced for transmitting all data over the serial port. What baudrate are you using? |
|
|
Guest
|
|
Posted: Sun May 08, 2005 9:01 am |
|
|
To see the problem, I let it run a few minutes to see a noticeable difference. I did the test a few times the problem was always there. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun May 08, 2005 12:58 pm |
|
|
Anonymous wrote: | To see the problem, I let it run a few minutes to see a noticeable difference. I did the test a few times the problem was always there. | You didn't answer my questions.
I suspect the way you meassure the time is introducing some 'overhead'.
For example, running the test several times also incorporates the time of transmitting the display texts over RS232. I don't know your baudrate, but at 9600baud the given texts will take almost 0,1 sec. Then you'll have to add to this the turn-around-time of your PC Operating system, I remember an old Windows version where this was default set to a few hundred miliseconds (configurable). All these additional delays could be the cause of your 0.5sec timing offset.
Like I said before, the program looks fine. Without you giving more details on how you took your meassurements I'm not going to invest time in searching for a bug that's likely not there. |
|
|
Dam Guest
|
|
Posted: Sun May 08, 2005 1:50 pm |
|
|
Yes, I understand.
I simply ran the code, hit a key, then wait 8 minutes, then hit another key, and saw the result. I measured the time with the pc clock, and there was (at each test) 4 seconds more than what the PIC told me.
I've tested to make the interrupt code longer: the time difference was yet greater. So it seems that the timer only counts again when the interrupt code has been executed. I'm not a PIC specialist so I can't analyse the ASM code.
This problem really bothers me . Thanks for your help |
|
|
Ttelmah Guest
|
|
Posted: Sun May 08, 2005 4:30 pm |
|
|
The timer runs in the interrupt.
Look carefully at your count values. Realise that the timer counts to 250, and then resets to zero on the _next_ increment. The timer values you are using, will actually run 251/250 'slow'. Should be two seconds, not four seconds, in eight minutes, but this is probably the starting point for the error.
Best Wishes |
|
|
Dam Guest
|
|
Posted: Tue May 10, 2005 3:19 am |
|
|
Ckielstra, do you also think that the problem could come from that ?
Anyway I'm disappointed that this code, provided as example by CCS, doesn't work properly. |
|
|
Ttelmah Guest
|
|
Posted: Tue May 10, 2005 8:20 am |
|
|
Historically the 'wizard', calculated the wrong values for many times, for a lot of CCS versions, so I'd suspect the timings, were set up by the wizard, which happened to be faulty at that point, and this has never been corrected...
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
|