It has to be both. You re using two PWM channels from one timer and one PWM channel from the other so BOTH have to be running at 10 kHz.
// Look Up table of a single sine period divied up into 256 values.
// Max PWM value is 99
const uint8_t sine256[256] PROGMEM =
{
50, 51, 52, 53, 54, 56, 57, 58,
59, 60, 62, 63, 64, 65, 66, 67,
69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84,
85, 85, 86, 87, 88, 89, 89, 90,
91, 91, 92, 93, 93, 94, 94, 95,
95, 96, 96, 97, 97, 97, 98, 98,
98, 98, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 98, 98,
98, 98, 97, 97, 97, 96, 96, 96,
95, 95, 94, 94, 93, 92, 92, 91,
90, 90, 89, 88, 87, 87, 86, 85,
84, 83, 82, 81, 81, 80, 79, 78,
77, 76, 75, 73, 72, 71, 70, 69,
68, 67, 66, 65, 63, 62, 61, 60,
59, 57, 56, 55, 54, 53, 51, 50,
49, 48, 46, 45, 44, 43, 42, 40,
39, 38, 37, 36, 34, 33, 32, 31,
30, 29, 28, 27, 26, 24, 23, 22,
21, 20, 19, 18, 18, 17, 16, 15,
14, 13, 12, 12, 11, 10, 9, 9,
8, 7, 7, 6, 5, 5, 4, 4,
3, 3, 3, 2, 2, 2, 1, 1,
1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 2, 2, 2, 3, 3, 4,
4, 5, 5, 6, 6, 7, 8, 8,
9, 10, 10, 11, 12, 13, 14, 14,
15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30,
32, 33, 34, 35, 36, 37, 39, 40,
41, 42, 43, 45, 46, 47, 48, 50
};
// Output pins on UNO or Nano
int PWM1 = 3; // Phase 1 = OC2B
int PWM2 = 9; // Phase 2 = OC1A
int PWM3 = 10; // Phase 3 = OC1B
const int Phase2Offset = 256 / 3; // 120° phase shift
const int Phase3Offset = (256 * 2) / 3; // 240° phase shift
double OutputFrequency = 50.0;
const double CarrierFrequency = 10000.0; // Carrier frequency in Hz
volatile unsigned long TuningValue; // dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.
// Timer 1 setup
// WGM=8 Phase/Frequency Correct PWM with TOP in ICR1
// To get 10 kHz we need TOP = (16 MHz / 10 kHz / prescale / 2) - 1 = (1600/8/2) - 1 = 99
// Note: We have to use the same TOP on the 8-bit Timer2 so we can't use prescale=1 (799 won't fit in 8 bits)
void Setup_timer1(void)
{
TCCR1B = 0; // Stop the counter (clock select = 0)
TCCR1A = 0; // Clear the register
TIMSK1 = 0; // Disable all Timer1 interrupts
ICR1 = 99; // 10 kHz with prescale = 8;
TCCR1A |= _BV(COM1A1) | _BV(COM1B1); // Enable PWM on A and B
TCCR1B |= _BV(WGM13); // WGM = 8
TCCR1B |= _BV(CS11); // Prescale = 8
}
// Timer 2 setup
// WGM=5 Phase Correct PWM with TOP in OCR2A
// To get 10 kHz we need TOP = (16 MHz / 10 kHz / prescale / 2) - 1 = (1600/8/2) - 1 = 99
void Setup_timer2()
{
TCCR2B = 0; // Stop the counter (clock select = 0)
TCCR2A = 0; // Clear the register
TIMSK2 = 0; // Disable all Timer2 interrupts
OCR2A = 99; // 10 kHz with prescale = 8;
TCCR2A |= _BV(COM1B1); // Enable PWM on B only. (OCR2A holds TOP)
TCCR2A |= _BV(WGM20); // WGM = 5
TCCR2B |= _BV(WGM22) | _BV(CS21); // WGM = 5, Prescale = 8
TIMSK2 |= _BV(TOIE2); // Enable Timer2 Overflow Interrupt
}
void setup()
{
pinMode(PWM1, OUTPUT); //sets the digital pin as output
pinMode(PWM2, OUTPUT); //sets the digital pin as output
pinMode(PWM3, OUTPUT); //sets the digital pin as output
// The tuning value
TuningValue = pow(2, 32) * OutputFrequency / CarrierFrequency; //calulate DDS new tuning word
Setup_timer1();
Setup_timer2();
}
// Timer2 Overflow Interrupt (10 KHz)
ISR(TIMER2_OVF_vect)
{
static uint32_t phase_accumulator = 0;
phase_accumulator += TuningValue;
uint8_t current_count = phase_accumulator >> 24; // use upper 8 bits of phase_accumulator as frequency information
// read value fron ROM sine table and send to PWM1
OCR2B = pgm_read_byte_near(sine256 + current_count);
// read value fron ROM sine table (120° out of phase) and send to PWM2
OCR1A = pgm_read_byte_near(sine256 + (uint8_t)(current_count + Phase2Offset));
// read value from ROM sine table (240° out of phase) and send to PWM3
OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + Phase3Offset));
}
void loop() {}