TMK Firmware with leds, HELP!!!

User avatar
Cortes

21 Jan 2017, 00:44

Hi people!, 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

User avatar
Ray

21 Jan 2017, 11:02

Google is pretty good in translating spanish to english it seems.

I have to ask: in which job do you learn to prototype like this?
I have been told my breadboards look very german, because everything was neat and angled, but your wireing is another level of tidy than mine.

You can PWM the anode or cathode of the led, really no difference there.
The PWM from the ATMEGA32U4 should switch between high and low output, so also no problem here.
So if you "invert" the duty-cycle, you can do 5V -> Resistor -> Led -> Pin B6 (PWM)

regarding the backlight.c, I'm not in the mood right now to look into it.

User avatar
Cortes

21 Jan 2017, 15:22

Ray wrote: I have to ask: in which job do you learn to prototype like this?
I have been told my breadboards look very german, because everything was neat and angled, but your wireing is another level of tidy than mine.
hahaha, I have been self-taught electronica, but in some things I am a perfectionist, this wiring wanted me to be as orderly as possible.

This is a photo from my previous project:

Image

I have been reading that as I now have my circuit, it would not work, since the pins of teensy are limited to a 20mA amperage, and I need much more than that to turn on 93 LEDs.

Also I have informed that with a mosfet could solve it, I will have to read more about them.

Matt_

21 Jan 2017, 15:28

Your wiring/soldering work is excellent, bravo.

I have no idea if it is possible to set up different backlight zones (keycaps & case) with TMK so I can't help you with the software. However, regarding actual wiring and using a mosfet, flabbergast made a good summary of how to do it with TMK here : workshop-f7/how-to-build-your-very-own- ... ml#p240050

User avatar
Cortes

07 Feb 2017, 23:53

Matt_ wrote: Your wiring/soldering work is excellent, bravo.

I have no idea if it is possible to set up different backlight zones (keycaps & case) with TMK so I can't help you with the software. However, regarding actual wiring and using a mosfet, flabbergast made a good summary of how to do it with TMK here : workshop-f7/how-to-build-your-very-own- ... ml#p240050
Thanks for your help, Gangolfus put a transistor to his circuit, but says a year later, that the transistors that put him on the keyboard burn after a few weeks of use: S
I'll have to look at more powerful transistors that live longer.

Now I need to know how to do the programming, which is where I'm most lost xD

Post Reply

Return to “Workshop”