|
|
View previous topic :: View next topic |
Author |
Message |
deskcard Guest
|
Why CCP1 Compare mode timer not match |
Posted: Thu Jun 09, 2005 10:55 pm |
|
|
#include <16F877A.h> // Standard Header file for the PIC16F877A device
#fuses HS,NOWDT,NOPROTECT,NOLVP // Configuration word
#use delay(clock=20000000) // oscillator
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // standard output
// Global variables -------------------------------------------------------/
int16 time;
// Functions : compare_isr ------------------------------------------------/
// Description : compare interrupt
// ------------------------------------------------------------------------/
#INT_CCP1
void compare_isr() {
time = get_timer1(); // Get Timer1
printf("\r\nCompare set interrupt");
printf("\r\nTimer1: %lu us",time);
disable_interrupts(GLOBAL); // All interrupts OFF
}
// Main program -----------------------------------------------------------/
void main(void) {
setup_ccp1(CCP_COMPARE_INT); // Interrupt on compare mode
setup_timer_1(T1_INTERNAL); // Set up timer to instruction clk
enable_interrupts(INT_CCP1); // Enable interrupt CCP1
enable_interrupts(GLOBAL); // All interrupts ON
set_timer1(0); // Set Timer1
CCP_1 = 500 // Set CCPR1L register
// Set high time limit to 100 us
// limit is time/(clock/4)
// 500 = .0001*(20000000/4)
while(TRUE);
}
output :
Compare set interrupt
Timer1: 538
why ? overtime 38 us |
|
|
Ttelmah Guest
|
|
Posted: Fri Jun 10, 2005 3:23 am |
|
|
Multiple things. When the event occurs, the interrupt is then responded to in the next cycle (501). The jump to the interupt handler itself takes a cycle. Then the global interrupt handler saves all the registers that may be needed. Typically perhaps 28 machine cycles. Then the handler itself is called (another couple of cycles), then the routine to read the registers itself takes another couple of cycles. Typically, I'd perhaps expect about 535 cycles to be shown.
The 'overtime', is not 38uSec, but 38 instruction times (7.6uSec at 20MHz).
Seperately, the 'disable interrupt' in the interrupt handler won't work. The global interrupt enable bit, is cleared when an interrupt is called, and automatically set again when the interrupt exits. If you want to prevent the interrupt from repeating, have a flag, that is set when the code is called, then in the 'main' loop, test this flag, and disable interrupts when it is set.
Best Wishes |
|
|
deskcard Guest
|
|
Posted: Wed Jun 15, 2005 3:59 am |
|
|
Thank you. |
|
|
deskcard Guest
|
thank you |
Posted: Wed Jun 15, 2005 3:59 am |
|
|
Thank you. |
|
|
deskcard Guest
|
thank you |
Posted: Wed Jun 15, 2005 4:00 am |
|
|
Thank you. |
|
|
|
|
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
|