Help with interrupts code

Hi, I am trying to generate a sine wave with the arduino, and a digital-to-analog converter. I have most of the code and hardware all setup, but I want to make this device have variable frequency, but I am not sure if this code allows for this.

Essentially, I want to be able to alter the value of TCNT2 in the ISR(TIMER2_OVF_vect) function. I want to be able to pass in a value for it from elsewhere, but this does not seem like the most straightforward process.

Is there any way that I could do this?

/*
 Copyright 2007 Richard Cappels used with his permission
 www.projects.cappels.org
 This assumes an R/2R DAC (or other DAC) connected to PORTD, and
 Arduino 16mhz clock
 The frequency measured at the output of the DAC is 1.007 kHz.
 ported to arduino by hotcarrier
 output to pd7-pd2 with 6 bits- needs high order pd7 to work
 correctly
 */

#include <avr/interrupt.h>
#include <stdlib.h>
char sinetable [32];
int  i ;
int b = 0;
void ioinit (void)
{
  //Initialize output ports
  PORTD = B11111111;
  DDRD  = B11111111;

}

void timer_setup(){
  TCCR2A = 0;
  TCNT2= 0;    //455 outputs 1.007khz
  TCCR2B = B00000010;
  //Timer2 Overflow Interrupt Enable
  TIMSK2 = 1<<TOIE2;
}
void setup(){           
  ioinit();
  arraysetup();
  cli();
  timer_setup();
  i = 0;
  sei();

}


ISR(TIMER2_OVF_vect) {
  PORTD=(sinetable[i++]);
  TCNT2=455;
  if(i==32){
    i=0;
  }

}
void arraysetup(void){
  sinetable[0]=127;  // Put 32 step 8 bit sine table into array.
  sinetable[1]=152;
  sinetable[2]=176;
  sinetable[3]=198;
  sinetable[4]=217;
  sinetable[5]=233;
  sinetable[6]=245;
  sinetable[7]=252;
  sinetable[8]=254;
  sinetable[9]=252;
  sinetable[10]=245;
  sinetable[11]=233;
  sinetable[12]=217;
  sinetable[13]=198;
  sinetable[14]=176;
  sinetable[15]=152;
  sinetable[16]=128;
  sinetable[17]=103;
  sinetable[18]=79;
  sinetable[19]=57;
  sinetable[20]=38;
  sinetable[21]=22;
  sinetable[22]=10;
  sinetable[23]=3;
  sinetable[24]=0;
  sinetable[25]=3;
  sinetable[26]=10;
  sinetable[27]=22;
  sinetable[28]=38;
  sinetable[29]=57;
  sinetable[30]=79;
  sinetable[31]=103;
}
void loop()
{
  
  while (1)
  {

  }

}

To pass something into the ISR(TIMER2_OVF_vect) function , you declare it as a volatile global variable. You can modify this in the loop:

volatile int myInt ;
int  i ;

ISR(TIMER2_OVF_vect) {
  PORTD=(sinetable[i++]);
  TCNT2=myInt ;
  if(i==32){
    i=0;
  }
}

void loop() {
  if ( condition ) {
    myInt = 455 ;
  }
}

6v6gt:
To pass something into the ISR(TIMER2_OVF_vect) function , you declare it as a volatile global variable. You can modify this in the loop:

volatile int myInt ;

int  i ;

ISR(TIMER2_OVF_vect) {
  PORTD=(sinetable[i++]);
  TCNT2=myInt ;
  if(i==32){
    i=0;
  }
}

void loop() {
  if ( condition ) {
    myInt = 455 ;
  }
}

You are a life saver, thank you so much!!

Your timer setup is a little peculiar.

Timer 2 is an 8 bit timer so when you set TCNT2 = 455, you are really creating a value of TCNT2=199 when the byte variable rollover. So, setting TCNT2 to 455 is the same as setting TCNT2 to 199.

The overflow occurs at a count of 256 so you are seeing 56 counts(with prescaler eight) until each execution of the overflow ISR. Mathematically that should be giving a frequency of 1116Hz, but with the interrupt overhead the 1KHz of the comments sounds reasonable.

You are correct that changing the preset value of TCNT2 will vary the frequency by changing the overflow rate.

What are you trying to achieve? How do you want to vary the frequency?

6v6gt:
To pass something into the ISR(TIMER2_OVF_vect) function , you declare it as a volatile global variable. You can modify this in the loop:

volatile int myInt ;

int  i ;

ISR(TIMER2_OVF_vect) {
  PORTD=(sinetable[i++]);
  TCNT2=myInt ;
  if(i==32){
    i=0;
  }
}

void loop() {
  if ( condition ) {
    myInt = 455 ;
  }
}

Always disable interrupts before writing to a variable that is read by an interrupt, or else you can get glitches.