sanguino/cores/arduino/Tone.cpp

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /* Tone.cpp
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 A Tone Generator Library
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 Written by Brett Hagman
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 You should have received a copy of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 License along with this library; if not, write to the Free Software
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 Version Modified By Date Comments
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 ------- ----------- -------- --------
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 0001 B Hagman 09/08/02 Initial coding
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 0002 B Hagman 09/08/18 Multiple pins
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 0003 B Hagman 09/08/18 Moved initialization from constructor to begin()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 0004 B Hagman 09/09/26 Fixed problems with ATmega8
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 09/11/25 Changed pin toggle method to XOR
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 09/11/25 Fixed timer0 from being excluded
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 0006 D Mellis 09/12/29 Replaced objects with functions
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 *************************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 #include <avr/interrupt.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 #include <avr/pgmspace.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 #include "Arduino.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 #include "pins_arduino.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 #define TCCR2A TCCR2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 #define TCCR2B TCCR2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 #define COM2A1 COM21
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 #define COM2A0 COM20
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 #define OCR2A OCR2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 #define TIMSK2 TIMSK
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 #define OCIE2A OCIE2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 #define TIMER2_COMPA_vect TIMER2_COMP_vect
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 #define TIMSK1 TIMSK
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 // timerx_toggle_count:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 // > 0 - duration specified
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 // = 0 - stopped
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 // < 0 - infinitely (until stop() method called, or new play() called)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 #if !defined(__AVR_ATmega8__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 volatile long timer0_toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 volatile uint8_t *timer0_pin_port;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 volatile uint8_t timer0_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 volatile long timer1_toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 volatile uint8_t *timer1_pin_port;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 volatile uint8_t timer1_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 volatile long timer2_toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66 volatile uint8_t *timer2_pin_port;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 volatile uint8_t timer2_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 #if defined(TIMSK3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 volatile long timer3_toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 volatile uint8_t *timer3_pin_port;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 volatile uint8_t timer3_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 #if defined(TIMSK4)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 volatile long timer4_toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 volatile uint8_t *timer4_pin_port;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 volatile uint8_t timer4_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 #if defined(TIMSK5)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 volatile long timer5_toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 volatile uint8_t *timer5_pin_port;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 volatile uint8_t timer5_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 // MLS: This does not make sense, the 3 options are the same
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 #define AVAILABLE_TONE_PINS 1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 #elif defined(__AVR_ATmega8__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 #define AVAILABLE_TONE_PINS 1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 #define AVAILABLE_TONE_PINS 1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 // Leave timer 0 to last.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 static int8_t toneBegin(uint8_t _pin)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 int8_t _timer = -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 // if we're already using the pin, the timer should be configured.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 if (tone_pins[i] == _pin) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 return pgm_read_byte(tone_pin_to_timer_PGM + i);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 // search for an unused timer.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 if (tone_pins[i] == 255) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 tone_pins[i] = _pin;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 if (_timer != -1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 // Set timer specific stuff
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 // All timers in CTC mode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 // 8 bit timers will require changing prescalar values,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 switch (_timer)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 #if defined(TCCR0A) && defined(TCCR0B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 case 0:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 // 8 bit timer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 TCCR0A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 TCCR0B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 bitWrite(TCCR0A, WGM01, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 bitWrite(TCCR0B, CS00, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 timer0_pin_mask = digitalPinToBitMask(_pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 case 1:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 // 16 bit timer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 TCCR1A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 TCCR1B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 bitWrite(TCCR1B, WGM12, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 bitWrite(TCCR1B, CS10, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 timer1_pin_mask = digitalPinToBitMask(_pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 #if defined(TCCR2A) && defined(TCCR2B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 case 2:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 // 8 bit timer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 TCCR2A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 TCCR2B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 bitWrite(TCCR2A, WGM21, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 bitWrite(TCCR2B, CS20, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 timer2_pin_mask = digitalPinToBitMask(_pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 case 3:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 // 16 bit timer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 TCCR3A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 TCCR3B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 bitWrite(TCCR3B, WGM32, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 bitWrite(TCCR3B, CS30, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 timer3_pin_mask = digitalPinToBitMask(_pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 case 4:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 // 16 bit timer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 TCCR4A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 TCCR4B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 #if defined(WGM42)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 bitWrite(TCCR4B, WGM42, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 #elif defined(CS43)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 #warning this may not be correct
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 // atmega32u4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 bitWrite(TCCR4B, CS43, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 bitWrite(TCCR4B, CS40, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 timer4_pin_mask = digitalPinToBitMask(_pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 case 5:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 // 16 bit timer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 TCCR5A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 TCCR5B = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 bitWrite(TCCR5B, WGM52, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 bitWrite(TCCR5B, CS50, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 timer5_pin_mask = digitalPinToBitMask(_pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 return _timer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 // frequency (in hertz) and duration (in milliseconds).
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 uint8_t prescalarbits = 0b001;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 long toggle_count = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 uint32_t ocr = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 int8_t _timer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 _timer = toneBegin(_pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 if (_timer >= 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 // Set the pinMode as OUTPUT
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 pinMode(_pin, OUTPUT);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 // if we are using an 8 bit timer, scan through prescalars to find the best fit
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 if (_timer == 0 || _timer == 2)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 ocr = F_CPU / frequency / 2 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 prescalarbits = 0b001; // ck/1: same for both timers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 if (ocr > 255)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 ocr = F_CPU / frequency / 2 / 8 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 prescalarbits = 0b010; // ck/8: same for both timers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 if (_timer == 2 && ocr > 255)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 ocr = F_CPU / frequency / 2 / 32 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 prescalarbits = 0b011;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 if (ocr > 255)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 ocr = F_CPU / frequency / 2 / 64 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 prescalarbits = _timer == 0 ? 0b011 : 0b100;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 if (_timer == 2 && ocr > 255)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 ocr = F_CPU / frequency / 2 / 128 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 prescalarbits = 0b101;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271 if (ocr > 255)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273 ocr = F_CPU / frequency / 2 / 256 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274 prescalarbits = _timer == 0 ? 0b100 : 0b110;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275 if (ocr > 255)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277 // can't do any better than /1024
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278 ocr = F_CPU / frequency / 2 / 1024 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279 prescalarbits = _timer == 0 ? 0b101 : 0b111;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
280 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
281 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
282 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
283 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
284
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
285 #if defined(TCCR0B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
286 if (_timer == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
287 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
288 TCCR0B = prescalarbits;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
289 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
290 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
291 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
292 #if defined(TCCR2B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
293 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
294 TCCR2B = prescalarbits;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
295 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
296 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
297 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
298 // dummy place holder to make the above ifdefs work
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
299 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
300 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
301 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
302 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
303 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
304 // two choices for the 16 bit timers: ck/1 or ck/64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
305 ocr = F_CPU / frequency / 2 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
306
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
307 prescalarbits = 0b001;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
308 if (ocr > 0xffff)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
309 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
310 ocr = F_CPU / frequency / 2 / 64 - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
311 prescalarbits = 0b011;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
312 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
313
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
314 if (_timer == 1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
315 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
316 #if defined(TCCR1B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
317 TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
318 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
319 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
320 #if defined(TCCR3B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
321 else if (_timer == 3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
322 TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
323 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
324 #if defined(TCCR4B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
325 else if (_timer == 4)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
326 TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
327 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
328 #if defined(TCCR5B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
329 else if (_timer == 5)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
330 TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
331 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
332
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
333 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
334
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
335
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
336 // Calculate the toggle count
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
337 if (duration > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
338 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
339 toggle_count = 2 * frequency * duration / 1000;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
340 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
341 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
342 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
343 toggle_count = -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
344 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
345
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
346 // Set the OCR for the given timer,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
347 // set the toggle count,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
348 // then turn on the interrupts
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
349 switch (_timer)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
350 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
351
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
352 #if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
353 case 0:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
354 OCR0A = ocr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
355 timer0_toggle_count = toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
356 bitWrite(TIMSK0, OCIE0A, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
357 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
358 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
359
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
360 case 1:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
361 #if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
362 OCR1A = ocr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
363 timer1_toggle_count = toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
364 bitWrite(TIMSK1, OCIE1A, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
365 #elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
366 // this combination is for at least the ATmega32
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
367 OCR1A = ocr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
368 timer1_toggle_count = toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
369 bitWrite(TIMSK, OCIE1A, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
370 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
371 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
372
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
373 #if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
374 case 2:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
375 OCR2A = ocr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
376 timer2_toggle_count = toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
377 bitWrite(TIMSK2, OCIE2A, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
378 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
379 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
380
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
381 #if defined(TIMSK3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
382 case 3:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
383 OCR3A = ocr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
384 timer3_toggle_count = toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
385 bitWrite(TIMSK3, OCIE3A, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
386 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
387 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
388
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
389 #if defined(TIMSK4)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
390 case 4:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
391 OCR4A = ocr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
392 timer4_toggle_count = toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
393 bitWrite(TIMSK4, OCIE4A, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
394 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
395 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
396
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
397 #if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
398 case 5:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
399 OCR5A = ocr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
400 timer5_toggle_count = toggle_count;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
401 bitWrite(TIMSK5, OCIE5A, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
402 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
403 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
404
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
405 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
406 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
407 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
408
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
409
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
410 // XXX: this function only works properly for timer 2 (the only one we use
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
411 // currently). for the others, it should end the tone, but won't restore
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
412 // proper PWM functionality for the timer.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
413 void disableTimer(uint8_t _timer)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
414 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
415 switch (_timer)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
416 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
417 case 0:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
418 #if defined(TIMSK0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
419 TIMSK0 = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
420 #elif defined(TIMSK)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
421 TIMSK = 0; // atmega32
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
422 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
423 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
424
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
425 #if defined(TIMSK1) && defined(OCIE1A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
426 case 1:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
427 bitWrite(TIMSK1, OCIE1A, 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
428 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
429 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
430
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
431 case 2:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
432 #if defined(TIMSK2) && defined(OCIE2A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
433 bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
434 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
435 #if defined(TCCR2A) && defined(WGM20)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
436 TCCR2A = (1 << WGM20);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
437 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
438 #if defined(TCCR2B) && defined(CS22)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
439 TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
440 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
441 #if defined(OCR2A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
442 OCR2A = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
443 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
444 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
445
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
446 #if defined(TIMSK3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
447 case 3:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
448 TIMSK3 = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
449 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
450 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
451
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
452 #if defined(TIMSK4)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
453 case 4:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
454 TIMSK4 = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
455 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
456 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
457
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
458 #if defined(TIMSK5)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
459 case 5:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
460 TIMSK5 = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
461 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
462 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
463 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
464 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
465
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
466
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
467 void noTone(uint8_t _pin)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
468 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
469 int8_t _timer = -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
470
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
471 for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
472 if (tone_pins[i] == _pin) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
473 _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
474 tone_pins[i] = 255;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
475 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
476 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
477
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
478 disableTimer(_timer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
479
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
480 digitalWrite(_pin, 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
481 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
482
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
483 #if 0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
484 #if !defined(__AVR_ATmega8__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
485 ISR(TIMER0_COMPA_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
486 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
487 if (timer0_toggle_count != 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
488 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
489 // toggle the pin
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
490 *timer0_pin_port ^= timer0_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
491
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
492 if (timer0_toggle_count > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
493 timer0_toggle_count--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
494 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
495 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
496 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
497 disableTimer(0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
498 *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
499 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
500 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
501 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
502
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
503
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
504 ISR(TIMER1_COMPA_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
505 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
506 if (timer1_toggle_count != 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
507 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
508 // toggle the pin
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
509 *timer1_pin_port ^= timer1_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
510
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
511 if (timer1_toggle_count > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
512 timer1_toggle_count--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
513 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
514 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
515 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
516 disableTimer(1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
517 *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
518 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
519 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
520 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
521
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
522
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
523 ISR(TIMER2_COMPA_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
524 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
525
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
526 if (timer2_toggle_count != 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
527 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
528 // toggle the pin
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
529 *timer2_pin_port ^= timer2_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
530
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
531 if (timer2_toggle_count > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
532 timer2_toggle_count--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
533 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
534 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
535 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
536 // need to call noTone() so that the tone_pins[] entry is reset, so the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
537 // timer gets initialized next time we call tone().
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
538 // XXX: this assumes timer 2 is always the first one used.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
539 noTone(tone_pins[0]);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
540 // disableTimer(2);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
541 // *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
542 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
543 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
544
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
545
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
546
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
547 //#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
548 #if 0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
549
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
550 ISR(TIMER3_COMPA_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
551 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
552 if (timer3_toggle_count != 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
553 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
554 // toggle the pin
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
555 *timer3_pin_port ^= timer3_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
556
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
557 if (timer3_toggle_count > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
558 timer3_toggle_count--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
559 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
560 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
561 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
562 disableTimer(3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
563 *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
564 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
565 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
566
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
567 ISR(TIMER4_COMPA_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
568 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
569 if (timer4_toggle_count != 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
570 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
571 // toggle the pin
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
572 *timer4_pin_port ^= timer4_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
573
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
574 if (timer4_toggle_count > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
575 timer4_toggle_count--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
576 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
577 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
578 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
579 disableTimer(4);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
580 *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
581 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
582 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
583
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
584 ISR(TIMER5_COMPA_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
585 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
586 if (timer5_toggle_count != 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
587 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
588 // toggle the pin
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
589 *timer5_pin_port ^= timer5_pin_mask;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
590
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
591 if (timer5_toggle_count > 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
592 timer5_toggle_count--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
593 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
594 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
595 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
596 disableTimer(5);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
597 *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
598 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
599 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
600
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
601 #endif

mercurial