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 support@ccsinfo.com

Why CCP1 Compare mode timer not match

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







Why CCP1 Compare mode timer not match
PostPosted: Thu Jun 09, 2005 10:55 pm     Reply with quote

#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







PostPosted: Fri Jun 10, 2005 3:23 am     Reply with quote

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







PostPosted: Wed Jun 15, 2005 3:59 am     Reply with quote

Thank you.
deskcard
Guest







thank you
PostPosted: Wed Jun 15, 2005 3:59 am     Reply with quote

Thank you.
deskcard
Guest







thank you
PostPosted: Wed Jun 15, 2005 4:00 am     Reply with quote

Thank you.
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