1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

digisparkでPWMの周波数を高くする

Posted at

https://digistump.com/wiki/digispark/tricks
こちらに記載がありますが、OSごとに場所が違ってよくわからなかったのでまとめておきます。


デフォルトでは 1007Hz となっているため、ブラシモータなどを接続すると音がなってしまいます。そのため、周波数を高くします。

Pin1,Pin4共に周波数を高くするには、 core_build_options.hwring.c をそれぞれ編集します。

core_build_options.h:

  • FAVOR_PHASE_CORRECT_PWM 1 → 0
--- core_build_options.h.bk
+++ core_build_options.h
@@ -116,7 +116,7 @@
   pins and one fast PWM pin.  If false there will be one phase-correct PWM 
   pin and two fast PWM pins.
 */
-#define FAVOR_PHASE_CORRECT_PWM                   1
+#define FAVOR_PHASE_CORRECT_PWM                   0
 
 /*
   Tone goes on whichever timer was not used for millis.

wring.c:

  • MS_TIMER_TICK_EVERY_X_CYCLES 64 → 1
--- wiring.c.bk
+++ wiring.c
@@ -48,7 +48,7 @@
 #define MILLISTIMER_OVF_vect                      MILLISTIMER_(OVF_vect)
 
 
-#define MS_TIMER_TICK_EVERY_X_CYCLES  64 /* Shall be a within 1, 8, 64, 256 or 1024. (default = 64) If set to 1, HW PWM is around 64.5KHz@16.5MHz with Digispark */
+#define MS_TIMER_TICK_EVERY_X_CYCLES  1 /* Shall be a within 1, 8, 64, 256 or 1024. (default = 64) If set to 1, HW PWM is around 64.5KHz@16.5MHz with Digispark */
 
 #if F_CPU >= 3000000L
 #if !defined(MS_TIMER_TICK_EVERY_X_CYCLES)

これで、64453Hzになるはずです。簡易オシロで確認。

image.png

良さそうです。

サンプルコード

.c
#define PWM_OUT 1

void setup()
{
  pinMode(PWM_OUT, OUTPUT);
  analogWrite(PWM_OUT, 50);
}

void loop()
{
}

各OSでのファイルの在処

Linux:

  • $HOME/.arduino15/packages/digistump/hardware/avr/1.6.7/cores/tiny/core_build_options.h
  • $HOME/.arduino15/packages/digistump/hardware/avr/1.6.7/cores/tiny/wiring.c

Windows 10:

  • %LOCALAPPDATA%\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny\core_build_options.h
  • %LOCALAPPDATA%\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny\wiring.c

OSX:

  • $HOME/Library/Arduino15/packages/digistump/hardware/avr/1.6.7/cores/tiny/core_build_options.h
  • $HOME/Library/Arduino15/packages/digistump/hardware/avr/1.6.7/cores/tiny/wring.c
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?