Unable to write to 16bit register

Dear all,

with this simple code

void setup(){
  Serial.begin(57600);
  ICR1H = 35;
  ICR1L = 53;
  Serial.print(ICR1L); Serial.print(F("\t"));
  Serial.print(ICR1H); Serial.print(F("\n"));
}

I get this result to my Serial monitor

53 35

On the other hand when I try this

void setup(){
  Serial.begin(57600);
  OCR1AH = 81;
  OCR1AL = 18;
  Serial.print(OCRA1L); Serial.print(F("\t"));
  Serial.print(OCRA1H); Serial.print(F("\n"));
}

I get this result to my Serial monitor

18 0

When I try to read OCR1BH I always get 0 too
Output Compare Registers A/B shouldn't suppose to be a r/w register?
Writing HIGH byte first and reading it last shouldn't be the right sequence?

I 'll be glad for any help tnxs

             OCR1AH = 81;
Serial.print(OCRA1H);

             OCR1AL = 18;
Serial.print(OCRA1L);

See any difference?

nop
018 is what I get now

Spelling is important

no I don't see any difference
I still get zero for HIGH byte

Now do you see...

OCR1AH
OCRA1H

OCR1AL
OCRA1L

Writing HIGH byte first and reading it last shouldn't be the right sequence?

Yes, that’s absolutely the correct sequence. The latch is on the high byte. Why do you say it shouldn’t be?

Also some timer registers have a 16 bit combined register name. The compiler generates the correct access sequence for you if you use them.

Clear the timer register TCCR1A before writing (that is always the first thing I do when configuring the timers). I’m not quite sure why but both of these approaches work if you do.

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
unsigned int i,j;
TCCR1A=0; 
OCR1AH = 23;
OCR1AL = 10;
Serial.println(OCR1A);
i=23*256+10;
OCR1A = i;
j=OCR1A;
Serial.println(j);
}

void loop() {
  // put your main code here, to run repeatedly:

}

pcbbc:
Now do you see...

OCR1AH

OCRA1H

OCR1AL
OCRA1L



Yes, that’s absolutely the correct sequence. The latch is on the high byte. Why do you say it shouldn’t be?

Also some timer registers have a 16 bit combined register name. The compiler generates the correct access sequence for you if you use them.

when there is a misspelling an error like this arise

exit status 1
'OCRA1H' was not declared in this scope

I also get zeros for HIGH byte when I try to use OCR1B

jremington:
Clear the timer register TCCR1A before writing (that is always the first thing I do when configuring the timers). I'm not quite sure why but both of these approaches work if you do.

void setup() {

// put your setup code here, to run once:
Serial.begin(9600);
unsigned int i,j;
TCCR1A=0;
OCR1AH = 23;
OCR1AL = 10;
Serial.println(OCR1A);
i=23*256+10;
OCR1A = i;
j=OCR1A;
Serial.println(j);
}

void loop() {
  // put your main code here, to run repeatedly:

}

OK this works fine, but why WGM[3:0] should be cleared to be able to write to OCR?

I suspect that if you don't clear TCCR1A, a timer interrupt interferes with the read or write operation. NOTHING may intervene (the data sheet specifically warns about that).

jremington:
I suspect that if you don’t clear TCCR1A, a timer interrupt interferes with the read or write operation. NOTHING may intervene (the data sheet specifically warns about that).

I have read about it and I also tried this with no results

cli();
OCR1AH = 23;
OCR1AL = 10;
sei();

I even tried to use PRR|=(1<<PRTIM1); to shutdown the module completely but only the WGM seems to work.

I suppose we never get what we want but sometimes we just get a code that runs…
thnxs man

In what Mode are you operating the timer?

I think that if you are in one of the modes with a 255 TOP, you can not set the high register.

during test as default which means mode 1, WGM1 = B0001
but the goal is to run in fast PWM (mode 15, WGM1 = B1111)

Mode 1 is PWM to 255.

Since the high register values are not relevant, something in the compiler or hardware is blocking attempts to set it or are clearing a previously set value to 0.

I use IDE 1.6.13; pretty stable and that's why I didn't upgrade it

something in the compiler

It seems unlikely that the compiler (or the compiler author(s)) would care about timer modes, but a hardware restriction certainly seems possible.

And, of course, it does not make any sense to have other than zero in the high bytes of OCR1A/B, if TOP=255.

from datasheet

Combined with the WGM1[3:2] bits found in the TCCR1B Register, these bits control the counting sequence of the counter, [b]the source for maximum (TOP) counter value[/b], and what type of waveform generation to be used.

So this explains why HIGH byte can be chopped in any mode other than Mode-0