aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino
diff options
context:
space:
mode:
authorZach Eveland <zeveland@blacklabel-development.com>2011-12-11 19:56:50 -0500
committerZach Eveland <zeveland@blacklabel-development.com>2011-12-11 19:56:50 -0500
commitc58fcf5554827113680ee16559c36ed21e0ec0e0 (patch)
tree642552d3055d47962092c6d2b7233f6c8f086ddd /cores/arduino
parenta9d1368e4c424f17d7d77efeb666f2f7734c7b93 (diff)
fixed TIMER4 use on Leonardo
ATMEGA32U4 has major differences in TIMER4 registers compared to ATMEGA1280 and 2560. turnOffPWM, analogWrite, and initialize routines had wrong registers, bit names, etc.
Diffstat (limited to 'cores/arduino')
-rw-r--r--[-rwxr-xr-x]cores/arduino/wiring.c11
-rw-r--r--cores/arduino/wiring_analog.c24
-rw-r--r--[-rwxr-xr-x]cores/arduino/wiring_digital.c22
3 files changed, 45 insertions, 12 deletions
diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c
index e7f7cde..9c264d0 100755..100644
--- a/cores/arduino/wiring.c
+++ b/cores/arduino/wiring.c
@@ -278,12 +278,21 @@ void init()
sbi(TCCR3B, CS30);
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
#endif
-
+
+#if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */
+ sbi(TCCR4A, COM4A1); // clear channel A on output compare match
+ sbi(TCCR4C, COM4D1); // clear channel D on output compare match
+ sbi(TCCR4B, CS42); // set timer4 prescale factor to 64
+ sbi(TCCR4B, CS41);
+ sbi(TCCR4B, CS40);
+ sbi(TCCR4D, WGM40); // put timer 4 in phase- and frequency-correct PWM mode
+#else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */
#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
sbi(TCCR4B, CS40);
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
#endif
+#endif /* end timer4 block for ATMEGA1280/2560 and similar */
#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c
index 902b153..db6cb7e 100644
--- a/cores/arduino/wiring_analog.c
+++ b/cores/arduino/wiring_analog.c
@@ -204,14 +204,18 @@ void analogWrite(uint8_t pin, int val)
break;
#endif
- #if defined(TCCR4A) && defined(COM4A1)
+ #if defined(TCCR4A)
case TIMER4A:
- // connect pwm to pin on timer 4, channel A
+ //connect pwm to pin on timer 4, channel A
+ #if defined(PWM4A) /* ATMEGA32U4 and related */
+ sbi(TCCR4A, PWM4A);
+ #elif defined(COM4A1) /* ATMEGA1280/2560 and related */
sbi(TCCR4A, COM4A1);
- OCR4A = val; // set pwm duty
+ #endif
+ OCR4A = val; // set pwm duty
break;
#endif
-
+
#if defined(TCCR4A) && defined(COM4B1)
case TIMER4B:
// connect pwm to pin on timer 4, channel B
@@ -228,14 +232,17 @@ void analogWrite(uint8_t pin, int val)
break;
#endif
- #if defined(TCCR4A) && defined(COM4D1)
- case TIMER4D:
+ #if defined(TCCR4C)
+ case TIMER4D:
// connect pwm to pin on timer 4, channel D
- sbi(TCCR4A, COM4D1);
- OCR4D = val; // set pwm duty
+ #if defined(PWM4D) /* ATMEGA32U4 and related */
+ sbi(TCCR4C, PWM4D);
+ #endif
+ OCR4D = val; // set pwm duty
break;
#endif
+
#if defined(TCCR5A) && defined(COM5A1)
case TIMER5A:
// connect pwm to pin on timer 5, channel A
@@ -270,3 +277,4 @@ void analogWrite(uint8_t pin, int val)
}
}
}
+
diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c
index 97ef134..112defc 100755..100644
--- a/cores/arduino/wiring_digital.c
+++ b/cores/arduino/wiring_digital.c
@@ -105,15 +105,31 @@ static void turnOffPWM(uint8_t timer)
case TIMER3C: cbi(TCCR3A, COM3C1); break;
#endif
- #if defined(TCCR4A) && defined(COM4A1)
- case TIMER4A: cbi(TCCR4A, COM4A1); break;
- #endif
+ #if defined(TCCR4A)
+ case TIMER4A:
+ #if defined(PWM4A)
+ cbi(TCCR4A, PWM4A);
+ #elif defined(COM4A1)
+ cbi(TCCR4A, COM4A1);
+ #endif
+ break;
+ #endif
+
#if defined(TCCR4A) && defined(COM4B1)
case TIMER4B: cbi(TCCR4A, COM4B1); break;
#endif
#if defined(TCCR4A) && defined(COM4C1)
case TIMER4C: cbi(TCCR4A, COM4C1); break;
#endif
+
+ #if defined(TCCR4C)
+ case TIMER4D:
+ #if defined(PWM4D)
+ cbi(TCCR4C, PWM4D);
+ #endif
+ break;
+ #endif
+
#if defined(TCCR5A)
case TIMER5A: cbi(TCCR5A, COM5A1); break;
case TIMER5B: cbi(TCCR5A, COM5B1); break;