Seting a bit. What is that?

HI,

I am currently reading a book on Arduino Timers and let me reproduce an excerpt from that:

Clear Timer on Compare Match (CTC)
In the CTC mode, the TCNT0 timer is reset to zero every time the TCNT0 counter reaches the value set in the Output Compare Register A (OCR0A) or B. The Output Compare Flag A (OCF0A) or B is set when this event occurs.

What is setting? Does this means setting it to one? If yes, when does this get cleared/unset, so that we can set it again as the TCNT0 counter again reaches value in OCR0A/B?

Thanks
Bu

Bugaboo:
What is setting? Does this means setting it to one?

Yes.

If yes, when does this get cleared/unset, so that we can set it again as the TCNT0 counter again reaches value in OCR0A/B?

When you clear it or when the corresponding interrupt service routine is called.

When you clear it or when the corresponding interrupt service routine is called.

Great. That makes sense, but what is the point? As far as I understand, the interrupt is called as soon as the event has occurred. RIght? So, I am guessing, the bit is one for just one clock cycle (roughly speaking???). The only reason that I could think of is that such a flag could be used to check inside an ISR if another event (or multiple events) has occurred while ISR is doing its sh**.

Bugaboo:
That makes sense, but what is the point? As far as I understand, the interrupt is called as soon as the event has occurred. RIght?

Only if the interrupt is enabled.

Ok.. But does that mean -- if the interrupt is enabled, the flag is "one" for roughly 1 clock cycle (16 MHz?????)

Thanks
Bu

From your perspective (someone trying to write useful code for an AVR processor) it means the flag is never set when interrupts are enabled.

OK. But let me explain a bit what I am trying to do. I am trying to modify a piece of code by Nick Gammon which I found here Gammon Forum : Electronics : Microprocessors : Timers and counters . I am attaching the code here for your reference also. In this code, the bit TOV1 (similar to OCF0A/B, right?) is accessed and played with. I have extracted the lines of code where it is done.

// if just missed an overflow
  if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 256)
    overflowCopy++;

So the question is for how long TOV1 remains 1 till the overflow has occurred.

The complete code is here:

// Timer and Counter example
// Author: Nick Gammon
// Date: 17th January 2012

// Input: Pin D5

// these are checked for in the main program
volatile unsigned long timerCounts;
volatile boolean counterReady;

// internal to counting routine
unsigned long overflowCount;
unsigned int timerTicks;
unsigned int timerPeriod;

void startCounting (unsigned int ms) 
  {
  counterReady = false;         // time not up yet
  timerPeriod = ms;             // how many 1 mS counts to do
  timerTicks = 0;               // reset interrupt counter
  overflowCount = 0;            // no overflows yet

  // reset Timer 1 and Timer 2
  TCCR1A = 0;             
  TCCR1B = 0;              
  TCCR2A = 0;
  TCCR2B = 0;

  // Timer 1 - counts events on pin D5
  TIMSK1 = bit (TOIE1);   // interrupt on Timer 1 overflow

  // Timer 2 - gives us our 1 mS counting interval
  // 16 MHz clock (62.5 nS per tick) - prescaled by 128
  //  counter increments every 8 µS. 
  // So we count 125 of them, giving exactly 1000 µS (1 mS)
  TCCR2A = bit (WGM21) ;   // CTC mode
  OCR2A  = 124;            // count up to 125  (zero relative!!!!)

  // Timer 2 - interrupt on match (ie. every 1 mS)
  TIMSK2 = bit (OCIE2A);   // enable Timer2 Interrupt

  TCNT1 = 0;      // Both counters to zero
  TCNT2 = 0;     

  // Reset prescalers
  GTCCR = bit (PSRASY);        // reset prescaler now
  // start Timer 2
  TCCR2B =  bit (CS20) | bit (CS22) ;  // prescaler of 128
  // start Timer 1
  // External clock source on T1 pin (D5). Clock on rising edge.
  TCCR1B =  bit (CS10) | bit (CS11) | bit (CS12);
  }  // end of startCounting

ISR (TIMER1_OVF_vect)
  {
  ++overflowCount;               // count number of Counter1 overflows  
  }  // end of TIMER1_OVF_vect


//******************************************************************
//  Timer2 Interrupt Service is invoked by hardware Timer 2 every 1ms = 1000 Hz
//  16Mhz / 128 / 125 = 1000 Hz

ISR (TIMER2_COMPA_vect) 
  {
  // grab counter value before it changes any more
  unsigned int timer1CounterValue;
  timer1CounterValue = TCNT1;  // see datasheet, page 117 (accessing 16-bit registers)
  unsigned long overflowCopy = overflowCount;

  // see if we have reached timing period
  if (++timerTicks < timerPeriod) 
    return;  // not yet

 // if just missed an overflow
  if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 256)
    overflowCopy++;

  // end of gate time, measurement ready

  TCCR1A = 0;    // stop timer 1
  TCCR1B = 0;    

  TCCR2A = 0;    // stop timer 2
  TCCR2B = 0;    

  TIMSK1 = 0;    // disable Timer1 Interrupt
  TIMSK2 = 0;    // disable Timer2 Interrupt
    
  // calculate total count
  timerCounts = (overflowCopy << 16) + timer1CounterValue;  // each overflow is 65536 more
  counterReady = true;              // set global flag for end count period
  }  // end of TIMER2_COMPA_vect

void setup () 
  {
  Serial.begin(115200);       
  Serial.println("Frequency Counter");
  } // end of setup

void loop () 
  {
  // stop Timer 0 interrupts from throwing the count out
  byte oldTCCR0A = TCCR0A;
  byte oldTCCR0B = TCCR0B;
  TCCR0A = 0;    // stop timer 0
  TCCR0B = 0;    
  
  startCounting (500);  // how many mS to count for

  while (!counterReady) 
     { }  // loop until count over

  // adjust counts by counting interval to give frequency in Hz
  float frq = (timerCounts *  1000.0) / timerPeriod;

  Serial.print ("Frequency: ");
  Serial.print ((unsigned long) frq);
  Serial.println (" Hz.");
  
  // restart timer 0
  TCCR0A = oldTCCR0A;
  TCCR0B = oldTCCR0B;
  
  // let serial stuff finish
  delay(200);
  }   // end of loop

Overflow means a result greater than the maximum value. In the case of a timer there will be no overflow when using the output compare functions as the timer counter chain will never wrap around ie set the nth bit +1 to 1. You can view the overflow flag as the 17th bit in a 16 bit counter or the 9th bit in an 8 bit counter. So to restate this you will not overflow unless your timer's mode is free run, in which case an overflow occurs every time the counter wraps around to 0.
see Smart | Connected | Secure | Microchip Technology

Bugaboo:
OK. But let me explain a bit what I am trying to do. I am trying to modify a piece of code by Nick Gammon which I found here Gammon Forum : Electronics : Microprocessors : Timers and counters

So you have essentially cross-posted. Which means you have wasted my time and possibly @ajofscott's time.

Replies go here... Pulse Counting and question about TOV1 - Programming Questions - Arduino Forum

Thread locked.