From c58fcf5554827113680ee16559c36ed21e0ec0e0 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sun, 11 Dec 2011 19:56:50 -0500 Subject: 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. --- cores/arduino/wiring_analog.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'cores/arduino/wiring_analog.c') 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) } } } + -- cgit v1.2.3-18-g5258 From 177641003c53844ad73584553a9d44f2da4648e2 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sun, 18 Dec 2011 14:18:38 -0500 Subject: fixed digitalWrite on timer 4 pins (D6 and D13) --- cores/arduino/wiring_analog.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'cores/arduino/wiring_analog.c') diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index db6cb7e..8a6fef3 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -207,11 +207,7 @@ void analogWrite(uint8_t pin, int val) #if defined(TCCR4A) case TIMER4A: //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); - #endif OCR4A = val; // set pwm duty break; #endif @@ -235,9 +231,7 @@ void analogWrite(uint8_t pin, int val) #if defined(TCCR4C) case TIMER4D: // connect pwm to pin on timer 4, channel D - #if defined(PWM4D) /* ATMEGA32U4 and related */ - sbi(TCCR4C, PWM4D); - #endif + sbi(TCCR4C, COM4D1); OCR4D = val; // set pwm duty break; #endif -- cgit v1.2.3-18-g5258 From 4fade7007fbe2049f4f7a3867c9f9a76f8c06db1 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 2 Mar 2012 18:58:53 -0500 Subject: Small changes for the ATmega1284. http://code.google.com/p/arduino/issues/detail?id=736 --- cores/arduino/wiring_analog.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'cores/arduino/wiring_analog.c') diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 902b153..a8bc817 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -45,6 +45,8 @@ int analogRead(uint8_t pin) if (pin >= 54) pin -= 54; // allow for channel or pin numbers #elif defined(__AVR_ATmega32U4__) if (pin >= 18) pin -= 18; // allow for channel or pin numbers +#elif defined(__AVR_ATmega1284__) + if (pin >= 24) pin -= 24; // allow for channel or pin numbers #else if (pin >= 14) pin -= 14; // allow for channel or pin numbers #endif -- cgit v1.2.3-18-g5258 From 56ddc4637de1b8b0e21840519ed0b94966395ff5 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Thu, 19 Apr 2012 15:46:32 -0400 Subject: bugfix for boards with a timer 4 but no channel D compilation failed for Mega because the COM4D1 and OCR4D registers are defined for 32U4 but not for Mega --- cores/arduino/wiring_analog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cores/arduino/wiring_analog.c') diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 40a3363..c06cad4 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -230,7 +230,7 @@ void analogWrite(uint8_t pin, int val) break; #endif - #if defined(TCCR4C) + #if defined(TCCR4C) && defined(COM4D1) case TIMER4D: // connect pwm to pin on timer 4, channel D sbi(TCCR4C, COM4D1); -- cgit v1.2.3-18-g5258 From 6ae1a1723561266767ed1ba39ae61c35ea263162 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Thu, 19 Apr 2012 15:52:16 -0400 Subject: bugfix for configuring PWM on D6 and D13 too early. (thanks to Limor Fried) was starting PWM on these pins too soon - in init() instead of when analogWrite() was called. as a result doing output on port registers directly failed. --- cores/arduino/wiring_analog.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'cores/arduino/wiring_analog.c') diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index c06cad4..0e9881f 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -210,6 +210,9 @@ void analogWrite(uint8_t pin, int val) case TIMER4A: //connect pwm to pin on timer 4, channel A sbi(TCCR4A, COM4A1); + #if defined(COM4A0) // only used on 32U4 + cbi(TCCR4A, COM4A0); + #endif OCR4A = val; // set pwm duty break; #endif @@ -234,6 +237,9 @@ void analogWrite(uint8_t pin, int val) case TIMER4D: // connect pwm to pin on timer 4, channel D sbi(TCCR4C, COM4D1); + #if defined(COM4D0) // only used on 32U4 + cbi(TCCR4C, COM4D0); + #endif OCR4D = val; // set pwm duty break; #endif -- cgit v1.2.3-18-g5258