Mozzi  version v1.1.0
sound synthesis library for Arduino
mozzi_analog.h
1 /*
2  * mozzi_analog.h
3  *
4  * Copyright 2012 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 #ifndef MOZZI_ANALOG_H_
13 #define MOZZI_ANALOG_H_
14 
15  #if ARDUINO >= 100
16  #include "Arduino.h"
17 #else
18  #include "WProgram.h"
19 #endif
20 
21 #include "hardware_defines.h"
22 
23 #if (USE_AUDIO_INPUT==true)
24 #warning "Using AUDIO_INPUT_PIN defined in mozzi_config.h for audio input."
25 #endif
26 
27 // hack for Teensy 2 (ATmega32U4), which has "adc_mapping" instead of "analog_pin_to_channel_PGM"
28 #if defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
29 //pasted from hardware/arduino/variants/leonardo/pins_arduino.h, doesn't work as of mozzi 0.01.2a
30 // __AVR_ATmega32U4__ has an unusual mapping of pins to channels
31 //extern const uint8_t PROGMEM analog_pin_to_channel_PGM[];
32 //#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) )
33 
34 // look at Arduino.app/Contents/Resources/Java/hardware/teensy/cores/teensy/pins_teensy.c - analogRead
35 // adc_mapping is already declared in pins_teensy.c, but it's static there so we can't access it
36 static const uint8_t PROGMEM adc_mapping[] = {
37 // 0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8
38  0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8, 10, 11, 12, 13, 7, 6, 5, 4, 1, 0, 8
39 };
40 #define analogPinToChannel(P) ( pgm_read_byte( adc_mapping + (P) ) )
41 #endif
42 
43 
44 // include this although already in teensy 3 analog.c, because it is static there
45 #if defined(__MK20DX128__)
46 static const uint8_t channel2sc1a[] = {
47  5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
48  0, 19, 3, 21, 26, 22, 23
49 };
50 #elif defined(__MK20DX256__)
51 static const uint8_t channel2sc1a[] = {
52  5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
53  0, 19, 3, 19+128, 26, 18+128, 23,
54  5+192, 5+128, 4+128, 6+128, 7+128, 4+192
55 // A15 26 E1 ADC1_SE5a 5+64
56 // A16 27 C9 ADC1_SE5b 5
57 // A17 28 C8 ADC1_SE4b 4
58 // A18 29 C10 ADC1_SE6b 6
59 // A19 30 C11 ADC1_SE7b 7
60 // A20 31 E0 ADC1_SE4a 4+64
61 };
62 #endif
63 
64 
65 // for setupFastAnalogRead()
66 enum ANALOG_READ_SPEED {FAST_ADC,FASTER_ADC,FASTEST_ADC};
67 
68 /**
69 @ingroup analog
70 This is automatically called in startMozzi.
71 It makes mozziAnalogRead() happen faster than the standard Arduino analogRead(), changing the
72 duration from about 105 in unmodified Arduino to about 16 microseconds for a
73 dependable read with the default speed parameter FAST_ADC.
74 If you want to set on of the faster modes (see params) you can call this after startMozzi().
75 See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208715493/11, and
76 http://www.marulaberry.co.za/index.php/tutorials/code/arduino-adc/
77 @param speed FAST_ADC, FASTER_ADC or FASTEST_ADC. If no parameter is supplied, the
78 default is FAST_ADC, which sets the analog conversion clock predivide rate to
79 16, giving 1Mhz on a 16MHz board, the fastest rate for which behaviour is
80 defined (~16us per sample). However, divisors of 8 and 4 also show usable
81 results in the graphs in this paper:
82 http://dam.mellis.org/Mellis%20-%20Sensor%20Library%20for%20Arduino%20-%20Paper.pdf,
83 so you can also try FASTER_ADC or FASTEST_ADC for divide rates of 8 or 4, giving
84 times of about 8us or 4us per sample. Beware, reliable results will depend on
85 the sort of input signal you have. Only use FASTER_ADC or FASTEST_ADC if you
86 know what you're doing.
87 */
88 void setupFastAnalogRead(int8_t speed=FAST_ADC);
89 
90 
91 
92 /* @ingroup analog
93 Set up for asynchronous analog input, which enables analog reads to take
94 place in the background without blocking the processor.
95 @param speed FAST_ADC, FASTER_ADC or FASTEST_ADC. See setupFastAnalogRead();
96 */
97 void setupMozziADC(int8_t speed=FAST_ADC);
98 
99 
100 
101 /** @ingroup analog
102 Prepare an analog input channel by turning off its digital input buffer.
103 This helps to reduce noise, increase analog reading speed, and save power.
104 
105 Here's more detail from http://www.openmusiclabs.com/learning/digital/atmega-adc/:
106 
107 The DIDR (Data Input Disable Register) disconnects the digital inputs from
108 whichever ADC channels you are using. This is important for 2 reasons. First
109 off, an analog input will be floating all over the place, and causing the
110 digital input to constantly toggle high and low. This creates excessive noise
111 near the ADC, and burns extra power. Secondly, the digital input and associated
112 DIDR switch have a capacitance associated with them which will slow down your
113 input signal if you are sampling a highly resistive load.
114 
115 And from the ATmega328p datasheet, p266:
116 
117 When an analog signal is applied to the ADC pin and the digital input from
118 this pin is not needed, this bit should be written logic one to reduce power
119 consumption in the digital input buffer. Note that ADC named_pins ADC7
120 and ADC6 do not have digital input buffers, and therefore do not require
121 Digital Input Disable bits.
122 @param channel_num the analog input channel you wish to use.
123 */
124 void disconnectDigitalIn(uint8_t channel_num);
125 
126 
127 /** @ingroup analog
128 Reconnect the digital input buffer for an analog input channel which has
129 been set for analog input with disconnectDigitalIn().
130 @param channel_num the analog input channel you wish to reconnect.
131 */
132 void reconnectDigitalIn(uint8_t channel_num);
133 
134 
135 /** @ingroup analog
136 Prepare all analog input channels by turning off their digital input buffers.
137 This helps to reduce noise, increase analog reading speed, and save power.
138 */
140 
141 
142 /** @ingroup analog
143 Reconnect the digital input buffers for analog input channels which have
144 been set for analog input with disconnectDigitalIn().
145 */
147 
148 
149 
150 /* @ingroup analog
151 Starts an analog to digital conversion of the voltage on a specified channel. Unlike
152 Arduino's analogRead() function which waits until a conversion is complete before
153 returning, adcStartConversion() only sets the conversion to begin, so you can use
154 the cpu for other things and call for the result later with adcGetResult().
155 @param channel is the analog channel number (0 to ....), which is not necessarily the same as the pin number
156 Use adcPinToChannelNum() to convert the pin number to its channel number.
157 @note Timing: about 1us when used in updateControl() with CONTROL_RATE 64.
158 */
159 void adcStartConversion(uint8_t channel);
160 
161 
162 
163 /** @ingroup analog
164 Reads the analog input of a chosen channel, without blocking other operations from running.
165 It actually returns the most recent analog reading and puts the chosen pin or channel
166 on the stack of channels to be read in the background before the next control
167 interrupt.
168 @param pin_or_channel the analog pin or channel number.
169 @return the digitised value of the voltage on the chosen channel, in the range 0-1023. @Note that non-AVR
170 hardware may return a different range, e.g. 0-4095 on STM32 boards.
171 */
172 int mozziAnalogRead(uint8_t pin);
173 
174 
175 uint8_t adcPinToChannelNum(uint8_t pin);
176 
177 
178 #endif /* MOZZI_ANALOG_H_ */
int mozziAnalogRead(uint8_t pin)
Reads the analog input of a chosen channel, without blocking other operations from running...
Definition: MozziGuts.cpp:109
void adcReconnectAllDigitalIns()
Reconnect the digital input buffers for analog input channels which have been set for analog input wi...
void reconnectDigitalIn(uint8_t channel_num)
Reconnect the digital input buffer for an analog input channel which has been set for analog input wi...
void disconnectDigitalIn(uint8_t channel_num)
Prepare an analog input channel by turning off its digital input buffer.
void setupFastAnalogRead(int8_t speed)
NOTE: Code needed to set up faster than usual analog reads, e.g.
void adcDisconnectAllDigitalIns()
Prepare all analog input channels by turning off their digital input buffers.