37 #include <avr/interrupt.h> 38 #include <avr/pgmspace.h> 40 #include "pins_arduino.h" 42 #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) 50 #define TIMER2_COMPA_vect TIMER2_COMP_vect 59 #if !defined(__AVR_ATmega8__) 73 volatile long timer3_toggle_count;
74 volatile uint8_t *timer3_pin_port;
75 volatile uint8_t timer3_pin_mask;
79 volatile long timer4_toggle_count;
80 volatile uint8_t *timer4_pin_port;
81 volatile uint8_t timer4_pin_mask;
85 volatile long timer5_toggle_count;
86 volatile uint8_t *timer5_pin_port;
87 volatile uint8_t timer5_pin_mask;
91 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 93 #define AVAILABLE_TONE_PINS 1 99 #elif defined(__AVR_ATmega8__) 101 #define AVAILABLE_TONE_PINS 1 107 #elif defined(__AVR_ATmega32U4__) 109 #define AVAILABLE_TONE_PINS 1 117 #define AVAILABLE_TONE_PINS 1 156 #if defined(TCCR0A) && defined(TCCR0B) && defined(WGM01) 168 #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12) 180 #if defined(TCCR2A) && defined(TCCR2B) 192 #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3) 204 #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4) 222 #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5) 243 void tone(uint8_t _pin,
unsigned int frequency,
unsigned long duration)
245 uint8_t prescalarbits = 0b001;
246 long toggle_count = 0;
258 if (_timer == 0 || _timer == 2)
260 ocr = F_CPU / frequency / 2 - 1;
261 prescalarbits = 0b001;
264 ocr = F_CPU / frequency / 2 / 8 - 1;
265 prescalarbits = 0b010;
267 if (_timer == 2 && ocr > 255)
269 ocr = F_CPU / frequency / 2 / 32 - 1;
270 prescalarbits = 0b011;
275 ocr = F_CPU / frequency / 2 / 64 - 1;
276 prescalarbits = _timer == 0 ? 0b011 : 0b100;
278 if (_timer == 2 && ocr > 255)
280 ocr = F_CPU / frequency / 2 / 128 - 1;
281 prescalarbits = 0b101;
286 ocr = F_CPU / frequency / 2 / 256 - 1;
287 prescalarbits = _timer == 0 ? 0b100 : 0b110;
291 ocr = F_CPU / frequency / 2 / 1024 - 1;
292 prescalarbits = _timer == 0 ? 0b101 : 0b111;
301 TCCR0B = (TCCR0B & 0b11111000) | prescalarbits;
307 TCCR2B = (TCCR2B & 0b11111000) | prescalarbits;
318 ocr = F_CPU / frequency / 2 - 1;
320 prescalarbits = 0b001;
323 ocr = F_CPU / frequency / 2 / 64 - 1;
324 prescalarbits = 0b011;
330 TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
334 else if (_timer == 3)
335 TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
338 else if (_timer == 4)
339 TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
342 else if (_timer == 5)
343 TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
352 toggle_count = 2 * frequency * duration / 1000;
365 #if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A) 374 #if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A) 378 #elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A) 386 #if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A) 394 #if defined(OCR3A) && defined(TIMSK3) && defined(OCIE3A) 397 timer3_toggle_count = toggle_count;
402 #if defined(OCR4A) && defined(TIMSK4) && defined(OCIE4A) 405 timer4_toggle_count = toggle_count;
410 #if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A) 413 timer5_toggle_count = toggle_count;
438 #if defined(TIMSK1) && defined(OCIE1A) 445 #if defined(TIMSK2) && defined(OCIE2A) 448 #if defined(TCCR2A) && defined(WGM20) 449 TCCR2A = (1 << WGM20);
451 #if defined(TCCR2B) && defined(CS22) 452 TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
459 #if defined(TIMSK3) && defined(OCIE3A) 465 #if defined(TIMSK4) && defined(OCIE4A) 471 #if defined(TIMSK5) && defined(OCIE5A) 498 ISR(TIMER0_COMPA_vect)
518 ISR(TIMER1_COMPA_vect)
563 ISR(TIMER3_COMPA_vect)
565 if (timer3_toggle_count != 0)
568 *timer3_pin_port ^= timer3_pin_mask;
570 if (timer3_toggle_count > 0)
571 timer3_toggle_count--;
576 *timer3_pin_port &= ~(timer3_pin_mask);
583 ISR(TIMER4_COMPA_vect)
585 if (timer4_toggle_count != 0)
588 *timer4_pin_port ^= timer4_pin_mask;
590 if (timer4_toggle_count > 0)
591 timer4_toggle_count--;
596 *timer4_pin_port &= ~(timer4_pin_mask);
603 ISR(TIMER5_COMPA_vect)
605 if (timer5_toggle_count != 0)
608 *timer5_pin_port ^= timer5_pin_mask;
610 if (timer5_toggle_count > 0)
611 timer5_toggle_count--;
616 *timer5_pin_port &= ~(timer5_pin_mask);
void pinMode(uint8_t, uint8_t)
static uint8_t tone_pins[AVAILABLE_TONE_PINS]
volatile long timer1_toggle_count
volatile long timer2_toggle_count
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
volatile uint8_t * timer1_pin_port
volatile uint8_t timer1_pin_mask
void noTone(uint8_t _pin)
volatile uint8_t timer2_pin_mask
static int8_t toneBegin(uint8_t _pin)
#define AVAILABLE_TONE_PINS
void disableTimer(uint8_t _timer)
volatile uint8_t * timer0_pin_port
#define portOutputRegister(P)
#define digitalPinToBitMask(P)
void digitalWrite(uint8_t, uint8_t)
volatile uint8_t timer0_pin_mask
#define bitWrite(value, bit, bitvalue)
const uint8_t PROGMEM tone_pin_to_timer_PGM[]
volatile long timer0_toggle_count
volatile uint8_t * timer2_pin_port
#define digitalPinToPort(P)