Strange problem of repeated prints in setup

Maybe a pair of fresh eyes can help out find out what is going on in the code.

The program just writes a byte of data to part of port b and part of port d.
Connected to those 8 pins I have a R-2R resistor ladder for conversion to analog.
The data is such that the output out of the R-2R is a sine wave.

That all works when using a for loop to read the array into the pins.

What I am trying to is timer 2 output compare interrupt to read the data.

What happens is when I run the code is the program just keeps printing the message "setting up" which is part setup() routine.

It acts like the micro is forever getting reset but the reset line stays high.

Here is the code ...

#define SAMPLE_RATE 360000

void timerSetup(void);

int i;
byte note,flag=0, myarray[360];
unsigned int counter=0;


const unsigned int sinewave[] PROGMEM ={
	0x7f,0x81,0x83,0x86,0x88,0x8a,0x8c,0x8e,0x91,0x93,0x95,0x97,0x99,0x9c,0x9e,0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xaf,0xb1,0xb3,0xb5,0xb7,0xb9,0xbb,0xbd,0xbe,0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcb,0xcd,0xcf,0xd1,0xd2,0xd4,0xd6,0xd7,0xd9,0xda,0xdc,0xdd,0xdf,0xe0,0xe2,0xe3,0xe4,0xe6,0xe7,0xe8,0xe9,0xeb,0xec,0xed,0xee,0xef,0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf6,0xf7,0xf8,0xf8,0xf9,0xfa,0xfa,0xfb,0xfb,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfe,0xfd,0xfd,0xfd,0xfc,0xfc,0xfc,0xfb,0xfb,0xfa,0xfa,0xf9,0xf8,0xf8,0xf7,0xf6,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0,0xef,0xee,0xed,0xec,0xeb,0xea,0xe8,0xe7,0xe6,0xe5,0xe3,0xe2,0xe0,0xdf,0xdd,0xdc,0xda,0xd9,0xd7,0xd6,0xd4,0xd2,0xd1,0xcf,0xcd,0xcc,0xca,0xc8,0xc6,0xc4,0xc2,0xc1,0xbf,0xbd,0xbb,0xb9,0xb7,0xb5,0xb3,0xb1,0xaf,0xad,0xab,0xa9,0xa6,0xa4,0xa2,0xa0,0x9e,0x9c,0x9a,0x97,0x95,0x93,0x91,0x8f,0x8c,0x8a,0x88,0x86,0x84,0x81,0x7f,0x7d,0x7b,0x79,0x76,0x74,0x72,0x70,0x6e,0x6b,0x69,0x67,0x65,0x63,0x60,0x5e,0x5c,0x5a,0x58,0x56,0x54,0x52,0x50,0x4e,0x4c,0x4a,0x48,0x46,0x44,0x42,0x40,0x3e,0x3c,0x3a,0x38,0x36,0x35,0x33,0x31,0x2f,0x2e,0x2c,0x2a,0x29,0x27,0x25,0x24,0x22,0x21,0x1f,0x1e,0x1c,0x1b,0x1a,0x18,0x17,0x16,0x15,0x13,0x12,0x11,0x10,0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x09,0x08,0x07,0x06,0x06,0x05,0x04,0x04,0x03,0x03,0x02,0x02,0x02,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x02,0x02,0x02,0x03,0x03,0x04,0x04,0x05,0x05,0x06,0x07,0x08,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x16,0x17,0x18,0x19,0x1b,0x1c,0x1d,0x1f,0x20,0x22,0x23,0x25,0x27,0x28,0x2a,0x2b,0x2d,0x2f,0x31,0x32,0x34,0x36,0x38,0x3a,0x3b,0x3d,0x3f,0x41,0x43,0x45,0x47,0x49,0x4b,0x4d,0x4f,0x51,0x53,0x55,0x57,0x59,0x5c,0x5e,0x60,0x62,0x64,0x66,0x69,0x6b,0x6d,0x6f,0x71,0x74,0x76,0x78,0x7a,0x7c

};


//for sine wave we want to set up so this is call at 360 khz
ISR(TIMER2_COMPA_vect) {
  
             //note = myFile.read();
            note= pgm_read_byte (&(sinewave[counter])) ;
            PORTB = (note >> 6) & 0x03 ;
            PORTD = (note << 2) & 0xfc;
            myarray[counter]=note;
            counter++;
             
             //flag=1;
}


