スピーカーで音を鳴らす
Dependencies: mbed
main.cpp@3:722a88ffb4f7, 2015-08-22 (annotated)
- Committer:
- hagi_hara
- Date:
- Sat Aug 22 09:59:15 2015 +0000
- Revision:
- 3:722a88ffb4f7
- Parent:
- 2:44b8465482f9
Change for NUCLEO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hagi_hara | 2:44b8465482f9 | 1 | //example_01_speaker |
hagi_hara | 0:f5bba12ef049 | 2 | //スピーカーを鳴らすサンプルコード |
hagi_hara | 0:f5bba12ef049 | 3 | #include "mbed.h" |
hagi_hara | 0:f5bba12ef049 | 4 | |
hagi_hara | 2:44b8465482f9 | 5 | #define SP_OFF 0.0 |
hagi_hara | 2:44b8465482f9 | 6 | #define SP_ON 0.5 |
hagi_hara | 0:f5bba12ef049 | 7 | |
hagi_hara | 2:44b8465482f9 | 8 | #define mC 261.626 //「ド」の周波数(Hz) |
hagi_hara | 2:44b8465482f9 | 9 | |
hagi_hara | 2:44b8465482f9 | 10 | |
hagi_hara | 3:722a88ffb4f7 | 11 | PwmOut speaker(A0);//A0ピンを出力ピンに設定 |
hagi_hara | 0:f5bba12ef049 | 12 | |
hagi_hara | 0:f5bba12ef049 | 13 | int main() |
hagi_hara | 0:f5bba12ef049 | 14 | { |
hagi_hara | 0:f5bba12ef049 | 15 | //音を鳴らす |
hagi_hara | 0:f5bba12ef049 | 16 | speaker.period(1.0/mC); |
hagi_hara | 2:44b8465482f9 | 17 | speaker.write(SP_ON); |
hagi_hara | 0:f5bba12ef049 | 18 | |
hagi_hara | 0:f5bba12ef049 | 19 | //5秒待つ |
hagi_hara | 0:f5bba12ef049 | 20 | wait(5.0); |
hagi_hara | 0:f5bba12ef049 | 21 | |
hagi_hara | 0:f5bba12ef049 | 22 | //音を止める |
hagi_hara | 2:44b8465482f9 | 23 | speaker.write(SP_OFF); |
hagi_hara | 0:f5bba12ef049 | 24 | |
hagi_hara | 0:f5bba12ef049 | 25 | |
hagi_hara | 1:42c121dda003 | 26 | while(true); |
hagi_hara | 0:f5bba12ef049 | 27 | } |