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.

4
  • 1
    Are you sure your attiny runs at 16Mhz? The output pin is pin OC0B, which is pin 6 on the ATTiny45. Not sure why you are using the pin mapping of the ATMega644P. Please tell us which processor you are actually using, because mentions three.
    – Gerben
    Jul 30, 2020 at 9:15
  • Thanks Gerben, I removed the comment regarding the ATTiny as it was not relevant to the issue here described. I'm using the Arduino Nano's ATMega indeed.
    – B7th
    Jul 30, 2020 at 9:27
  • 1
    OC0B is on arduino pin 5. It seems you code also enables output on OC0A, which is Arduino pin 6, giving you a fixed 50% percent duty cycles. Which is quite useful when you are debugging things, and have it connected to a oscilloscope, or logic analyzer . EDIT Disregard this last part if you actually want to use waveform mode 1.
    – Gerben
    Jul 30, 2020 at 9:42
  • 1
    You'd want Waveform Generation Mode 5. This is PWM, Phase Correct, but with TOP being the value of OCR0A instead of 255. For mode 5 you need to set both the WGM00 and WGM02 bits. Note that the WGM02 bit is in the TCCR0B register
    – Gerben
    Jul 30, 2020 at 9:48

1 Answer 1

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.

1
  • 1
    Not sure why I didn't validate it then. Thank you! That helped quite a fair bit!
    – B7th
    Aug 10, 2020 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.