author | Tero Marttila <terom@paivola.fi> |
Thu, 01 May 2014 23:55:53 +0300 | |
changeset 85 | ee05d89bde77 |
parent 61 | a960cf5981f7 |
permissions | -rw-r--r-- |
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 | 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 | 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 | 25 |
LKM_DATA_WRITE = 0b00000000, |
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 { |
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
64 |
LKM_DISPLAY_DECIMAL = 0b10000000, |
58
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 |
|
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
67 |
/* |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
68 |
* XXX: RED/GREEN somehow swapped vs reference; bug in our write code? |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
69 |
*/ |
59 | 70 |
enum { |
71 |
LKM_LED_OFF = 0b00, |
|
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
72 |
LKM_LED_GREEN = 0b01, |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
73 |
LKM_LED_RED = 0b10, |
59 | 74 |
LKM_LED_ORANGE = 0b11, |
75 |
}; |
|
76 |
||
77 |
/* |
|
78 |
* Button bitfields. |
|
79 |
* |
|
80 |
* The JY-LKM1638 uses K3 |
|
81 |
*/ |
|
82 |
enum { |
|
83 |
LKM_BUTTON_K1L = 0b00000100, |
|
84 |
LKM_BUTTON_K2L = 0b00000010, |
|
85 |
LKM_BUTTON_K3L = 0b00000001, |
|
86 |
||
87 |
LKM_BUTTON_K1H = 0b01000000, |
|
88 |
LKM_BUTTON_K2H = 0b00100000, |
|
89 |
LKM_BUTTON_K3H = 0b00010000, |
|
90 |
}; |
|
91 |
||
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
92 |
void lkm_init () |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
93 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
94 |
// strobe off: high output |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
95 |
// 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
|
96 |
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
|
97 |
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
|
98 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
99 |
// clock low |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
103 |
// data tri-state |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
104 |
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
|
105 |
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
|
106 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
107 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
108 |
/* |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
109 |
* 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
|
110 |
*/ |
59 | 111 |
void lkm_out () |
58
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 |
// clock low |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
114 |
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
|
115 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
116 |
// data out |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
117 |
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
|
118 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
119 |
// select on: low |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
120 |
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
|
121 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
122 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
123 |
/* |
59 | 124 |
* Select the LKM for read. |
125 |
*/ |
|
126 |
void lkm_in () |
|
127 |
{ |
|
128 |
// data in |
|
129 |
cbi(&LKM_DDR, LKM_DIO); |
|
130 |
cbi(&LKM_PORT, LKM_DIO); |
|
131 |
||
132 |
// select on: low |
|
133 |
cbi(&LKM_PORT, LKM_STB); |
|
134 |
||
135 |
// clock high |
|
136 |
sbi(&LKM_PORT, LKM_CLK); |
|
137 |
||
138 |
// pause |
|
139 |
_delay_us(1); |
|
140 |
} |
|
141 |
||
142 |
/* |
|
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
143 |
* Write out one byte |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
144 |
*/ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
145 |
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
|
146 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
147 |
char i; |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
148 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
149 |
for (i = 0; i < 8; i++) { |
59 | 150 |
// clock read: low |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
151 |
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
|
152 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
153 |
// set output |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
154 |
if (b & 1) |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
155 |
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
|
156 |
else |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
157 |
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
|
158 |
|
59 | 159 |
// clock write: high |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
160 |
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
|
161 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
162 |
// next bit |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
163 |
b >>= 1; |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
164 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
165 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
166 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
167 |
/* |
59 | 168 |
* Read in one byte. |
169 |
*/ |
|
170 |
byte lkm_read () |
|
171 |
{ |
|
172 |
byte b = 0; |
|
173 |
char i; |
|
174 |
||
175 |
// XXX: this loop is timing-critical; we must allow the signal to settle betwen clocks |
|
176 |
for (i = 0; i < 8; i++) { |
|
177 |
// next bit |
|
178 |
b >>= 1; |
|
179 |
||
180 |
// clock read: low |
|
181 |
cbi(&LKM_PORT, LKM_CLK); |
|
182 |
||
183 |
// pause |
|
184 |
_delay_us(1); |
|
185 |
||
186 |
// read input |
|
187 |
if (tbi(&LKM_PIN, LKM_DIO)) |
|
188 |
b |= 0x80; |
|
189 |
||
190 |
// pause |
|
191 |
_delay_us(1); |
|
192 |
||
193 |
// clock write: high |
|
194 |
sbi(&LKM_PORT, LKM_CLK); |
|
195 |
||
196 |
// pause |
|
197 |
_delay_us(1); |
|
198 |
} |
|
199 |
||
200 |
return b; |
|
201 |
} |
|
202 |
||
203 |
/* |
|
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
204 |
* End command. |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
205 |
*/ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
206 |
void lkm_end () |
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 |
// select off: high |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
209 |
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
|
210 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
211 |
// tristate data |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
212 |
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
|
213 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
214 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
215 |
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
|
216 |
{ |
59 | 217 |
lkm_out(); |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
218 |
lkm_write(cmd); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
219 |
lkm_end(); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
220 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
221 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
222 |
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
|
223 |
{ |
59 | 224 |
lkm_out(); |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
225 |
lkm_write(cmd); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
226 |
lkm_write(arg); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
227 |
lkm_end(); |
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 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
230 |
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
|
231 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
232 |
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
|
233 |
| (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
|
234 |
| (intensity & LKM_CONTROL_INTENSITY) |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
235 |
); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
236 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
237 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
238 |
/* |
59 | 239 |
* Blank display/LEDs |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
240 |
*/ |
59 | 241 |
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
|
242 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
243 |
char i; |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
244 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
245 |
lkm_cmd(LKM_CMD_DATA |
59 | 246 |
| LKM_DATA_TEST_NORMAL |
247 |
| 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
|
248 |
| LKM_DATA_WRITE |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
249 |
); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
250 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
251 |
// write out all 16 bytes of 0x00 |
59 | 252 |
lkm_out(); |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
253 |
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
|
254 |
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
|
255 |
lkm_write(0x00); |
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 |
lkm_end(); |
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 |
} |
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 |
/* |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
262 |
* 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
|
263 |
*/ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
264 |
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
|
265 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
266 |
lkm_cmd(LKM_CMD_DATA |
59 | 267 |
| LKM_DATA_TEST_NORMAL |
268 |
| 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
|
269 |
| LKM_DATA_WRITE |
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 |
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
|
272 |
value |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
273 |
); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
274 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
275 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
276 |
/* |
59 | 277 |
* Set raw output mask for given led 0..7 |
278 |
*/ |
|
279 |
static inline void lkm_led (byte led, byte value) |
|
280 |
{ |
|
281 |
lkm_cmd(LKM_CMD_DATA |
|
282 |
| LKM_DATA_TEST_NORMAL |
|
283 |
| LKM_DATA_ADDR_AUTO |
|
284 |
| LKM_DATA_WRITE |
|
285 |
); |
|
286 |
lkm_cmd1(LKM_CMD_ADDRESS | ((led * 2 + 1) & LKM_ADDRESS), |
|
287 |
value |
|
288 |
); |
|
289 |
} |
|
290 |
||
291 |
/* |
|
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
292 |
* 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
|
293 |
*/ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
294 |
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
|
295 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
296 |
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
|
297 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
298 |
|
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
299 |
static inline void lkm_display_dec (byte display, byte dec, byte decimal) |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
300 |
{ |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
301 |
byte raw; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
302 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
303 |
if (dec < 10) |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
304 |
raw = LKM_DISPLAY_FONT[dec]; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
305 |
else |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
306 |
raw = 0x00; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
307 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
308 |
if (decimal) |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
309 |
raw |= LKM_DISPLAY_DECIMAL; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
310 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
311 |
lkm_display(display, raw); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
312 |
} |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
313 |
|
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
314 |
/* |
59 | 315 |
* Read the 8-bit key states |
316 |
*/ |
|
317 |
byte lkm_buttons () |
|
318 |
{ |
|
319 |
byte k3 = 0; |
|
320 |
char i; |
|
321 |
||
322 |
lkm_out(); |
|
323 |
lkm_write(LKM_CMD_DATA |
|
324 |
| LKM_DATA_TEST_NORMAL |
|
325 |
| LKM_DATA_ADDR_AUTO |
|
326 |
| LKM_DATA_READ |
|
327 |
); |
|
328 |
lkm_in(); |
|
329 |
for (i = 0; i < 4; i++) { |
|
330 |
/* |
|
331 |
* The ordering of keys used is weird; it seems to go 04 15 26 37 |
|
332 |
*/ |
|
333 |
k3 |= lkm_read() << i; |
|
334 |
||
335 |
/* |
|
336 |
k3 >>= 1; |
|
337 |
||
338 |
byte b = lkm_read(); |
|
339 |
||
340 |
if (b & LKM_BUTTON_K3L) |
|
341 |
k3 |= 0b00001000; |
|
342 |
||
343 |
if (b & LKM_BUTTON_K3H) |
|
344 |
k3 |= 0b10000000; |
|
345 |
*/ |
|
346 |
} |
|
347 |
lkm_end(); |
|
348 |
||
349 |
return k3; |
|
350 |
} |
|
351 |
||
352 |
/* |
|
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
353 |
* Set 8-bit hexadecimal output on displays n..n+1 |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
354 |
*/ |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
355 |
void lkm_display_hh (byte display, byte hex) |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
356 |
{ |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
357 |
lkm_display_hex(display + 0, ((hex >> 4) & 0xF)); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
358 |
lkm_display_hex(display + 1, ((hex >> 0) & 0xF)); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
359 |
} |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
360 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
361 |
/* |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
362 |
* 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
|
363 |
*/ |
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
364 |
void lkm_display_hhhh (short hex) |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
365 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
371 |
|
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
372 |
/* |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
373 |
* Set 2-digit decimal output on displays n..n+1 |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
374 |
*/ |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
375 |
void lkm_display_dd (byte display, byte dec) |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
376 |
{ |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
377 |
byte d = dec / 10; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
378 |
byte u = dec % 10; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
379 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
380 |
byte c = d / 10; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
381 |
d = d % 10; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
382 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
383 |
lkm_display_dec(display + 0, d, c > 1); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
384 |
lkm_display_dec(display + 1, u, c > 0); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
385 |
} |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
386 |
|
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
387 |
// debug |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
388 |
#define DEBUG_DDR DDRB |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
389 |
#define DEBUG_PORT PORTB |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
390 |
#define DEBUG_LED 0 |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
391 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
392 |
int main (void) |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
393 |
{ |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
394 |
// led_init(); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
395 |
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
|
396 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
397 |
timer_init(); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
398 |
lkm_init(); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
399 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
400 |
sei(); |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
401 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
402 |
// setup display |
59 | 403 |
lkm_clear(); |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
404 |
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
|
405 |
|
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
406 |
/* |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
407 |
// test |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
408 |
lkm_led(0, LKM_LED_OFF); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
409 |
lkm_led(1, LKM_LED_RED); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
410 |
lkm_led(2, LKM_LED_GREEN); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
411 |
lkm_led(3, LKM_LED_ORANGE); |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
412 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
413 |
return 0; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
414 |
*/ |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
415 |
|
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
416 |
// start |
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
417 |
byte channels[4] = { }; |
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
418 |
enum channel_state { |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
419 |
CHANNEL_IDLE = 0, |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
420 |
CHANNEL_INC, |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
421 |
CHANNEL_DEC, |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
422 |
} channel_state[4] = { }; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
423 |
byte channel_count[4] = { }; |
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
424 |
char c; |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
425 |
|
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
426 |
while (true) { |
59 | 427 |
// scan input |
428 |
byte buttons = lkm_buttons(); |
|
429 |
||
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
430 |
for (c = 0; c < 4; c++) { |
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
431 |
enum channel_state state = channel_state[c], next; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
432 |
byte count = channel_count[c]; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
433 |
|
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
434 |
// decode buttons -> state |
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
435 |
if (buttons & 0b1) { |
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
436 |
next = CHANNEL_INC; |
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
437 |
} else if (buttons & 0b10) { |
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
438 |
next = CHANNEL_DEC; |
59 | 439 |
} else { |
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
440 |
next = CHANNEL_IDLE; |
59 | 441 |
} |
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
442 |
|
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
443 |
buttons >>= 2; |
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
444 |
|
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
445 |
// feedback |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
446 |
if (state == CHANNEL_INC || next == CHANNEL_INC) |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
447 |
lkm_led(c * 2 + 0, LKM_LED_GREEN); |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
448 |
else if (state == CHANNEL_DEC && next == CHANNEL_DEC) |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
449 |
lkm_led(c * 2 + 0, LKM_LED_RED); |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
450 |
else |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
451 |
lkm_led(c * 2 + 0, LKM_LED_OFF); |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
452 |
|
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
453 |
if (state == CHANNEL_DEC || next == CHANNEL_DEC) |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
454 |
lkm_led(c * 2 + 1, LKM_LED_RED); |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
455 |
else if (state == CHANNEL_INC && next == CHANNEL_INC) |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
456 |
lkm_led(c * 2 + 1, LKM_LED_GREEN); |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
457 |
else |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
458 |
lkm_led(c * 2 + 1, LKM_LED_OFF); |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
459 |
|
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
460 |
// counts |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
461 |
if ((channel_state[c] = next) != state) |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
462 |
channel_count[c] = 0; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
463 |
else |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
464 |
count = ++channel_count[c]; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
465 |
|
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
466 |
// state transitions |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
467 |
if (next == CHANNEL_INC && count > 0) { |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
468 |
channels[c] += count; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
469 |
} else if (next == CHANNEL_DEC && count > 0) { |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
470 |
channels[c] -= count; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
471 |
} else if (state == CHANNEL_INC && !count) { |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
472 |
channels[c] = 0xff; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
473 |
} else if (state == CHANNEL_DEC && !count) { |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
474 |
channels[c] = 0x00; |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
475 |
} |
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
476 |
|
60
b9648067e9d7
hello-lkm: simple 4-channel control
Tero Marttila <terom@paivola.fi>
parents:
59
diff
changeset
|
477 |
lkm_display_hh(c * 2, channels[c]); |
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
478 |
|
59 | 479 |
xbi(&DEBUG_PORT, DEBUG_LED); |
480 |
} |
|
481 |
||
61
a960cf5981f7
hello-lkm: more complicated control buttons
Tero Marttila <terom@paivola.fi>
parents:
60
diff
changeset
|
482 |
timer_sleep(8000); |
58
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
483 |
} |
a445e08b63e0
hello-lkm: Control the JY-LKM1638 LED 7-segment display module
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
484 |
} |