Dear
All,
I have implemented a 32-bits time information using
the code below.
Timer1 is incremented using internal clock, prescaler=1;
SIG_OVERFLOW interrupt is used to increment a variable used to complete the 16-bits of Timer1.
Timer1 is incremented using internal clock, prescaler=1;
SIG_OVERFLOW interrupt is used to increment a variable used to complete the 16-bits of Timer1.
In fact, the code below have the following problem:
if the call is made exactly when the counter overflow, TCNT1 (tempL) is equal to zero, but hiword_time
(tempH) is pending to be incremented, leading to an error (time is returning in the
past).
Does anybody has a solution ?
I was thinking
testing the interrupt flag during the critical section.
Is TCNT1=0 the only case possible
?
Eric.
static uint16_t hiword_time;
//================================================
// timer32_now
// Retrieve the current time from the global clock in Timer1,
// disabling interrupts to avoid stomping on the TEMP register.
//------------------------------------------------
inline uint32_t timer32_now(void)
{
int8_t sreg;
uint16_t tempL, tempH;
// timer32_now
// Retrieve the current time from the global clock in Timer1,
// disabling interrupts to avoid stomping on the TEMP register.
//------------------------------------------------
inline uint32_t timer32_now(void)
{
int8_t sreg;
uint16_t tempL, tempH;
sreg=SREG;
cli();
tempL=TCNT1;
tempH=hiword_time;
SREG=sreg;
cli();
tempL=TCNT1;
tempH=hiword_time;
SREG=sreg;
return
(((uint32_t)(tempH)<<16) + tempL);
}
}
//================================================
// SIG_OVERFLOW1
//------------------------------------------------
SIGNAL( SIG_OVERFLOW1 )
{
hiword_time++;
}
// SIG_OVERFLOW1
//------------------------------------------------
SIGNAL( SIG_OVERFLOW1 )
{
hiword_time++;
}
_______________________________________________ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list