still pretty new to AVR programming, I don't really understand what's going wrong here, and I don't know really how to solve it...
The Concept:
Play a sine wav saved as a char array in PROGMEM through a 8bit DAC connected to PORTC pins on the arduino mega.
This Works:
//file just defines an array and a size
#include "sine440.h"
int i = 0;
void setup() {
DDRC = B11111111;
}
void loop() {
PORTC = pgm_read_byte(&sine[i++]);
if(i >= sine_size){
i = i - sine_size;
}
delayMicroseconds(60);
}
The above code produces a sine wav of about 440hz, exactly what I wanted. Now, I'd like to move this into an interrupt routine, as follows:
This Doesn't Work
#include "sine440.h"
int i = 0;
void setup() {
DDRC = B11111111;
TCCR5B = (1 << WGM52) | (1 << CS50 );
OCR5A = 16000000/16000;
TIMSK5 = (1 << OCIE5A);
}
ISR(TIMER5_COMPA_vect){
PORTC = pgm_read_byte(&sine[i++]);
if(i >= sine_size){
i = i - sine_size;
}
}
void loop() {
}
The above code produces a sine wave of 1721hz. I know that this means it's iterating through the array too fast, but I don't know how to change it. In order for it to produce 440hz, it needs to be preforming 16000 iterations per second, which is what I believe OCR5A = 16000000/16000;
does. BUT it doesn't appear that changing the value of OCR5A does anything at all. Giving it the max value of 65,535 still produces 1721hz sine wav...
So what am I doing wrong here? How do I change the speed of this timer interrupt?
TCCR5A = 0
explicitly, maybe arduino has WGM51 or WGM50 set at startup for some reason. – BrettAM Jun 11 at 23:34