teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

2021/02/09 01:49

投稿

jyojyo
jyojyo

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,23 +1,12 @@
1
- ソフトウェアからハードウェア置換えて正確な1秒にたいと考えています
1
+ この2つを同じ意味合いにしていますがコンパイルは通り動作が違います‼️
2
- そのためにスイッチとブザーをハードウェアにしたけどうまくいかなかったです。
3
- ※ピン番号は変えいで行うこと
2
+ どこをどのように変えればすか?
3
+
4
+ 動作成功
4
5
  ```Arduino
5
6
  #define F_CPU 8000000UL
6
7
  #include<avr/io.h>
7
8
  #include<util/delay.h>
8
9
  const uint8_t tempo = 100; /*記憶*/
9
- //1
10
- uint8_t notes [2] [50] = {"gdefedccegfeddefgecc facbagdgfeddefgecc n",
11
- "afgefdc afgeegba n"}; /* 音の速度 スペースは休憩を表します*/
12
- uint8_t level [2] [50] = {{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0,
13
- 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0},
14
- {4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 0}};/*音の高さ*/
15
- uint8_t beat [2] [50] = {{ 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2+1,
16
- 2, 1, 2, 1, 1, 3, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2},
17
- {4, 4, 4, 4, 4, 4, 4, 4+1, 4, 4, 4, 4, 2, 2, 4, 4, 4}};/*ビート*/
18
- uint8_t line [] = {0,0,1};/*行*/
19
-
20
-
21
10
  void playTone(uint16_t tone, uint16_t duration) {
22
11
  for (uint32_t i = 0; i < duration * 1000L; i += tone * 2 / 10) {/*duration=期間*/
23
12
  PORTC = 0x0000001;
@@ -32,249 +21,31 @@
32
21
 
33
22
  return;
34
23
  }
35
-
36
- void playNote(uint8_t note, uint16_t level, uint16_t duration) {
37
- uint8_t names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b'}; //ドレミファソラシ
38
- uint16_t tones[] = { 19111, 17026, 15169 + 1, 14317, 12755, 11364, 10124};
39
- /*
40
- / *
41
- timeHigh =期間/ 2 = 1 /(2 * toneFrequency)
42
- ここで、さまざまなトーンは次のように説明されています。
43
- 音符 頻度 期間 高い時間
44
- do4 261.63 Hz 3830 1911.0958223445323548522722929328 (261->1915=1915.7088122605363984674329501916)
45
- re4 293.66 Hz 3400 1702.6493223455697064632568276238 (294->1700=1700.6802721088435374149659863946)
46
- mi4 329.63 Hz 3038 1516.8522282559233079513393805175 (329->1519=1519.7568389057750759878419452888)
47
- fa4 349.23 Hz 2864 1431.7212152449674999284139392378 (349->1432=1432.6647564469914040114613180516)
48
- so4 392 Hz 2550 1275=1275.5102040816326530612244897959
49
- ra4 440 Hz 2272 1136=1136.3636363636363636363636363636
50
- si4 493.88 Hz 2028 1012.3916740908722766663966955536 (493->1014=1014.1987829614604462474645030426)
51
- do5 523.25 Hz 1912 955.566172957477305303392259914 (523->956=956.02294455066921606118546845124)
52
- */
53
- /*音名に対応した音を再生します*/
54
- for (uint8_t i = 0; i < 7; i++) {
55
- if (note == names[i]) {
56
- uint8_t a = level - 4;
57
- playTone(tones[i] / pow(2, a), duration);
58
- }
59
- }
60
-
61
- return;
62
- }
63
-
64
- void setup(){
65
- DDRC = 0x0000001; /* 出力:ブザー(PORTC0)*/
66
- DDRB = 0x00; /*入力:スィッチ(PINB5)*//* <iom328.h> #define PINB5 5*/
67
- DDRD = 0x7F; /* DDRB 出力ピン対応ビットはセット、入力ピン対応ビットはクリア */
68
- PORTD = 0xFF; /* 入力ピンに対応するビットに1を書き込み、内蔵プルアップ指定 */
69
- DDRB = 0x00; /* I/ o:key(PORT0-7)*/
70
- PORTB = 0xFF; /* 入力時にプルアップする必要があります。*/
71
- }
72
- void loop(){
73
- static bool a_mode = 0; /*0-手動、1-自動*/
74
- main_loop:
75
- if (bit_is_clear(PORTD, PORTD7)) /* スイッチが閉じると成立 */
76
- PORTB = PORTB & ~_BV(PB1); /* ここではPD0をクリアしてLED点灯 */
77
- if (a_mode) { //auto(PIND == 0b00100000)
78
- DDRB = 0xff; //出力のみ
79
-
80
- for (uint8_t i = 0; i < 3; i++) { /*行数*/
81
- for (uint8_t j = 0; ; j++) {
82
- if (notes[line[i]][j] == ' ') {
83
- for (uint16_t k = 0; k < beat[line[i]][j] * tempo; k++) {
84
- _delay_ms(1); //rest(milli)
85
- }
86
- }
87
- else if (notes[line[i]][j] == 'n') { /*(j <length [i])の場合、for {}は続行します。*/
88
- break;
89
- }
90
- else{
91
- uint8_t k;
92
- if (notes[line[i]][j] == 'c') {
93
- if (level[line[i]][j] == 4) {
94
- k = 0;
95
- }
96
- else {
97
- k = 7;
98
- }
99
- }
100
- else if (notes[line[i]][j] == 'a') {
101
- k = 5;
102
- }
103
- else if (notes[line[i]][j] == 'b') {
104
- k = 6;
105
- }
106
- else {
107
- for (k = 0; k < 5; k++) {
108
- if (notes[line[i]][j] == 'c' + k) {
109
- break;
110
- }
111
- }
112
- }
113
-
114
- PORTD ^= _BV(k);
115
- playNote(notes[line[i]][j], level[line[i]][j], beat[line[i]][j] * tempo);
116
- PORTD ^= _BV(k);
117
- }//if
118
- if (!(PINB & _BV(5))) { //auto(PINB is 0x00)
119
- a_mode = 0;
120
- goto main_loop;
121
- }
122
- }//for
123
-
124
- }//for
125
-
126
- }//auto
127
- else { /*手動(PIND == 0x00)*/
128
- DDRB = 0x00; /*入力のみ*/
129
- PORTB = 0xff; /*プルアップ*/
130
- switch (PINB) {
131
- case 0b11111110:
132
- do {
133
- playNote('c', 4, 1 * tempo);
134
- } while (PINB == 0b11111110);
135
- break;
136
-
137
- case 0b11111101:
138
- do {
139
- playNote('d', 4, 1 * tempo);
140
- } while (PINB == 0b11111101);
141
- break;
142
-
143
- case 0b11111011:
144
- do {
145
- playNote('e', 4, 1 * tempo);
146
- } while (PINB == 0b11111011);
147
- break;
148
-
149
- case 0b11110111:
150
- do {
151
- playNote('f', 4, 1 * tempo);
152
- } while (PINB == 0b11110111);
153
- break;
154
-
155
- case 0b11101111:
156
- do {
157
- playNote('g', 4, 1 * tempo);
158
- } while (PINB == 0b11101111);
159
- break;
160
-
161
- case 0b11011111:
162
- do {
163
- playNote('a', 4, 1 * tempo);
164
- } while (PINB == 0b11011111);
165
- break;
166
-
167
- case 0b10111111:
168
- do {
169
- playNote('b', 4, 1 * tempo);
170
- } while (PINB == 0b10111111);
171
- break;
172
-
173
- case 0b01111111:
174
- do {
175
- playNote('c', 5, 1 * tempo);
176
- } while (PINB == 0b01111111);
177
- break;
178
-
179
- default:
180
- break;
181
- }
182
-
183
- if (PINB & _BV(5)) { //manual(PINB is 0x20)
184
- a_mode = 1;
185
- }
186
-
187
- }/*マニュアル*/
188
-
189
- }
190
24
  ```
