Cannot set up Timer1 with CTC properly

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[Sorry don't see any way to move this now that "megaAVR" is where it should be, I'll just repost there]

I'm trying to configure the 16-bit Timer1<!--break--> in ATmega328p and keep my needs simple with CTC and toggling the OC1A output pin each time it compares. After a compare the ISR would write the next value to delay before I need OC1A output to toggle again.
I'm using Arduino Pro Mini, also tried on Arduino Uno R3.

I set everything the specs say would be needed, but it wasn't right and when I read back the registers after setting them, it's not quite what I wrote.

Could my problem be the hardware registers not allowing modes to be set in that combination? I don't think Arduino could magically be using or changing those but I admit I have limited info on the inner workings. I see how it's used in Servo.h, so it must be available since I'm not using that.
 

This is my code in Arduino IDE, see comments for what is incorrect after setting:

  // clk is 16MHz
  // CS12:0   = 2 (clk/8 prescaler)
  // COM1A1:0 = 1 (toggle PB1 on each compare)
  // WGM13:0  = 4 (CTC on OCR1A)
  uint8_t oldSREG = SREG;
  cli();
  TCCR1A = 0;
  TCCR1A = _BV(COM1A0);        //0x40  but reads back 0x41
  TCCR1B = _BV(WGM12) | _BV(CS11);    //0x0a  but reads back 0x03
  OCR1A = usToTicks(1000);       //0x07d0, reads correct only if first TCCR1A=0 and then try to set TCCR1A to what I need
  SREG = oldSREG;
  pinMode(9,OUTPUT);   // output pin with pulse train (OC1A)

Problems

  1. It's not doing the documented CTC mode if TCCR1 registers can't even hold what I write to them.
  2. The OCR1A pin is NOT enabled even though COM1A1:0 is 01. Instead I can still control that pin writing to PORTB (because the pin is PB1, arduino pin 9).

 

Seems like too many things just do not work the way they are documented in the spec sheet? I downloaded "ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061B.pdf" to get the details that don't work.

Last Edited: Sat. Aug 20, 2022 - 01:21 PM