void timerSetup() {
    
    ASSR &= ~(_BV(EXCLK) | _BV(AS2));  //use internal clock
    
    TCCR2A |= _BV(WGM21);  // clear timer on compare
    TCCR2A &= ~( _BV(WGM20) | _BV(COM2A0) | _BV(COM2A1) ); //OCRA pin disconnected

    TCCR2B |= (_BV(CS20) | _BV(CS21));   //div by x prescaler
    TCCR2B &= ~( _BV(CS22) | _BV(CS22) | _BV(WGM22)); //reset these bits
    //TCCR2B &= ~(_BV(CS22) | _BV(CS21) | _BV(WGM22));
    
    OCR2A = F_CPU/SAMPLE_RATE;
    //OCR2A = 250;
     
    TIMSK2 |= _BV(OCIE2A);   //enable compare A match interrupt and
    TIMSK2 &= ~(_BV(OCIE2B) | _BV(TOIE2)); //disable these 
}
  
void setup()
{   
 
 interrupts(); //turn on interrupts  
 Serial.begin(9600);

 while (!Serial);       // wait for serial port to connect. Needed for native USB port only
 
 Serial.println("Setting up");
 
 noInterrupts(); 
 timerSetup();
 
 
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);

interrupts();
} //setup



void loop() {

/*confirm register values
  Serial.println("In the loop ");
  Serial.println("TCCR2A IS "); Serial.println(TCCR2A,HEX);
  Serial.println("TCCR2B IS ");Serial.println(TCCR2B,HEX); 
  Serial.println("OCR2A IS "); Serial.println(OCR2A ,HEX);
  Serial.println("TIMSK2 IS ");  Serial.println( TIMSK2 ,HEX);
*/
  
    if(counter > 359) {
      noInterrupts();  //turn off interrupts
      
      for (i=0;i<360;i++) 
        Serial.println(myarray[i],HEX);
    
      while(1);  
      
    } //if 
  
} //loop
 myarray[counter]=note;
            counter++;

?

I did that to make sure I was reading the data in the correct order and not skipping any data points. Once I read through the whole array I disable interrupts and print out what was read in to myarray[].

And what happens when you run off the end of your array?

If you look at loop() it checks the counter value. If counter is greater than 359 then interrupts are turned off.
counter is post incremented so after the last data point is read the count is 360.

I saw that already.

Well, if you're confident that's working for you, I'll leave you to carry on.

TheMemberFormerlyKnownAsAWOL:
I saw that already.

Well, if you're confident that's working for you, I'll leave you to carry on.

lol, ok, you seem to see something I dont see. Obviously your telling me I am
accessing past the end of the array, right ?

No, I'm not telling you that, I'm just saying that in my opinion, you're not taking adequate measures to prevent it.
YMMV

So I can place a couple of negative numbers at the end of the array then I can check for a negative number andr a count and use that information as a stop. Unless there is a better way.

Where are you writing to the array?
Where do you think the code to stop you writing past the end of the array should be?

Also, counter should be qualified volatile

It is all done in the ISR. So I would say there.

Make it so.

Seems really ambitious to have a 16MHz MCU do this at 360kHz (an interrupt every 2.8uS).

Anyway, try adding this to the ISR to ensure you stay within the limits of the array:

.
.
.    myarray[counter]=note;
    if( counter < 359 )    //add this
        counter++;

Hah! I miscounted the zeroes, and had it "only" at 36kHz

It is working now.
Not at 360kHz, prob won't at that rate.

Thanks for your help "TheMemberFormalyKnownAsAWOL"
You've been a great help. I learned something today. karma++

modified code in ISR

ISR(TIMER2_COMPA_vect) {
  
             //note = myFile.read();
          note= pgm_read_byte (&(sinewave[counter])) ;
            
          if (note != 0xe1) {
               PORTB = (note >> 6) & 0x03 ;
               PORTD = (note << 2) & 0xfc;
               myarray[counter]=note;
               counter++;
             
           }
           else
              flag=1;
}

modified code in loop()

if(flag) {
          TIMSK2 &= ~(_BV(OCIE2A));   //turn off interrupts
          Serial.println("flag");
          for (i=0;i<360;i++) 
              Serial.println(myarray[i],HEX);
    
      while(1);