25
+ 動作失敗
191
- 参考プログラム
26
+ ※このプログラムを動作同じにしていくこと
192
27
  ```Arduino
193
- include <avr/io.h>
194
- #include <avr/interrupt.h>
195
-
196
- ISR(TIMER0_OVF_vect)
197
- {
198
- PORTB=0x01; // LED点灯(High)
199
- }
200
-
201
- ISR(TIM0_COMP_vect)
202
- {
203
- PORTB=0x00; // LED消灯(Low)
204
- }
205
-
206
- int main(void)
207
- {
208
- PORTB=0x00;
209
- DDRB=0x01; //PORTB0 出力/Lowに。Highで点灯するようLEDを接続
210
- TCNT0=0;
211
- OCR0=38; // TCNT0=39の瞬間に比較一致成立。39*1024/4MHz≒10msec.
212
- TIFR=_BV(OCF0)|_BV(TOV0); //OCF0/TOV0クリア
213
- TIMSK=_BV(TOIE0)|_BV(OCIE0); //オーバーフロー・比較一致割り込み許可
214
- TCCR0=5; // プリスケーラ 1/1024 タイマが動き出す
215
- sei(); // 全割り込み許可
216
- while(1);
217
- }
218
- ```
219
- 試したこと
220
- 参考プログラムのledをブザーに置換えたけどエラーが発生しました。
221
- ```Arduino
222
28
  #define F_CPU 8000000UL
223
29
  #include<avr/io.h>
224
30
  #include<util/delay.h>
31
+ #include <avr/interrupt.h>
225
32
  const uint8_t tempo = 100; //char
226
33
  const byte PIN_BUZZLE = 10;
227
- const byte PIN_INTERRUPT =PORTC ;
34
+ const byte PIN_INTERRUPT = 23;
228
- volatile byte state = Low:
35
+ volatile byte state = LOW;
229
- //1
230
- void playTone(uint16_t tone, uint16_t duration) {
36
+ void playTone(uint16_t tone, uint16_t duration) {
37
+ pinMode( PIN_BUZZLE, OUTPUT );
231
- for (uint32_t i = 0; i < duration * 1000L; i += tone * 2 / 10) {
38
+ digitalWrite( PIN_BUZZLE, state );
39
+
232
40
  attachInterrupt(
233
- digitalPinToInterrupt(PIN_INTERRUPT, toggle_temp, FALLING );
41
+ digitalPinToInterrupt(PIN_INTERRUPT),
42
+ toggle_BUZZLE, FALLING );
234
43
  }
235
-
236
- void toggle_temp(){
44
+ void duration() {
237
45
  digitalWrite( PIN_BUZZLE, state );
46
+ }
47
+ void toggle_BUZZLE(){
238
48
  state != state;
49
+ _delay_us(1);
239
- }
50
+ }
240
- }
241
- return;
242
- }
243
- void setup()
244
- {
245
- /*
246
- DDRx:Set i/o. 0->input, 1->output. When input, must pullup. cf.mega88A.pdf:14.2.3. "入出力間の切り替え"
247
- PORTx:write only
248
- PINx:read only
249
- */
250
- DDRC = 0b0000001; //output:buzzer(PORTC0)
251
- DDRB = 0x00; //input:switch(PINB5) //<iom328.h>#define PINB5 5
252
- PORTB = 0xff; //When input, must pullup.
253
- DDRD = 0x00; //i/o:key(PORT0-7)
254
- PORTD = 0xff; //When input, must pullup.
255
- //タイマ0,CTC,割り込み用、比較A一致で割り込み
256
- TCCR0A = 0b00000010;
257
- TCCR0B = 0b00000011; // N=64
258
- OCR0A = 78; // 5msごとに割り込み
259
- TIMSK0 = 0b0000010; //比較A一致割り込み有効
260
-
261
-
262
- //方向レジスタの設定
263
- DDRC = 0xff; //Cを出力
264
- DDRD = 0xff; //Dを出力
265
- PORTB = 0xff;
266
-
267
- //割り込み許可
268
- sei();
269
-
270
- while(1){}
271
- }
272
-
273
- ```
274
- エラー
275
- ```Arduino
276
- error: macro "digitalPinToInterrupt" passed 3 arguments, but takes just 1
277
-
278
- exit status 1
279
- macro "digitalPinToInterrupt" passed 3 arguments, but takes just 1
280
51
  ```

