Skip to content

Commit 3bbdf49

Browse files
committed
Merge pull request #2961 from jan-r/issue292-fix
Tone: fix for ATmega8
2 parents 25aeb68 + df577bf commit 3bbdf49

File tree

1 file changed

+11
-10
lines changed
  • hardware/arduino/avr/cores/arduino

1 file changed

+11
-10
lines changed

hardware/arduino/avr/cores/arduino/Tone.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Version Modified By Date Comments
3030
0006 D Mellis 09/12/29 Replaced objects with functions
3131
0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
3232
0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
33+
0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62)
3334
*************************************************/
3435

3536
#include <avr/interrupt.h>
@@ -296,13 +297,13 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
296297
#if defined(TCCR0B)
297298
if (_timer == 0)
298299
{
299-
TCCR0B = prescalarbits;
300+
TCCR0B = (TCCR0B & 0b11111000) | prescalarbits;
300301
}
301302
else
302303
#endif
303304
#if defined(TCCR2B)
304305
{
305-
TCCR2B = prescalarbits;
306+
TCCR2B = (TCCR2B & 0b11111000) | prescalarbits;
306307
}
307308
#else
308309
{
@@ -389,15 +390,15 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
389390
break;
390391
#endif
391392

392-
#if defined(TIMSK3)
393+
#if defined(OCR3A) && defined(TIMSK3) && defined(OCIE3A)
393394
case 3:
394395
OCR3A = ocr;
395396
timer3_toggle_count = toggle_count;
396397
bitWrite(TIMSK3, OCIE3A, 1);
397398
break;
398399
#endif
399400

400-
#if defined(TIMSK4)
401+
#if defined(OCR4A) && defined(TIMSK4) && defined(OCIE4A)
401402
case 4:
402403
OCR4A = ocr;
403404
timer4_toggle_count = toggle_count;
@@ -454,21 +455,21 @@ void disableTimer(uint8_t _timer)
454455
#endif
455456
break;
456457

457-
#if defined(TIMSK3)
458+
#if defined(TIMSK3) && defined(OCIE3A)
458459
case 3:
459-
TIMSK3 = 0;
460+
bitWrite(TIMSK3, OCIE3A, 0);
460461
break;
461462
#endif
462463

463-
#if defined(TIMSK4)
464+
#if defined(TIMSK4) && defined(OCIE4A)
464465
case 4:
465-
TIMSK4 = 0;
466+
bitWrite(TIMSK4, OCIE4A, 0);
466467
break;
467468
#endif
468469

469-
#if defined(TIMSK5)
470+
#if defined(TIMSK5) && defined(OCIE5A)
470471
case 5:
471-
TIMSK5 = 0;
472+
bitWrite(TIMSK5, OCIE5A, 0);
472473
break;
473474
#endif
474475
}

0 commit comments

Comments
 (0)