Skip to main content
deleted 29 characters in body
Source Link
Gerben
  • 11.3k
  • 3
  • 22
  • 34

Since the timer1 library only accepts whole numbers for the µs parameter you get a error. You could skip using the library and configure the timer directly. Or you could have a look at the source code of the library, and see that you can kind of bypass the limitation it has by only calculating a more accurate value for the ICR1 register.

Look at cycles = ((F_CPU/100000 * microseconds) / 20);. If you were to insert your 66.66666µs into this, instead of 66µs, you'd get 533 instead of 528.

This value is then used to set the PWM frequency in line 213. So you'd need to overwrite the ICR1 register with the more accurate value.

Your final code should look like:

pinMode(9,OUTPUT);
Timer1.initialize(66);  // Frequency, 100us = 10khz
ICR1 = 533;
Timer1.pwm(9,255);

PS you might need to tweak this 533 value to get an even more accurate resulting frequency.

Since the timer1 library only accepts whole numbers for the µs parameter you get a error. You could skip using the library and configure the timer directly. Or you could have a look at the source code of the library, and see that you can kind of bypass the limitation it has by only calculating a more accurate value for the ICR1 register.

Look at cycles = ((F_CPU/100000 * microseconds) / 20);. If you were to insert your 66.66666µs into this, instead of 66µs, you'd get 533 instead of 528.

This value is then used to set the PWM frequency in line 213. So you'd need to overwrite the ICR1 register with the more accurate value.

Your final code should look like:

pinMode(9,OUTPUT);
Timer1.initialize(66);  // Frequency, 100us = 10khz
ICR1 = 533;
Timer1.pwm(9,255);

PS you might need to tweak this 533 value to get an even more accurate resulting frequency.

Since the timer1 library only accepts whole numbers for the µs parameter you get a error. You could skip using the library and configure the timer directly. Or you could have a look at the source code of the library, and see that you can kind of bypass the limitation it has by only calculating a more accurate value for the ICR1 register.

Look at cycles = ((F_CPU/100000 * microseconds) / 20);. If you were to insert your 66.66666µs into this, instead of 66µs, you'd get 533 instead of 528.

This value is then used to set the PWM frequency in line 213. So you'd need to overwrite the ICR1 register with the more accurate value.

Your final code should look like:

pinMode(9,OUTPUT);
Timer1.initialize(66);
ICR1 = 533;
Timer1.pwm(9,255);

PS you might need to tweak this 533 value to get an even more accurate resulting frequency.

Source Link
Gerben
  • 11.3k
  • 3
  • 22
  • 34

Since the timer1 library only accepts whole numbers for the µs parameter you get a error. You could skip using the library and configure the timer directly. Or you could have a look at the source code of the library, and see that you can kind of bypass the limitation it has by only calculating a more accurate value for the ICR1 register.

Look at cycles = ((F_CPU/100000 * microseconds) / 20);. If you were to insert your 66.66666µs into this, instead of 66µs, you'd get 533 instead of 528.

This value is then used to set the PWM frequency in line 213. So you'd need to overwrite the ICR1 register with the more accurate value.

Your final code should look like:

pinMode(9,OUTPUT);
Timer1.initialize(66);  // Frequency, 100us = 10khz
ICR1 = 533;
Timer1.pwm(9,255);

PS you might need to tweak this 533 value to get an even more accurate resulting frequency.