# HG changeset patch # User Jacob Alexander # Date 1438491583 25200 # Node ID 38395a57cf52f6db98ad869a890c431c84e05755 # Parent b02fc17eb0279076fa01307d6f1a54f632082daf Adding 16-bit brightness control to LCD backlight diff -r b02fc17eb027 -r 38395a57cf52 Scan/STLcd/capabilities.kll --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Scan/STLcd/capabilities.kll Sat Aug 01 21:59:43 2015 -0700 @@ -0,0 +1,44 @@ +Name = STLcdCapabilities; +Version = 0.1; +Author = "HaaTa (Jacob Alexander) 2015"; +KLL = 0.3c; + +# Modified Date +Date = 2015-08-01; + +# Defines available to the STLcd sub-module + +# PWM Prescalar +# In general this can be left alone +# A higher prescalar should be lower power usage (less switching) +# However, flickering may be noticeable. +# +# Here's a calculation table valid at a 72 MHz system clock +# Prescalars range from 0 to 7 (1 to 128) +# 0 - 72 MHz - Highest power usage/best result +# 1 - 36 MHz +# 2 - 18 MHz +# 3 - 9 MHz - Slightly visible flicker (peripheral vision) +# 4 - 4 500 kHz - Visible flickering +# 5 - 2 250 kHz +# 6 - 1 125 kHz +# 7 - 562 500 Hz +# Defaulting to no prescalar, looks the best +STLcdBacklightPrescalar => STLcdBacklightPrescalar_define; +STLcdBacklightPrescalar = 0; + +# Default Backlight Channel Brightness +# There are 3 channels, RGB +# In order to get other colors you must mix the 3 colors +# Each channel is a 16-bit register (65536 levels) +# Technically, this means, the backlight is a 48-bit RGB pixel +# In practice, it may be difficult to get color consistency at times if too bright or too dim +STLcdBacklightRed => STLcdBacklightRed_define; +STLcdBacklightGreen => STLcdBacklightGreen_define; +STLcdBacklightBlue => STLcdBacklightBlue_define; + +# Defaults to 6% brightness, white +STLcdBacklightRed = 0xFFF; +STLcdBacklightGreen = 0xFFF; +STLcdBacklightBlue = 0xFFF; + diff -r b02fc17eb027 -r 38395a57cf52 Scan/STLcd/lcd_scan.c --- a/Scan/STLcd/lcd_scan.c Sat Aug 01 18:26:04 2015 -0700 +++ b/Scan/STLcd/lcd_scan.c Sat Aug 01 21:59:43 2015 -0700 @@ -21,6 +21,7 @@ // Project Includes #include +#include #include #include @@ -276,16 +277,49 @@ // Setup Backlight // TODO Expose default settings - // TODO Setup PWM - GPIOC_PDDR |= (1<<1); - PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PCOR |= (1<<1); - GPIOC_PDDR |= (1<<2); - PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PCOR |= (1<<2); - GPIOC_PDDR |= (1<<3); - PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); - GPIOC_PCOR |= (1<<3); + SIM_SCGC6 |= SIM_SCGC6_FTM0; + FTM0_CNT = 0; // Reset counter + + // PWM Period + // 16-bit maximum + FTM0_MOD = 0xFFFF; + + // Set FTM to PWM output - Edge Aligned, Low-true pulses + FTM0_C0SC = 0x24; // MSnB:MSnA = 10, ELSnB:ELSnA = 01 + FTM0_C1SC = 0x24; + FTM0_C2SC = 0x24; + + // Base FTM clock selection (72 MHz system clock) + // Pre-scalar calculations + // 0 - 72 MHz - Highest power usage/best result + // 1 - 36 MHz + // 2 - 18 MHz + // 3 - 9 MHz - Slightly visible flicker (peripheral vision) + // 4 - 4 500 kHz - Visible flickering + // 5 - 2 250 kHz + // 6 - 1 125 kHz + // 7 - 562 500 Hz + // System clock, /w prescalar setting + FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS( STLcdBacklightPrescalar_define ); + + /* Write frequency TODO API + FTM0_SC = 0; + FTM0_CNT = 0; + FTM0_MOD = mod; + FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale); + */ + + // Red + FTM0_C0V = STLcdBacklightRed_define; + PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4); + + // Green + FTM0_C1V = STLcdBacklightGreen_define; + PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4); + + // Blue + FTM0_C2V = STLcdBacklightBlue_define; + PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(4); }