src/hello-lkm.c
author Tero Marttila <terom@paivola.fi>
Sat, 05 Apr 2014 01:50:26 +0300
changeset 59 7090f61e5e17
parent 58 a445e08b63e0
child 60 b9648067e9d7
permissions -rw-r--r--
hello-lkm: buttons
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
/*
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     2
 * Control the JY-LKM1638 LED display module.
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
 */
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
#include <avr/io.h>
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
     6
#include <util/delay.h>
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     8
#include "stdlib.h"
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    10
// XXX
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
#include "timer.c"
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    13
#define LKM_DDR     DDRC
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    14
#define LKM_PORT    PORTC
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    15
#define LKM_PIN     PINC
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    16
#define LKM_CLK     0
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    17
#define LKM_DIO     1
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    18
#define LKM_STB     2
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    19
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    20
enum lkm_cmd {
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    21
    LKM_CMD_DATA    = 0b01000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    22
    LKM_CMD_CONTROL = 0b10000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    23
    LKM_CMD_ADDRESS = 0b11000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    24
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    25
    LKM_DATA_WRITE          = 0b00000000,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    26
    LKM_DATA_READ           = 0b00000010,
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    27
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    28
    LKM_DATA_ADDR_AUTO      = 0b00000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    29
    LKM_DATA_ADDR_FIXED     = 0b00000100,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    30
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    31
    LKM_DATA_TEST_NORMAL    = 0b00000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
    LKM_DATA_TEST_TEST      = 0b00001000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
    LKM_ADDRESS             = 0b00001111,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
    
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
    LKM_CONTROL_INTENSITY       = 0b00000111,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
    LKM_CONTROL_INTENSITY_MIN   = 0b00000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
    LKM_CONTROL_INTENSITY_MAX   = 0b00000111,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    40
    LKM_CONTROL_DISPLAY_OFF = 0b00000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
    LKM_CONTROL_DISPLAY_ON  = 0b00001000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    42
};
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    44
static const uint8_t LKM_DISPLAY_FONT[] = {
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    45
    0b00111111,	// 0
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    46
    0b00000110,	// 1
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
    0b01011011,	// 2
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    48
    0b01001111,	// 3
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    49
    0b01100110,	// 4
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    50
    0b01101101,	// 5
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    51
    0b01111101,	// 6
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    52
    0b00000111,	// 7
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    53
    0b01111111,	// 8
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    54
    0b01100111,	// 9
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    55
    0b01110111,	// A
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    56
    0b01111100,	// B
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    57
    0b00111001,	// C
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    58
    0b01011110,	// D
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    59
    0b01111001,	// E
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    60
    0b01110001,	// F
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    61
};
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    62
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    63
enum {
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    64
    LKM_DISPLAY_DOT     = 0b10000000,
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    65
};
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    66
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    67
enum {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    68
    LKM_LED_OFF     = 0b00,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    69
    LKM_LED_RED     = 0b01,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    70
    LKM_LED_GREEN   = 0b10,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    71
    LKM_LED_ORANGE  = 0b11,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    72
};
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    73
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    74
/*
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    75
 * Button bitfields.
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    76
 *
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    77
 * The JY-LKM1638 uses K3
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    78
 */
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    79
enum {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    80
    LKM_BUTTON_K1L  = 0b00000100,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    81
    LKM_BUTTON_K2L  = 0b00000010,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    82
    LKM_BUTTON_K3L  = 0b00000001,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    83
    
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    84
    LKM_BUTTON_K1H  = 0b01000000,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    85
    LKM_BUTTON_K2H  = 0b00100000,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    86
    LKM_BUTTON_K3H  = 0b00010000,
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    87
};
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
    88
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    89
void lkm_init ()
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    90
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    91
    // strobe off: high output
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    92
    // XXX: should use an external pull-up resistor?
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    93
    sbi(&LKM_PORT, LKM_STB);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    94
    sbi(&LKM_DDR, LKM_STB);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    95
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    96
    // clock low
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    97
    sbi(&LKM_DDR, LKM_CLK);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    98
    cbi(&LKM_PORT, LKM_CLK);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    99
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   100
    // data tri-state
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   101
    cbi(&LKM_DDR, LKM_DIO);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   102
    cbi(&LKM_DDR, LKM_DIO);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   103
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   104
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   105
/*
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   106
 * Select the LKM for write.
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   107
 */
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   108
void lkm_out ()
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   109
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   110
    // clock low
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   111
    cbi(&LKM_PORT, LKM_CLK);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   112
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   113
    // data out
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   114
    sbi(&LKM_DDR, LKM_DIO);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   115
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   116
    // select on: low
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   117
    cbi(&LKM_PORT, LKM_STB);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   118
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   119
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   120
/*
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   121
 * Select the LKM for read.
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   122
 */
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   123
void lkm_in ()
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   124
{
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   125
    // data in
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   126
    cbi(&LKM_DDR, LKM_DIO);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   127
    cbi(&LKM_PORT, LKM_DIO);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   128
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   129
    // select on: low
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   130
    cbi(&LKM_PORT, LKM_STB);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   131
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   132
    // clock high
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   133
    sbi(&LKM_PORT, LKM_CLK);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   134
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   135
    // pause
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   136
    _delay_us(1);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   137
}
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   138
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   139
/*
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   140
 * Write out one byte
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   141
 */
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   142
void lkm_write (byte b)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   143
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   144
    char i;
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   145
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   146
    for (i = 0; i < 8; i++) {
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   147
        // clock read: low
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   148
        cbi(&LKM_PORT, LKM_CLK);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   149
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   150
        // set output
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   151
        if (b & 1)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   152
            sbi(&LKM_PORT, LKM_DIO);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   153
        else 
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   154
            cbi(&LKM_PORT, LKM_DIO);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   155
        
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   156
        // clock write: high
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   157
        sbi(&LKM_PORT, LKM_CLK);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   158
        
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   159
        // next bit
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   160
        b >>= 1;
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   161
    }
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   162
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   163
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   164
/*
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   165
 * Read in one byte.
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   166
 */
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   167
byte lkm_read ()
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   168
{
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   169
    byte b = 0;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   170
    char i;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   171
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   172
    // XXX: this loop is timing-critical; we must allow the signal to settle betwen clocks
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   173
    for (i = 0; i < 8; i++) {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   174
        // next bit
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   175
        b >>= 1;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   176
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   177
        // clock read: low
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   178
        cbi(&LKM_PORT, LKM_CLK);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   179
    
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   180
        // pause
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   181
        _delay_us(1);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   182
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   183
        // read input
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   184
        if (tbi(&LKM_PIN, LKM_DIO))
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   185
            b |= 0x80;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   186
        
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   187
        // pause
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   188
        _delay_us(1);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   189
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   190
        // clock write: high
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   191
        sbi(&LKM_PORT, LKM_CLK);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   192
        
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   193
        // pause
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   194
        _delay_us(1);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   195
    }
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   196
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   197
    return b;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   198
}
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   199
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   200
/*
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   201
 * End command.
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   202
 */
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   203
void lkm_end ()
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   204
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   205
    // select off: high
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   206
    sbi(&LKM_PORT, LKM_STB);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   207
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   208
    // tristate data
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   209
    cbi(&LKM_DDR, LKM_DIO);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   210
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   211
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   212
void lkm_cmd (byte cmd)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   213
{
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   214
    lkm_out();
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   215
    lkm_write(cmd);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   216
    lkm_end();
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   217
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   218
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   219
void lkm_cmd1 (byte cmd, byte arg)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   220
{
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   221
    lkm_out();
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   222
    lkm_write(cmd);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   223
    lkm_write(arg);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   224
    lkm_end();
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   225
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   226
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   227
static inline void lkm_control (byte display, byte intensity)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   228
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   229
    lkm_cmd(LKM_CMD_CONTROL
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   230
        |   (display ? LKM_CONTROL_DISPLAY_ON : LKM_CONTROL_DISPLAY_OFF)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   231
        |   (intensity & LKM_CONTROL_INTENSITY)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   232
    );
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   233
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   234
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   235
/*
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   236
 * Blank display/LEDs
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   237
 */
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   238
static void lkm_clear ()
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   239
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   240
    char i;
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   241
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   242
    lkm_cmd(LKM_CMD_DATA
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   243
        |   LKM_DATA_TEST_NORMAL
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   244
        |   LKM_DATA_ADDR_AUTO
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   245
        |   LKM_DATA_WRITE
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   246
    );
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   247
    
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   248
    // write out all 16 bytes of 0x00
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   249
    lkm_out();
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   250
    lkm_write(LKM_CMD_ADDRESS | (0x0) & LKM_ADDRESS);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   251
    for (i = 0; i < 16; i++) {
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   252
        lkm_write(0x00);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   253
    }
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   254
    lkm_end();
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   255
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   256
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   257
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   258
/*
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   259
 * Set raw output mask for given display 0..7
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   260
 */
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   261
static inline void lkm_display (byte display, byte value)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   262
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   263
    lkm_cmd(LKM_CMD_DATA
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   264
        |   LKM_DATA_TEST_NORMAL
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   265
        |   LKM_DATA_ADDR_AUTO
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   266
        |   LKM_DATA_WRITE
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   267
    );
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   268
    lkm_cmd1(LKM_CMD_ADDRESS | ((display * 2 + 0) & LKM_ADDRESS),
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   269
        value
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   270
    );
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   271
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   272
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   273
/*
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   274
 * Set raw output mask for given led 0..7
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   275
 */
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   276
static inline void lkm_led (byte led, byte value)
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   277
{
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   278
    lkm_cmd(LKM_CMD_DATA
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   279
        |   LKM_DATA_TEST_NORMAL
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   280
        |   LKM_DATA_ADDR_AUTO
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   281
        |   LKM_DATA_WRITE
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   282
    );
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   283
    lkm_cmd1(LKM_CMD_ADDRESS | ((led * 2 + 1) & LKM_ADDRESS),
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   284
        value
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   285
    );
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   286
}
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   287
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   288
/*
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   289
 * Set 4-bit hexadecimal output for given display 0..7
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   290
 */
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   291
static inline void lkm_display_hex (byte display, byte hex)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   292
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   293
    lkm_display(display, LKM_DISPLAY_FONT[hex]);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   294
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   295
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   296
/*
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   297
 * Read the 8-bit key states
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   298
 */
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   299
byte lkm_buttons ()
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   300
{
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   301
    byte k3 = 0;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   302
    char i;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   303
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   304
    lkm_out();
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   305
    lkm_write(LKM_CMD_DATA
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   306
        |   LKM_DATA_TEST_NORMAL
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   307
        |   LKM_DATA_ADDR_AUTO
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   308
        |   LKM_DATA_READ
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   309
    );
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   310
    lkm_in();
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   311
    for (i = 0; i < 4; i++) {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   312
        /*
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   313
         * The ordering of keys used is weird; it seems to go 04 15 26 37
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   314
         */
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   315
        k3 |= lkm_read() << i;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   316
        
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   317
        /*
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   318
        k3 >>= 1;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   319
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   320
        byte b = lkm_read();
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   321
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   322
        if (b & LKM_BUTTON_K3L)
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   323
            k3 |= 0b00001000;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   324
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   325
        if (b & LKM_BUTTON_K3H)
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   326
            k3 |= 0b10000000;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   327
        */
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   328
    }
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   329
    lkm_end();
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   330
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   331
    return k3;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   332
}
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   333
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   334
/*
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   335
 * Set 16-bit hexadecimal output on displays 4..7
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   336
 */
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   337
void lkm_display_hex16 (short hex)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   338
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   339
    lkm_display_hex(7, ((hex >> 0) & 0xF));
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   340
    lkm_display_hex(6, ((hex >> 4) & 0xF));
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   341
    lkm_display_hex(5, ((hex >> 8) & 0xF));
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   342
    lkm_display_hex(4, ((hex >> 12) & 0xF));
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   343
}
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   344
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   345
// debug
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   346
#define DEBUG_DDR   DDRB
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   347
#define DEBUG_PORT  PORTB
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   348
#define DEBUG_LED   0
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   349
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   350
int main (void)
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   351
{
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   352
    // led_init();
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   353
    sbi(&DEBUG_DDR, DEBUG_LED);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   354
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   355
    timer_init();
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   356
    lkm_init();
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   357
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   358
    sei();
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   359
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   360
    // setup display
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   361
    lkm_clear();
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   362
    lkm_control(LKM_CONTROL_DISPLAY_ON, LKM_CONTROL_INTENSITY_MAX);
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   363
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   364
    // start
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   365
    byte state[8] = { };
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   366
    char i;
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   367
        
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   368
    while (true) {
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   369
        // scan input
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   370
        byte buttons = lkm_buttons();
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   371
        
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   372
        for (i = 0; i < 8; i++) {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   373
            if (!(buttons & (1 << i))) {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   374
                continue;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   375
            } else if (state[i] >= 0xF) {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   376
                state[i] = 0;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   377
            } else {
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   378
                state[i]++;
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   379
            }
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   380
59
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   381
            lkm_display_hex(i, state[i]);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   382
            lkm_led(i, state[i]);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   383
            
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   384
            xbi(&DEBUG_PORT, DEBUG_LED);
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   385
        }
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   386
7090f61e5e17 hello-lkm: buttons
Tero Marttila <terom@paivola.fi>
parents: 58
diff changeset
   387
        timer_sleep(16000);
58
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   388
    }
a445e08b63e0 hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   389
}