added functionality for just a port

This commit is contained in:
Jack Humbert 2017-02-07 15:23:56 -05:00
parent d639bd2021
commit b4e30d3929
1 changed files with 62 additions and 32 deletions

View File

@ -594,34 +594,45 @@ static const uint8_t backlight_pin = BACKLIGHT_PIN;
# define COM1x1 COM1A1 # define COM1x1 COM1A1
# define OCR1x OCR1A # define OCR1x OCR1A
#else #else
# error "Backlight pin not supported - use B5, B6, or B7" # define NO_BACKLIGHT_CLOCK
#endif
#ifndef BACKLIGHT_ON_STATE
#define BACKLIGHT_ON_STATE 0
#endif #endif
__attribute__ ((weak)) __attribute__ ((weak))
void backlight_init_ports(void) void backlight_init_ports(void)
{ {
// Setup backlight pin as output and output low. // Setup backlight pin as output and output to on state.
// DDRx |= n // DDRx |= n
_SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF); _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
// PORTx &= ~n #if BACKLIGHT_ON_STATE == 0
_SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); // PORTx &= ~n
_SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
#else
// PORTx |= n
_SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
#endif
// Use full 16-bit resolution. #ifndef NO_BACKLIGHT_CLOCK
ICR1 = 0xFFFF; // Use full 16-bit resolution.
ICR1 = 0xFFFF;
// I could write a wall of text here to explain... but TL;DW // I could write a wall of text here to explain... but TL;DW
// Go read the ATmega32u4 datasheet. // Go read the ATmega32u4 datasheet.
// And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
// Pin PB7 = OCR1C (Timer 1, Channel C) // Pin PB7 = OCR1C (Timer 1, Channel C)
// Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
// (i.e. start high, go low when counter matches.) // (i.e. start high, go low when counter matches.)
// WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
// Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010; TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001; TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
#endif
backlight_init(); backlight_init();
#ifdef BACKLIGHT_BREATHING #ifdef BACKLIGHT_BREATHING
@ -633,24 +644,43 @@ __attribute__ ((weak))
void backlight_set(uint8_t level) void backlight_set(uint8_t level)
{ {
// Prevent backlight blink on lowest level // Prevent backlight blink on lowest level
// PORTx &= ~n #if BACKLIGHT_ON_STATE == 0
_SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF); // PORTx &= ~n
_SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
#else
// PORTx |= n
_SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
#endif
if ( level == 0 ) { if ( level == 0 ) {
// Turn off PWM control on backlight pin, revert to output low. #ifndef NO_BACKLIGHT_CLOCK
TCCR1A &= ~(_BV(COM1x1)); // Turn off PWM control on backlight pin, revert to output low.
OCR1x = 0x0; TCCR1A &= ~(_BV(COM1x1));
} else if ( level == BACKLIGHT_LEVELS ) { OCR1x = 0x0;
// Turn on PWM control of backlight pin #else
TCCR1A |= _BV(COM1x1); #if BACKLIGHT_ON_STATE == 0
// Set the brightness // PORTx |= n
OCR1x = 0xFFFF; _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
} else { #else
// Turn on PWM control of backlight pin // PORTx &= ~n
TCCR1A |= _BV(COM1x1); _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
// Set the brightness #endif
OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2)); #endif
} }
#ifndef NO_BACKLIGHT_CLOCK
else if ( level == BACKLIGHT_LEVELS ) {
// Turn on PWM control of backlight pin
TCCR1A |= _BV(COM1x1);
// Set the brightness
OCR1x = 0xFFFF;
}
else {
// Turn on PWM control of backlight pin
TCCR1A |= _BV(COM1x1);
// Set the brightness
OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
}
#endif
#ifdef BACKLIGHT_BREATHING #ifdef BACKLIGHT_BREATHING
breathing_intensity_default(); breathing_intensity_default();