3

2021/02/09 01:49

投稿

jyojyo
jyojyo

スコア3

title CHANGED
File without changes
body CHANGED
@@ -2,8 +2,11 @@
2
2
  そのためにスイッチとブザーをハードウェアにしたけどうまくいかなかったです。
3
3
  ※ピン番号は変えないで行うこと
4
4
  ```Arduino
5
+ #define F_CPU 8000000UL
6
+ #include<avr/io.h>
7
+ #include<util/delay.h>
5
8
  const uint8_t tempo = 100; /*記憶*/
6
-
9
+ //1
7
10
  uint8_t notes [2] [50] = {"gdefedccegfeddefgecc facbagdgfeddefgecc n",
8
11
  "afgefdc afgeegba n"}; /* 音の速度 スペースは休憩を表します*/
9
12
  uint8_t level [2] [50] = {{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0,
@@ -185,9 +188,9 @@
185
188
 
186
189
  }
187
190
  ```
188
- 参考
191
+ 参考プログラム
189
192
  ```Arduino
190
- #include <avr/io.h>
193
+ include <avr/io.h>
191
194
  #include <avr/interrupt.h>
192
195
 
193
196
  ISR(TIMER0_OVF_vect)
@@ -214,4 +217,64 @@
214
217
  }
215
218
  ```
