Timer Learning help

any ideas what these lines means, translation from aliens to english welcome.

  // Set up timer 1.
  // Prescaler = 1, phase correct PWM mode, TOP = ICR1A
/* TCCR1A = (1 << COM1A1) | (1 << WGM11);
  TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10);    // CTC mode, prescaler = 1
  TCCR1C = 0;
  OCR1AH = (TIMER1_TOP/2 >> 8);
  OCR1AL = (TIMER1_TOP/2 & 0xFF);
  ICR1H = (TIMER1_TOP >> 8);
  ICR1L = (TIMER1_TOP & 0xFF);
  TCNT1H = 0;
  TCNT1L = 0;
  TIFR1 = 0x07;      // clear any pending interrupt
  TIMSK1 = (1 << TOIE1); */

They are configuring timer1 by writing to the timer control registers (this is how the timer is controlled - Arduino provides wrappers to get tones and PWM, but this is the kind of thing they do under the hood).

Looks like they're setting the WGM that uses ICR1 as TOP, and setting the output compare at half of TOP, so it looks like 50% duty cycle... And the prescaler is set to 8x instead of the usual 64x

Looks like it also enables the timer overflow interrupt, so an ISR should be firing when the timer hits TOP... you should find an ISR elsewhere in that code.

The function of those registers is explained in detail in the Timer1 chapter of the datasheet - my interpretation of them was from memory, too ;-)

Or at least that's what it would do if the whole thing wasn't commented out...

Consult the Holy Datasheet section 20 for deeper understanding.

ted:
any ideas what these lines means, translation from aliens to english welcome.

  // Set up timer 1.

// Prescaler = 1, phase correct PWM mode, TOP = ICR1A
/* TCCR1A = (1 << COM1A1) | (1 << WGM11);
  TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10);    // CTC mode, prescaler = 1
  TCCR1C = 0;
  OCR1AH = (TIMER1_TOP/2 >> 8);
  OCR1AL = (TIMER1_TOP/2 & 0xFF);
  ICR1H = (TIMER1_TOP >> 8);
  ICR1L = (TIMER1_TOP & 0xFF);
  TCNT1H = 0;
  TCNT1L = 0;
  TIFR1 = 0x07;      // clear any pending interrupt
  TIMSK1 = (1 << TOIE1); */

Ok. You need to understand the C bit-fiddling operators. What we are seeing here is some quite standard C idioms for fooling about with bits.

Take

TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10);

WGM12 is a constant containing a bit location. Let’s say - 5 (I don’t know what it might be). Bit 5 of TCCR1B relates to WGM12.
Likewise, WGM13 and CS10 contain (say) 3 and 2.

The operator << is the bit shifting operator. So (1<<WGM12) is the value 0b000000001 bitshifted by 5 - 0b00100000. Likewise, (1 << WGM13) and (1 << CS10) might evaluate to 0b00001000 and 0b00000010.

The operator | is the bitwise or operator. ‘oring’ two numbers together results in a value where each bit is true if either corresponding bit is true. So, 0b00100000 | 0b00001000 | 0b00000100 is 0b00101100, which would be the bit pattern that needs to be put into TCCR1B. Note that if WGM12 (etc) is a constant, this expression is evaluated at compile-time: it’s no slower than saying TCCR1B = 0x2C. But by using symbolic constants, you don’t screw up the bit pattern in code and a person reading the code can understand what’s going on.

There are other things here that a C programmer will instantly recognise. (foo & 0xFF), for instance, means “use only the low byte of foo”, and (foo >> [nobbc]8)[/nobbc] means “use the high byte of foo” - assuming that foo is an unsigned 16-bit quantity.

This:

  OCR1AH = (TIMER1_TOP/2 >> 8);
  OCR1AL = (TIMER1_TOP/2 & 0xFF);
  ICR1H = (TIMER1_TOP >> 8);
  ICR1L = (TIMER1_TOP & 0xFF);

Tells me that OCR1A and ICR1 are a pair of values with a high and a low byte. TIMER1_TOP goes into ICR1, and TIMER1_TOP/1 into OCR1A. Although the division by two could have been done with bitshifting, the person who wrote this is saying to me that OCR1A and ICR1 are numeric quantities, and that OCR1A needs to be numerically half ICR1.

Other common idioms are

foo |= (1<<bar); // set bit bar of foo
foo &= ~(1<<bar); // clear bit bar of foo
foo ^= (1<<bar); // toggle bit bar of foo

See:

http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/