Skip to main content

My requirement of 125Khz125 kHz is satisfied, but I am just curious to know why the sketch not working as it should ...

According to my measurements, it is. Using this slightly modified version of your code:

// A sketch that creates an 8MHz8 MHz, 50% duty cycle PWM and a 250KHz250 kHz,
// 6bit6-bit resolution PWM with varying duty cycle (changes every 5μs5 µs
// or about every period.

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  pinMode(3, OUTPUT); // outputOutput pin for OCR2B
  pinMode(5, OUTPUT); // outputOutput pin for OCR0B

  // Set up the 250KHz250 kHz output
  TCCR2A = bit(COM2A1) | bit(COM2B1) | bit(WGM21) | bit(WGM20);
  TCCR2B = bit(WGM22) | bit(CS20);
  OCR2A = 63;
  OCR2B = 31;

  // Set up the 8MHz8 MHz output
  TCCR0A = bit(COM0A1) | bit(COM0B1) | bit(WGM01) | bit(WGM00);
  TCCR0B = bit(WGM02) | bit(CS00);
  OCR0A = 1;
  OCR0B = 0;
 }

I get the predicted 8 MHz output on pin 5:

8 MHz scope output

And the 250 kHz output on pin 3:

250 kHz scope output

My requirement of 125Khz is satisfied, but just curious to know why the sketch not working as it should ...

According to my measurements, it is. Using this slightly modified version of your code:

// A sketch that creates an 8MHz, 50% duty cycle PWM and a 250KHz,
// 6bit resolution PWM with varying duty cycle (changes every 5μs
// or about every period.

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
 pinMode(3, OUTPUT); // output pin for OCR2B
 pinMode(5, OUTPUT); // output pin for OCR0B

// Set up the 250KHz output
 TCCR2A = bit(COM2A1) | bit(COM2B1) | bit(WGM21) | bit(WGM20);
 TCCR2B = bit(WGM22) | bit(CS20);
 OCR2A = 63;
 OCR2B = 31;

// Set up the 8MHz output
 TCCR0A = bit(COM0A1) | bit(COM0B1) | bit(WGM01) | bit(WGM00);
 TCCR0B = bit(WGM02) | bit(CS00);
 OCR0A = 1;
 OCR0B = 0;
 }

I get the predicted 8 MHz output on pin 5:

8 MHz scope output

And the 250 kHz output on pin 3:

250 kHz scope output

My requirement of 125 kHz is satisfied, but I am just curious to know why the sketch not working as it should.

According to my measurements, it is. Using this slightly modified version of your code:

// A sketch that creates an 8 MHz, 50% duty cycle PWM and a 250 kHz,
// 6-bit resolution PWM with varying duty cycle (changes every 5 µs
// or about every period.

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  pinMode(3, OUTPUT); // Output pin for OCR2B
  pinMode(5, OUTPUT); // Output pin for OCR0B

  // Set up the 250 kHz output
  TCCR2A = bit(COM2A1) | bit(COM2B1) | bit(WGM21) | bit(WGM20);
  TCCR2B = bit(WGM22) | bit(CS20);
  OCR2A = 63;
  OCR2B = 31;

  // Set up the 8 MHz output
  TCCR0A = bit(COM0A1) | bit(COM0B1) | bit(WGM01) | bit(WGM00);
  TCCR0B = bit(WGM02) | bit(CS00);
  OCR0A = 1;
  OCR0B = 0;
}

I get the predicted 8 MHz output on pin 5:

8 MHz scope output

And the 250 kHz output on pin 3:

250 kHz scope output

Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

My requirement of 125Khz is satisfied, but just curious to know why the sketch not working as it should ...

According to my measurements, it is. Using this slightly modified version of your code:

// A sketch that creates an 8MHz, 50% duty cycle PWM and a 250KHz,
// 6bit resolution PWM with varying duty cycle (changes every 5μs
// or about every period.

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
 pinMode(3, OUTPUT); // output pin for OCR2B
 pinMode(5, OUTPUT); // output pin for OCR0B

// Set up the 250KHz output
 TCCR2A = bit(COM2A1) | bit(COM2B1) | bit(WGM21) | bit(WGM20);
 TCCR2B = bit(WGM22) | bit(CS20);
 OCR2A = 63;
 OCR2B = 31;

// Set up the 8MHz output
 TCCR0A = bit(COM0A1) | bit(COM0B1) | bit(WGM01) | bit(WGM00);
 TCCR0B = bit(WGM02) | bit(CS00);
 OCR0A = 1;
 OCR0B = 0;
 }

I get the predicted 8 MHz output on pin 5:

8 MHz scope output

And the 250 kHz output on pin 3:

250 kHz scope output