TMK Firmware with leds, HELP!!!

User avatar
Cortes

20 Jan 2017, 17:24

Good afternoon, I came here asking for some help, because I'm totally lost with my personalized keyboard based on teensy 2.0.

First of all, I do not speak English, and I'm translating all this into Uncle google


The question is that this is my second keyboard based on Teensy 2.0, and the functional part of keyboard works correctly, but the question is that this second keyboard sends it backlight and I'm very lost with it.

To start I have to ask to see if I have the wiring right, which I do not have at all clear:

Pin B6 (PWM) -> Resistance -> Led -> Pin GND

Functional as well or pwm pines can not act as positive and change the circuit to something like this ?:

Pin + 5V -> Resistance -> Led -> Pin B6 (PWM)


My idea is to have 2 independent leds, backlight and leds case, and I would use pins B6 and B7

I have these parts of code backlight.c that gave me the user breh:

Code: Select all

    /*
    Copyright 2013 Mathias Andersson <wraul@dbox.se>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

    #include "backlight.h"
    #include "eeconfig.h"
    #include "debug.h"

    backlight_config_t backlight_config;

    void backlight_init(void)
    {
        //check signature
        if (!eeconfig_is_enabled()) {
            eeconfig_init();
        }
        backlight_config.raw = eeconfig_read_backlight();
        backlight_set(backlight_config.enable ? backlight_config.level : 0);
    }

    void backlight_increase(void)
    {
        if(backlight_config.level < BACKLIGHT_LEVELS)
        {
            backlight_config.level++;
            backlight_config.enable = 1;
            eeconfig_write_backlight(backlight_config.raw);
        }
        dprintf("backlight increase: %u\n", backlight_config.level);
        backlight_set(backlight_config.level);
    }

    void backlight_decrease(void)
    {
        if(backlight_config.level > 0)
        {
            backlight_config.level--;
            backlight_config.enable = !!backlight_config.level;
            eeconfig_write_backlight(backlight_config.raw);
        }
        dprintf("backlight decrease: %u\n", backlight_config.level);
        backlight_set(backlight_config.level);
    }

    void backlight_toggle(void)
    {
        backlight_config.enable ^= 1;
        eeconfig_write_backlight(backlight_config.raw);
        dprintf("backlight toggle: %u\n", backlight_config.enable);
        backlight_set(backlight_config.enable ? backlight_config.level : 0);
    }

    void backlight_step(void)
    {
        backlight_config.level++;
        if(backlight_config.level > BACKLIGHT_LEVELS)
        {
            backlight_config.level = 0;
        }
        backlight_config.enable = !!backlight_config.level;
        eeconfig_write_backlight(backlight_config.raw);
        dprintf("backlight step: %u\n", backlight_config.level);
        backlight_set(backlight_config.level);
    }

    void backlight_level(uint8_t level)
    {
        backlight_config.level ^= level;
        backlight_config.enable = !!backlight_config.level;
        eeconfig_write_backlight(backlight_config.raw);
        backlight_set(backlight_config.level);
    }

    // Plank Code for Backlight

    //#include <avr/io.h>
    //#include "backlight.h"

    #define CHANNEL OCR1B
    // Plank is OCR1C
    void backlight_init_ports()
    {

        // Setup PB7 as output and output low.
        DDRB |= (1<<6);
        PORTB &= ~(1<<6);
       
        // Use full 16-bit resolution.
        ICR1 = 0xFFFF;

        // I could write a wall of text here to explain... but TL;DW
        // Go read the ATmega32u4 datasheet.
        // 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 PB6 = OCR1B (Timer 1, Channel B)
        // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
       // Compare Output Mode = Clear on compare match, Channel C = COM1B1=1 COM1B0=0
        // (i.e. start high, go low when counter matches.)
        // 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
       
        TCCR1A = _BV(COM1B1) | _BV(WGM11); // = 0b00001010;
        TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;

        backlight_init();
    }

    void backlight_set(uint8_t level)
    {
        if ( level == 0 )
        {
            // Turn off PWM control on PB6, revert to output low.
            TCCR1A &= ~(_BV(COM1B1));
            CHANNEL = 0x0;
            // Prevent backlight blink on lowest level
            PORTB &= ~(_BV(PORTB6));
        }
        else if ( level == BACKLIGHT_LEVELS )
        {
            // Prevent backlight blink on lowest level
            PORTB &= ~(_BV(PORTB6));
            // Turn on PWM control of PB6
            TCCR1A |= _BV(COM1B1);
            // Set the brightness
            CHANNEL = 0xFFFF;
        }
        else       
        {
            // Prevent backlight blink on lowest level
            PORTB &= ~(_BV(PORTB6));
            // Turn on PWM control of PB6
            TCCR1A |= _BV(COM1B1);
            // Set the brightness
            CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
        }
    }
I want to do something like breh does on your numeric keypad, have specific keys (the ones above the arrows) for the regular backlight, power on / off / more brightness / less brightness, and in another layer to control the LEDs of the case.

Here also the complete keyboard configuration:

Https://dl.dropboxusercontent.com/u/5052546/Shiro.rar


Two pics :D

Image

Image

Post Reply

Return to “Keyboards”