ISR and Timer1 code does not work in my project, but works in blank sketch

Greetings. I’m having a problem with my project and so far I have not been able to figure out what’s wrong with it.
I have a tone generator implemented in my project and it’s not working.
The function takes a pre-calculated value from 0 to 65535 and writes it to the OCR1A register.
I have followed online Timer1 guides diligently, and I did get it to work, but only if I copied the Timer1 functions from my main project into a blank sketch. I do not get compilation errors in either source file. I’m using an Arduino Pro Mini Atmega328P, 3.3V, 8MHz.

EDIT: I found out what was the problem (EDIT2: no i didnt at that point). The code in my project did not set a value to OCR1A upon first initialization. When I hard-coded a fixed value (of 65535) into it, it started working. But now I found out that I can not change the value of the register by passing a new computed value into it through a variable. Does anybody know why I’m not able to edit the OCR1A register?

EDIT2: Found out the true culprit of my problem. If a timer is disabled by using PRR |= (1 << PRTIMx);, All writes to that timer registers vanish into the ether. Before updating OCR1A register value, switch off ISR hook by using TIMSK1 &= ~(1 << OCIE1A);. If code still does funny business, call PRR &= ~(1 << PRTIMx); before disabling ISR hooks to switch ON the relevant timer module.

I hope you’ll forgive me if I currently skip sharing my project file, because it’s over 2000 lines of code.
This is the code which works fine when copied into a blank sketch. Thanks for any and all help you’re able to provide.

uint16_t toneGenRegVal = 20000;

void setup() {
  pinMode(13, OUTPUT);  // Internal LED on pin 13
  pinMode(10, OUTPUT);  // Speaker PIN
  TCCR1A = 0;  // Clear register
  TCCR1B = 0;  // Clear register
  TCNT1  = 0;  // Clear register
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS10);    // 1 prescaler 
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt

void loop() {
    PRR |= (1 << PRTIM1);       // Disable Timer1 module
    TIMSK1 &= ~(1 << OCIE1A);   // Disable timer compare interrupt. For check only, not necessary.
    PORTB &= ~(1 << 2);         // Write LOW to pin to prevent digital noise

    PORTB &= ~(1 << 5);         // Switch OFF internal LED
    delay(500);                 // Wait for 500 milliseconds

    PRR &= ~(1 << PRTIM1);      // Enable Timer1 module
    TIMSK1 |= (1 << OCIE1A);    // Enable timer compare interrupt. For check only, not necessary.
    OCR1A = toneGenRegVal;      // Set compare match register value   
    PORTB |= (1 << 5);          // Switch ON internal LED
    delay(500);                 // Wait for 500 milliseconds

    PINB |= (1 << 2);

That is… uhm… a strange question. You have a sketch with a problem, but you only give a test sketch that has no problem.

Pin 10 is speaker, toggled in ISR. I think that only writing a ‘1’ will work. PINB = ( 1 << 2 ) ;
Pin 13 is system led.

Using the power register to turn off a timer is something I have not seen before. You could just clear TCCR1A and TCCR1B.

There might be other libraries that use Timer1. There are so many libraries that use Timer1. Which libraries do you use ?

Why don’t you use the normal Arduino tone() function ?

Yeah, you’re right, it is a weird method, which is why I ditched it and now the code switches ISR call by switching TIMSK1 on or off.

By the way, I managed to solve my problem.

For some odd reason after I had removed PRR register from my code, it kept switching the Timer1 module off.
So I just re-populated my code at critical places with PRR &= ~(1 << PRTIM1); and now everything works.

I had to implement my own tone function, because I need around 0.01 precision in all frequencies from 130Hz to 988Hz and any Tone or Timer1 libraries I have come across just don’t have that precision.
I’m glad to report that my labor has born fruit and my tone function is a success.

Very accurate ? but the ISR can be delayed by other interrupts. Is the crystal of the Arduino that accurate ? Your Arduino board might even have a resonator.
What about toneAC ? teckel12 / Arduino toneAC / wiki / Home — Bitbucket