|
|
View previous topic :: View next topic |
Author |
Message |
alex Guest
|
timer0 interrupt |
Posted: Tue May 25, 2004 8:29 am |
|
|
I am trying to write a program to generate 10 pulses at 40 kHz(T=25us) at pin C0.
The ploblem is the setting value of timer0 for timer interrupt at every 12.5 usec cannot be achieve in my following program.
The results from oscilloscope show that the timer interrupt always occurs at every 86 usec even when the timer0 is set at range of 254-235.
Can you suggest me how can I make the timer0 interrup occur at every 12.5 usec(approximately)?
My system is MPLAB ICD Evaluatuion kit.
Fosc= 3.6864 MHz
pic16LF877
CCS C-compiler
-----------------------------------
#include <16F877.H>
#use Delay(clock=3686400)
#define HIGH_START 0
byte seconds, high_count;
boolean i,TimerFlag;
#INT_RTCC
clock_isr() {
if(++high_count!=20) {
i=!i;
output_bit(PIN_C0,i);
set_timer0(235);
}
else {
TimerFlag=0;
output_bit(PIN_C1,0);
disable_interrupts(INT_RTCC);
}
}
main() {
again:
i=1;
TimerFlag=1;
//while (input(PIN_B0)); { /* waiting for press down */
//}
//while (!input(PIN_B0)); { /* waiting for press up */
//}
delay_ms(50);
high_count=HIGH_START;
setup_counters(RTCC_INTERNAL, WDT_2304MS);
enable_interrupts(GLOBAL);
output_bit(PIN_C1,1);
output_bit(PIN_C0,i);
set_timer0(235);
enable_interrupts(INT_RTCC);
while(TimerFlag);{
}
goto again;
}
------------------------------------------------------------------------- |
|
|
Ttelmah Guest
|
Re: timer0 interrupt |
Posted: Tue May 25, 2004 9:00 am |
|
|
alex wrote: | I am trying to write a program to generate 10 pulses at 40 kHz(T=25us) at pin C0.
The ploblem is the setting value of timer0 for timer interrupt at every 12.5 usec cannot be achieve in my following program.
The results from oscilloscope show that the timer interrupt always occurs at every 86 usec even when the timer0 is set at range of 254-235.
Can you suggest me how can I make the timer0 interrup occur at every 12.5 usec(approximately)?
My system is MPLAB ICD Evaluatuion kit.
Fosc= 3.6864 MHz
pic16LF877
CCS C-compiler
-----------------------------------
#include <16F877.H>
#use Delay(clock=3686400)
#define HIGH_START 0
byte seconds, high_count;
boolean i,TimerFlag;
#INT_RTCC
clock_isr() {
if(++high_count!=20) {
i=!i;
output_bit(PIN_C0,i);
set_timer0(235);
}
else {
TimerFlag=0;
output_bit(PIN_C1,0);
disable_interrupts(INT_RTCC);
}
}
main() {
again:
i=1;
TimerFlag=1;
//while (input(PIN_B0)); { /* waiting for press down */
//}
//while (!input(PIN_B0)); { /* waiting for press up */
//}
delay_ms(50);
high_count=HIGH_START;
setup_counters(RTCC_INTERNAL, WDT_2304MS);
enable_interrupts(GLOBAL);
output_bit(PIN_C1,1);
output_bit(PIN_C0,i);
set_timer0(235);
enable_interrupts(INT_RTCC);
while(TimerFlag);{
}
goto again;
}
------------------------------------------------------------------------- |
Setting a timer 'to' a value, where the same timer is used to generate an interrupt, is allways a problem. The reason is simply that when running using no divider, the timer increments for every instruction. The time to actually reach the interrupt handler, involves first the hardware latency, then the much longer delay of saving all the global variables, and then finally calling the handler itself. This typically involves perhaps 25-30 instruction times. With the time needed for the routine, the timer will have then allready incremented by perhaps 40 instructions before you set it. There are then perhaps another 20-25 instruction times to restore the registers before the routine exits. Typically then the minimum interrupt interval, will be about 75-80 instruction times, which corresponds closely to your 86Sec. The processor clock you have is just not fast enough to allow interrupts to be used at the frequency you want.
To achieve the speed you want, your will have to work without an interrupt. Simply code something like:
[code]
int8 count;
count=20;
while (count--) {
output_high(PIN_C0);
delay_cycles(12);
output_low(PIN_C0);
delay_cycles(5);
}
This should be fairly close, but you have to remember that the loop time itself will depend on other features of your program (for instance, if you have *=16 selected, it takes longer to access variables). 'Output_high', and 'output_low', code as single instruction 'bit set/reset' instructions, whereas output_bit, takes a much larger number of cycles. You will need to tweak the actual delay values to get the frequency required. Remember too, that if you have any other interrupts in use, these will interfere with the pulse times...
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
|