sanguino/cores/arduino/wiring_analog.c

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 wiring_analog.c - analog input and output
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Part of Arduino - http://www.arduino.cc/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 Copyright (c) 2005-2006 David A. Mellis
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
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 Public License along with this library; if not, write to the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 Boston, MA 02111-1307 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 Modified 28 September 2010 by Mark Sproul
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 #include "wiring_private.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 #include "pins_arduino.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 uint8_t analog_reference = DEFAULT;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 void analogReference(uint8_t mode)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 // can't actually set the register here because the default setting
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 // will connect AVCC and the AREF pin, which would cause a short if
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 // there's something connected to AREF.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 analog_reference = mode;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 int analogRead(uint8_t pin)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 uint8_t low, high;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 if (pin >= 54) pin -= 54; // allow for channel or pin numbers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 #elif defined(__AVR_ATmega32U4__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 if (pin >= 18) pin -= 18; // allow for channel or pin numbers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega644p__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 if (pin >= 24) pin -= 24; // allow for channel or pin numbers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 if (pin >= 14) pin -= 14; // allow for channel or pin numbers
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 #if defined(__AVR_ATmega32U4__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 pin = analogPinToChannel(pin);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 #elif defined(ADCSRB) && defined(MUX5)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 // the MUX5 bit of ADCSRB selects whether we're reading from channels
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 // set the analog reference (high two bits of ADMUX) and select the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 // channel (low 4 bits). this also sets ADLAR (left-adjust result)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 // to 0 (the default).
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66 #if defined(ADMUX)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 ADMUX = (analog_reference << 6) | (pin & 0x07);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 // without a delay, we seem to read from the wrong channel
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 //delay(1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 #if defined(ADCSRA) && defined(ADCL)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 // start the conversion
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 sbi(ADCSRA, ADSC);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 // ADSC is cleared when the conversion finishes
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 while (bit_is_set(ADCSRA, ADSC));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 // we have to read ADCL first; doing so locks both ADCL
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 // and ADCH until ADCH is read. reading ADCL second would
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 // cause the results of each conversion to be discarded,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 // as ADCL and ADCH would be locked when it completed.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 low = ADCL;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 high = ADCH;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87 // we dont have an ADC, return 0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 low = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 high = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 // combine the two bytes
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 return (high << 8) | low;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 // Right now, PWM output only works on the pins with
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 // hardware support. These are defined in the appropriate
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 // pins_*.c file. For the rest of the pins, we default
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 // to digital output.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 void analogWrite(uint8_t pin, int val)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 // We need to make sure the PWM output is enabled for those pins
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 // that support it, as we turn it off when digitally reading or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 // writing with them. Also, make sure the pin is in output mode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 // for consistenty with Wiring, which doesn't require a pinMode
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106 // call for the analog output pins.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 pinMode(pin, OUTPUT);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 if (val == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 digitalWrite(pin, LOW);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 else if (val == 255)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 digitalWrite(pin, HIGH);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 switch(digitalPinToTimer(pin))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 // XXX fix needed for atmega8
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 case TIMER0A:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 // connect pwm to pin on timer 0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 sbi(TCCR0, COM00);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 OCR0 = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 #if defined(TCCR0A) && defined(COM0A1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 case TIMER0A:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 // connect pwm to pin on timer 0, channel A
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 sbi(TCCR0A, COM0A1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 OCR0A = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 #if defined(TCCR0A) && defined(COM0B1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 case TIMER0B:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 // connect pwm to pin on timer 0, channel B
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 sbi(TCCR0A, COM0B1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 OCR0B = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 #if defined(TCCR1A) && defined(COM1A1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 case TIMER1A:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 // connect pwm to pin on timer 1, channel A
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 sbi(TCCR1A, COM1A1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 OCR1A = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 #if defined(TCCR1A) && defined(COM1B1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 case TIMER1B:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 // connect pwm to pin on timer 1, channel B
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 sbi(TCCR1A, COM1B1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 OCR1B = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 #if defined(TCCR2) && defined(COM21)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 case TIMER2:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 // connect pwm to pin on timer 2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 sbi(TCCR2, COM21);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 OCR2 = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 #if defined(TCCR2A) && defined(COM2A1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 case TIMER2A:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 // connect pwm to pin on timer 2, channel A
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 sbi(TCCR2A, COM2A1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 OCR2A = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 #if defined(TCCR2A) && defined(COM2B1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 case TIMER2B:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 // connect pwm to pin on timer 2, channel B
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 sbi(TCCR2A, COM2B1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 OCR2B = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 #if defined(TCCR3A) && defined(COM3A1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 case TIMER3A:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 // connect pwm to pin on timer 3, channel A
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 sbi(TCCR3A, COM3A1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 OCR3A = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 #if defined(TCCR3A) && defined(COM3B1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 case TIMER3B:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 // connect pwm to pin on timer 3, channel B
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 sbi(TCCR3A, COM3B1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 OCR3B = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 #if defined(TCCR3A) && defined(COM3C1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 case TIMER3C:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 // connect pwm to pin on timer 3, channel C
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 sbi(TCCR3A, COM3C1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 OCR3C = val; // set pwm duty
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(TCCR4A)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 case TIMER4A:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 //connect pwm to pin on timer 4, channel A
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 sbi(TCCR4A, COM4A1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 #if defined(COM4A0) // only used on 32U4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 cbi(TCCR4A, COM4A0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 OCR4A = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 #if defined(TCCR4A) && defined(COM4B1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 case TIMER4B:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 // connect pwm to pin on timer 4, channel B
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 sbi(TCCR4A, COM4B1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 OCR4B = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 #if defined(TCCR4A) && defined(COM4C1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229 case TIMER4C:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 // connect pwm to pin on timer 4, channel C
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 sbi(TCCR4A, COM4C1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 OCR4C = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236 #if defined(TCCR4C) && defined(COM4D1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 case TIMER4D:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 // connect pwm to pin on timer 4, channel D
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 sbi(TCCR4C, COM4D1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 #if defined(COM4D0) // only used on 32U4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 cbi(TCCR4C, COM4D0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 OCR4D = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 #if defined(TCCR5A) && defined(COM5A1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 case TIMER5A:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 // connect pwm to pin on timer 5, channel A
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 sbi(TCCR5A, COM5A1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 OCR5A = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 #if defined(TCCR5A) && defined(COM5B1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 case TIMER5B:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 // connect pwm to pin on timer 5, channel B
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 sbi(TCCR5A, COM5B1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 OCR5B = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264 #if defined(TCCR5A) && defined(COM5C1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 case TIMER5C:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266 // connect pwm to pin on timer 5, channel C
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 sbi(TCCR5A, COM5C1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 OCR5C = val; // set pwm duty
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 case NOT_ON_TIMER:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273 default:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274 if (val < 128) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275 digitalWrite(pin, LOW);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277 digitalWrite(pin, HIGH);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279 }
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

mercurial