#define START_OF_READING 0
#define MIDDLE_OF_READING 1
#define END_OF_READING 2
#define SAMPLE_COUNT 2
// being explicit about the types ( for quick readability )
unsigned int8 stateOfReading;
unsigned int8 timer1Overflow;
unsigned int16 uiCount, uiCountBefore, uiCountAfter, uiCountStart;
//int1 state = 0;
//#define printf
// Fires when timer1 overflows
#int_TIMER1
void TIMER1_isr(void)
{
++timer1Overflow;
//output_bit( PIN_C0, (state = !state));
}
#int_CCP1
void CCP1_isr(void)
{
/*
output_bit( PIN_C0, state );
state = !state;
//*/
uiCountStart = get_timer1();
if ( stateOfReading == START_OF_READING ) // first edge has arrived
{
stateOfReading = MIDDLE_OF_READING;
timer1Overflow = 0;
set_timer1 ( 0 ); // restart timer on this edge
}
else if ( stateOfReading == MIDDLE_OF_READING ) // second edge has arrived
{
uiCountBefore = get_timer1();
uiCount = CCP_1; // get capture value. *Theoretically* should be same as writing iCount = get_timer1(); ( Practically though, it never is even close ? Does the timer1 value get copied into CCP1 AFTER the ISR is serviced )
uiCountAfter = get_timer1();
stateOfReading = END_OF_READING; // prevent further processing during this interrupt
}
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_spi(SPI_SS_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_1( T1_INTERNAL | T1_DIV_BY_4 );
//setup_timer_1( T1_INTERNAL );
setup_ccp1( CCP_CAPTURE_RE | CCP_CAPTURE_DIV_4 ); // Configure CCP1 to capture every 4th rising edge\
//setup_ccp1( CCP_CAPTURE_RE );
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP1);
//enable_interrupts(GLOBAL);
disable_interrupts( GLOBAL );
stateOfReading = END_OF_READING; // We don't want to set this to either START_OF_READING or MIDDLE_OF_READING - as that would let the ISR code execute
unsigned int8 dx = 0;
while( TRUE )
{
unsigned int32 totalTicks = 0;
for ( dx = 0; dx < SAMPLE_COUNT; ++dx )
{
stateOfReading = START_OF_READING; // allow ISR to execute
enable_interrupts( GLOBAL );
while ( stateOfReading != END_OF_READING );
disable_interrupts( GLOBAL );
printf ( "[%d] timer1Overflow = %d, uiCount = %Lu/%Lu/%Lu/%Lu\r\n", dx, timer1Overflow, uiCountStart, uiCountBefore, uiCount, uiCountAfter );
totalTicks += ( 0x10000 * timer1Overflow ); /* Every overflow -> 65536 ticks */
totalTicks += uiCountStart;
}
printf ( "RAW totalTicks = %Ld\r\n", totalTicks );
totalTicks /= SAMPLE_COUNT;
}
}
[0] timer1Overflow = 3, uiCount = 3387/3392/3377/3396
[1] timer1Overflow = 3, uiCount = 3387/3392/3377/3396
RAW totalTicks = 399990
Kurenai_ryu said:mmmm are you sure it's for a 16f876A it doesn't work connecting a virtual terminal to RX/TX (RC6, RC7) and a pulse generator to RC2...
Kurenai_ryu said:
setup_ccp1( CCP_CAPTURE_RE | CCP_CAPTURE_DIV_4 ); // Configure CCP1 to capture every 4th rising edge\
#int_TIMER1
void TIMER1_isr(void)
{
++timer1Overflow;
}
#int_TIMER1
void TIMER1_isr(void)
{
if(stateOfReading == MIDDLE_OF_READING )
++timer1Overflow;
}
Kurenai_ryu said:well... doing some tests.. i found a bug in both our codes!!!
where:should beCode:#int_TIMER1 void TIMER1_isr(void) { ++timer1Overflow; }
Code:#int_TIMER1 void TIMER1_isr(void) { if(stateOfReading == MIDDLE_OF_READING ) ++timer1Overflow; }
all of them with the "before" variable, it seems the most accurate
ok, it's a bug in my code, i got more timeroverflows while printing the message...vsmGuy said:Kurenai_ryu said:well... doing some tests.. i found a bug in both our codes!!!
where:should beCode:#int_TIMER1 void TIMER1_isr(void) { ++timer1Overflow; }
Code:#int_TIMER1 void TIMER1_isr(void) { if(stateOfReading == MIDDLE_OF_READING ) ++timer1Overflow; }
Why do you say so?
If you look closely - you will notice that:
1. I enable interrupts only during the reading
2. I reset the timer at the start of the CCP1 ISR
yeah I think the same!all of them with the "before" variable, it seems the most accurate
It should not be so - I will come to that later
hugo said:Hi,
This look much simpler to me (mikroC) :
**broken link removed**
"This is a simple capture a lenght of pulse. Capture from rising edge
to rising edge and send to terminal via RS232"
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?