216
219
  試したこと
217
- 参考プログラムのledをブザーに置換えたけどエラーが発生しました。
220
+ 参考プログラムのledをブザーに置換えたけどエラーが発生しました。
221
+ ```Arduino
222
+ #define F_CPU 8000000UL
223
+ #include<avr/io.h>
224
+ #include<util/delay.h>
225
+ const uint8_t tempo = 100; //char
226
+ const byte PIN_BUZZLE = 10;
227
+ const byte PIN_INTERRUPT =PORTC ;
228
+ volatile byte state = Low:
229
+ //1
230
+ void playTone(uint16_t tone, uint16_t duration) {
231
+ for (uint32_t i = 0; i < duration * 1000L; i += tone * 2 / 10) {
232
+ attachInterrupt(
233
+ digitalPinToInterrupt(PIN_INTERRUPT, toggle_temp, FALLING );
234
+ }
235
+
236
+ void toggle_temp(){
237
+ digitalWrite( PIN_BUZZLE, state );
238
+ state != state;
239
+ }
240
+ }
241
+ return;
242
+ }
243
+ void setup()
244
+ {
245
+ /*
246
+ DDRx:Set i/o. 0->input, 1->output. When input, must pullup. cf.mega88A.pdf:14.2.3. "入出力間の切り替え"
247
+ PORTx:write only
248
+ PINx:read only
249
+ */
250
+ DDRC = 0b0000001; //output:buzzer(PORTC0)
251
+ DDRB = 0x00; //input:switch(PINB5) //<iom328.h>#define PINB5 5
252
+ PORTB = 0xff; //When input, must pullup.
253
+ DDRD = 0x00; //i/o:key(PORT0-7)
254
+ PORTD = 0xff; //When input, must pullup.
255
+ //タイマ0,CTC,割り込み用、比較A一致で割り込み
256
+ TCCR0A = 0b00000010;
257
+ TCCR0B = 0b00000011; // N=64
258
+ OCR0A = 78; // 5msごとに割り込み
259
+ TIMSK0 = 0b0000010; //比較A一致割り込み有効
260
+
261
+
262
+ //方向レジスタの設定
263
+ DDRC = 0xff; //Cを出力
264
+ DDRD = 0xff; //Dを出力
265
+ PORTB = 0xff;
266
+
267
+ //割り込み許可
268
+ sei();
269
+
270
+ while(1){}
271
+ }
272
+
273
+ ```
274
+ エラー
275
+ ```Arduino
276
+ error: macro "digitalPinToInterrupt" passed 3 arguments, but takes just 1
277
+
278
+ exit status 1
279
+ macro "digitalPinToInterrupt" passed 3 arguments, but takes just 1
280
+ ```

2

2021/02/08 11:16

投稿

jyojyo
jyojyo

スコア3

title CHANGED
File without changes
body CHANGED
@@ -185,7 +185,8 @@
185
185
 
186
186
  }
187
187
  ```
188
+ 参考
188
- ```Arduino参考
189
+ ```Arduino
189
190
  #include <avr/io.h>
190
191
  #include <avr/interrupt.h>
191
192
 

1

2021/02/05 05:29

投稿

jyojyo
jyojyo

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ソフトウェアからハードウェアに置換えて正確な1秒にしたいと考えています。
2
2
  そのためにスイッチとブザーをハードウェアにしたけどうまくいかなかったです。
3
3
  ※ピン番号は変えないで行うこと
4
- ``Arduino
4
+ ```Arduino
5
5
  const uint8_t tempo = 100; /*記憶*/
6
6
 
7
7
  uint8_t notes [2] [50] = {"gdefedccegfeddefgecc facbagdgfeddefgecc n",