50 Hz interrupts (ftom int timer)

I make music player through sound chip AY-3-8910 (ZX Spectrum).

I found a code that a play is a digital sample. It works fine. But I could not in it to adjust the frequency of 50 Hz. It is additional then decreases. I commented out the code from the PWM.

I tried to use the library SimpleTime, works fine. But I have a problem getting music through the COM port from your computer. And then I’m afraid without interruption can not do (which may be artifacts).

:relaxed:

#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

#define SAMPLE_RATE 239


int speakerPin = 11;
volatile uint16_t sample;
byte lastSample;

// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) {

    if (sample) {

      sample = 0;
      
//      OCR2A = 255;
      
    }
    else {
     
      sample = 1;
//     OCR2A = 0; 

      
      
    }


    digitalWrite(speakerPin, sample);

}

void startPlayback()
{
    pinMode(speakerPin, OUTPUT);

    // Set up Timer 2 to do pulse width modulation on the speaker
    // pin.

    // Use internal clock (datasheet p.160)
    ASSR &= ~(_BV(EXCLK) | _BV(AS2));

    // Set fast PWM mode  (p.157)
//    TCCR2A |= _BV(WGM21) | _BV(WGM20);
//    TCCR2B &= ~_BV(WGM22);

    // Do non-inverting PWM on pin OC2A (p.155)
    // On the Arduino this is pin 11.
//    TCCR2A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
//    TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));

    // No prescaler (p.158)
    //TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);


    cli();

    // Set CTC mode (Clear Timer on Compare Match) (p.133)
    // Have to set OCR1A *after*, otherwise it gets reset to 0!
    TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
    TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));

    // No prescaler (p.134)
    TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);

    // Set the compare register (OCR1A).
    // OCR1A is a 16-bit register, so we have to do this with
    // interrupts disabled to be safe.
    OCR1A = F_CPU / SAMPLE_RATE;    // 16e6 / 8000 = 2000

    // Enable interrupt when TCNT1 == OCR1A (p.136)
    TIMSK1 |= _BV(OCIE1A);

    sei();
}


void setup()
{
    startPlayback();
}

void loop()
{
    while (true);
}

What is the frequency of your house electric system? It will be very dependable.