2

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.

B7th
  • 167
  • 8

1 Answers1

2

pinMode(3,OUTPUT);

Pin 3 on the Nano is OC2B. It's PWM is controlled by Timer 2. You want to use pin 5 (OC0B), since you are using Timer 0. Or switch to Timer 2.

Phase correct PWM with top as OCR0A

Note that this is the description of mode 5. If you want this mode, you have to set both the bit WGM00 in TCCR0A (which you did) and the bit WGM02 in TCCR0B (which you didn't). Here you have set mode 1: phase correct PWM with top = 0xff.

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81