Electronic – the difference between TCCR1A and TCCR1B

atmegadatasheetregistertimer

I had an error in the configuration of PWM for the atmega8 because I didn't set my register properly. But I thought that Timer 1 is timer 1, no matter is TCCR1A or TCCR1B. Why can I set all the timer 1 using only TCCR1A like so:

TCCR1A  = ((1 << COM1A1) | (1 << COM1A0) (1 << CS11) | (1 << WGM13) );

Datasheet

EDIT : I was confused about the fact that TCCR1A and TCCR1B are different registers which are set for the same timer.

Best Answer

First of all I think you missed a | symbol:

TCCR1A  = ((1 << COM1A1) | (1 << COM1A0) (1 << CS11) | (1 << WGM13) );

TCCR1A  = ((1 << COM1A1) | (1 << COM1A0) | (1 << CS11) | (1 << WGM13) );

Second:

  • Bits COM1A1 and COM1A0 are in register TCCR1A
  • Bits CS11 and WGM13 are in TCCR1B

TCCR1A and TCCR1B are different registers, but they work on the same timer, timer1. They configure different behavior and are located in separate registers, simply because all the bits don't fit in a single byte.

Notice that COM1A1 is simply an alias for the number 7, so is COM1A0 alias for number 6, WGM13 is an alias for number 4 and CS11 for 1. It is up to you, the user, to check if you are writing the correct bits in the correct registers. The compiler does not check this for you.

For example the following three lines will have the same resulting assembly code:

TCCR1A = ( 1 << COM1A1 );
TCCR1A = ( 1 << PB7 );
TCCR1A = ( 1 << ICNC1 );

All three lines will result in the following identical two assembly instructions:

ldi r24, 0x80
out 0x2F, r24

TCCR1(A|B) cannot be accessed as a single 16 bit register like TCNT1(H|L) can.

Related Topic