I am trying to create a sinusoidal wave segmented in 64 Frame, each holding a Duty cycle precisely varying from 0x00 to 0xff. This is for an Arduino Nano.
I have trouble understanding the following:
- Which Wave Generator Mode and Matches will cause an interrupt at the constant Frame as well as help do a phase correct/fast PWM for the varying Duty Cycle?
- How to tell which pin to use? I guess OC0B means Pin5 according to Pin Mapping but I'm not sure I truly understand.
This is what I have for the moment.
const byte waveFrame = 126;
const byte wave[] = {
0x80,0x98,/*a total of 64 values*/,0x67
};
byte frame = 0;
void setup(){
pinMode(3,OUTPUT);
noInterrupts();
TCCR0A = (1<< WGM00) // Phase correct PWM with top as OCR0A
| (1<<COM0A0) // Toggle OC0A on match
| (1<<COM0B1); // Clear/Set OC0B on Compare Match when up/down-counting.
TCCR0B = (1<< CS01); // Prescaler of 8 (16MHz/8= 2MHz)
TCNT0 = 0; // Counter restarted
OCR0A = waveFrame; // Constant frequency/64 for each frame
OCR0B = 0; // Duty Cycle
interrupts();
}
ISR(TIMER2_COMPA_vect){
OCR0B = wave[frame & 0x3f]; //Rolls around
frame++;
}
Thank you! I've lost a little bit of hair on this.