1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-20 21:01:25 +03:00

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.
This commit is contained in:
Zach Eveland
2011-12-11 19:56:50 -05:00
parent 7c90d9d8b5
commit 699315c359
5 changed files with 61 additions and 28 deletions

View File

@ -144,21 +144,21 @@ mega.build.variant=mega
############################################################## ##############################################################
#leonardo.name=Arduino Leonardo leonardo.name=Arduino Leonardo
#leonardo.upload.protocol=arduino leonardo.upload.protocol=arduino
#leonardo.upload.maximum_size=28672 leonardo.upload.maximum_size=28672
#leonardo.upload.speed=1200 leonardo.upload.speed=1200
#leonardo.bootloader.low_fuses=0xde leonardo.bootloader.low_fuses=0xde
#leonardo.bootloader.high_fuses=0xd8 leonardo.bootloader.high_fuses=0xd8
#leonardo.bootloader.extended_fuses=0xcb leonardo.bootloader.extended_fuses=0xcb
#leonardo.bootloader.path=diskloader leonardo.bootloader.path=diskloader
#leonardo.bootloader.file=DiskLoader-Leonardo.hex leonardo.bootloader.file=DiskLoader-Leonardo.hex
#leonardo.bootloader.unlock_bits=0x3F leonardo.bootloader.unlock_bits=0x3F
#leonardo.bootloader.lock_bits=0x2F leonardo.bootloader.lock_bits=0x2F
#leonardo.build.mcu=atmega32u4 leonardo.build.mcu=atmega32u4
#leonardo.build.f_cpu=16000000L leonardo.build.f_cpu=16000000L
#leonardo.build.core=arduino leonardo.build.core=arduino
#leonardo.build.variant=leonardo leonardo.build.variant=leonardo
############################################################## ##############################################################

9
hardware/arduino/cores/arduino/wiring.c Executable file → Normal file
View File

@ -279,11 +279,20 @@ void init()
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
#endif #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) #if defined(TCCR4B) && defined(CS41) && defined(WGM40)
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64 sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
sbi(TCCR4B, CS40); sbi(TCCR4B, CS40);
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
#endif #endif
#endif /* end timer4 block for ATMEGA1280/2560 and similar */
#if defined(TCCR5B) && defined(CS51) && defined(WGM50) #if defined(TCCR5B) && defined(CS51) && defined(WGM50)
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64 sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64

View File

@ -204,10 +204,14 @@ void analogWrite(uint8_t pin, int val)
break; break;
#endif #endif
#if defined(TCCR4A) && defined(COM4A1) #if defined(TCCR4A)
case TIMER4A: 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); sbi(TCCR4A, COM4A1);
#endif
OCR4A = val; // set pwm duty OCR4A = val; // set pwm duty
break; break;
#endif #endif
@ -228,14 +232,17 @@ void analogWrite(uint8_t pin, int val)
break; break;
#endif #endif
#if defined(TCCR4A) && defined(COM4D1) #if defined(TCCR4C)
case TIMER4D: case TIMER4D:
// connect pwm to pin on timer 4, channel D // connect pwm to pin on timer 4, channel D
sbi(TCCR4A, COM4D1); #if defined(PWM4D) /* ATMEGA32U4 and related */
sbi(TCCR4C, PWM4D);
#endif
OCR4D = val; // set pwm duty OCR4D = val; // set pwm duty
break; break;
#endif #endif
#if defined(TCCR5A) && defined(COM5A1) #if defined(TCCR5A) && defined(COM5A1)
case TIMER5A: case TIMER5A:
// connect pwm to pin on timer 5, channel A // connect pwm to pin on timer 5, channel A
@ -270,3 +277,4 @@ void analogWrite(uint8_t pin, int val)
} }
} }
} }

20
hardware/arduino/cores/arduino/wiring_digital.c Executable file → Normal file
View File

@ -105,15 +105,31 @@ static void turnOffPWM(uint8_t timer)
case TIMER3C: cbi(TCCR3A, COM3C1); break; case TIMER3C: cbi(TCCR3A, COM3C1); break;
#endif #endif
#if defined(TCCR4A) && defined(COM4A1) #if defined(TCCR4A)
case TIMER4A: cbi(TCCR4A, COM4A1); break; case TIMER4A:
#if defined(PWM4A)
cbi(TCCR4A, PWM4A);
#elif defined(COM4A1)
cbi(TCCR4A, COM4A1);
#endif #endif
break;
#endif
#if defined(TCCR4A) && defined(COM4B1) #if defined(TCCR4A) && defined(COM4B1)
case TIMER4B: cbi(TCCR4A, COM4B1); break; case TIMER4B: cbi(TCCR4A, COM4B1); break;
#endif #endif
#if defined(TCCR4A) && defined(COM4C1) #if defined(TCCR4A) && defined(COM4C1)
case TIMER4C: cbi(TCCR4A, COM4C1); break; case TIMER4C: cbi(TCCR4A, COM4C1); break;
#endif #endif
#if defined(TCCR4C)
case TIMER4D:
#if defined(PWM4D)
cbi(TCCR4C, PWM4D);
#endif
break;
#endif
#if defined(TCCR5A) #if defined(TCCR5A)
case TIMER5A: cbi(TCCR5A, COM5A1); break; case TIMER5A: cbi(TCCR5A, COM5A1); break;
case TIMER5B: cbi(TCCR5A, COM5B1); break; case TIMER5B: cbi(TCCR5A, COM5B1); break;

View File

@ -212,7 +212,7 @@ const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[30] = {
_BV(6), // D29 / D12 - A11 - PD6 _BV(6), // D29 / D12 - A11 - PD6
}; };
const uint8_t PROGMEM digital_pin_to_timer_PGM[18] = { const uint8_t PROGMEM digital_pin_to_timer_PGM[16] = {
NOT_ON_TIMER, NOT_ON_TIMER,
NOT_ON_TIMER, NOT_ON_TIMER,
NOT_ON_TIMER, NOT_ON_TIMER,