avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] 32-bits time


From: Eric Pasquier
Subject: [avr-gcc-list] 32-bits time
Date: Wed, 12 Oct 2005 22:22:07 +0200

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.
 
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;
 
  sreg=SREG;
  cli();
  tempL=TCNT1;
  tempH=hiword_time;
  SREG=sreg;
 
  return (((uint32_t)(tempH)<<16) + tempL);
}
 
//================================================
// SIG_OVERFLOW1
//------------------------------------------------
SIGNAL( SIG_OVERFLOW1 )
{
  hiword_time++;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]