1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
| #include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
static const uint8_t font5x7[][5] = {
{0x00,0x00,0x00,0x00,0x00}, // 32
{0x00,0x00,0x5F,0x00,0x00}, // 33 !
{0x00,0x07,0x00,0x07,0x00}, // 34 "
{0x14,0x7F,0x14,0x7F,0x14}, // 35 #
{0x24,0x2A,0x7F,0x2A,0x12}, // 36 $
{0x23,0x13,0x08,0x64,0x62}, // 37 %
{0x36,0x49,0x55,0x22,0x50}, // 38 &
{0x00,0x05,0x03,0x00,0x00}, // 39 '
{0x00,0x1C,0x22,0x41,0x00}, // 40 (
{0x00,0x41,0x22,0x1C,0x00}, // 41 )
{0x14,0x08,0x3E,0x08,0x14}, // 42 *
{0x08,0x08,0x3E,0x08,0x08}, // 43 +
{0x00,0x50,0x30,0x00,0x00}, // 44 ,
{0x08,0x08,0x08,0x08,0x08}, // 45 -
{0x00,0x60,0x60,0x00,0x00}, // 46 .
{0x20,0x10,0x08,0x04,0x02}, // 47 /
{0x3E,0x51,0x49,0x45,0x3E}, // 48 0
{0x00,0x42,0x7F,0x40,0x00}, // 49 1
{0x42,0x61,0x51,0x49,0x46}, // 50 2
{0x21,0x41,0x45,0x4B,0x31}, // 51 3
{0x18,0x14,0x12,0x7F,0x10}, // 52 4
{0x27,0x45,0x45,0x45,0x39}, // 53 5
{0x3C,0x4A,0x49,0x49,0x30}, // 54 6
{0x01,0x71,0x09,0x05,0x03}, // 55 7
{0x36,0x49,0x49,0x49,0x36}, // 56 8
{0x06,0x49,0x49,0x29,0x1E}, // 57 9
{0x00,0x36,0x36,0x00,0x00}, // 58 :
{0x00,0x56,0x36,0x00,0x00}, // 59 ;
{0x08,0x14,0x22,0x41,0x00}, // 60 <
{0x14,0x14,0x14,0x14,0x14}, // 61 =
{0x00,0x41,0x22,0x14,0x08}, // 62 >
{0x02,0x01,0x51,0x09,0x06}, // 63 ?
{0x32,0x49,0x79,0x41,0x3E}, // 64 @
{0x7E,0x11,0x11,0x11,0x7E}, // 65 A
{0x7F,0x49,0x49,0x49,0x36}, // 66 B
{0x3E,0x41,0x41,0x41,0x22}, // 67 C
{0x7F,0x41,0x41,0x22,0x1C}, // 68 D
{0x7F,0x49,0x49,0x49,0x41}, // 69 E
{0x7F,0x09,0x09,0x09,0x01}, // 70 F
{0x3E,0x41,0x49,0x49,0x7A}, // 71 G
{0x7F,0x08,0x08,0x08,0x7F}, // 72 H
{0x00,0x41,0x7F,0x41,0x00}, // 73 I
{0x20,0x40,0x41,0x3F,0x01}, // 74 J
{0x7F,0x08,0x14,0x22,0x41}, // 75 K
{0x7F,0x40,0x40,0x40,0x40}, // 76 L
{0x7F,0x02,0x0C,0x02,0x7F}, // 77 M
{0x7F,0x04,0x08,0x10,0x7F}, // 78 N
{0x3E,0x41,0x41,0x41,0x3E}, // 79 O
{0x7F,0x09,0x09,0x09,0x06}, // 80 P
{0x3E,0x41,0x51,0x21,0x5E}, // 81 Q
{0x7F,0x09,0x19,0x29,0x46}, // 82 R
{0x46,0x49,0x49,0x49,0x31}, // 83 S
{0x01,0x01,0x7F,0x01,0x01}, // 84 T
{0x3F,0x40,0x40,0x40,0x3F}, // 85 U
{0x1F,0x20,0x40,0x20,0x1F}, // 86 V
{0x3F,0x40,0x38,0x40,0x3F}, // 87 W
{0x63,0x14,0x08,0x14,0x63}, // 88 X
{0x07,0x08,0x70,0x08,0x07}, // 89 Y
{0x61,0x51,0x49,0x45,0x43}, // 90 Z
{0x00,0x7F,0x41,0x41,0x00}, // 91 [
{0x02,0x04,0x08,0x10,0x20}, // 92 '\'
{0x00,0x41,0x41,0x7F,0x00}, // 93 ]
{0x04,0x02,0x01,0x02,0x04}, // 94 ^
{0x40,0x40,0x40,0x40,0x40}, // 95 _
{0x00,0x01,0x02,0x04,0x00}, // 96 `
{0x20,0x54,0x54,0x54,0x78}, // 97 a
{0x7F,0x48,0x44,0x44,0x38}, // 98 b
{0x38,0x44,0x44,0x44,0x20}, // 99 c
{0x38,0x44,0x44,0x48,0x7F}, //100 d
{0x38,0x54,0x54,0x54,0x18}, //101 e
{0x08,0x7E,0x09,0x01,0x02}, //102 f
{0x0C,0x52,0x52,0x52,0x3E}, //103 g
{0x7F,0x08,0x04,0x04,0x78}, //104 h
{0x00,0x44,0x7D,0x40,0x00}, //105 i
{0x20,0x40,0x44,0x3D,0x00}, //106 j
{0x7F,0x10,0x28,0x44,0x00}, //107 k
{0x00,0x41,0x7F,0x40,0x00}, //108 l
{0x7C,0x04,0x18,0x04,0x78}, //109 m
{0x7C,0x08,0x04,0x04,0x78}, //110 n
{0x38,0x44,0x44,0x44,0x38}, //111 o
{0x7C,0x14,0x14,0x14,0x08}, //112 p
{0x08,0x14,0x14,0x18,0x7C}, //113 q
{0x7C,0x08,0x04,0x04,0x08}, //114 r
{0x48,0x54,0x54,0x54,0x20}, //115 s
{0x04,0x3F,0x44,0x40,0x20}, //116 t
{0x3C,0x40,0x40,0x20,0x7C}, //117 u
{0x1C,0x20,0x40,0x20,0x1C}, //118 v
{0x3C,0x40,0x30,0x40,0x3C}, //119 w
{0x44,0x28,0x10,0x28,0x44}, //120 x
{0x0C,0x50,0x50,0x50,0x3C}, //121 y
{0x44,0x64,0x54,0x4C,0x44}, //122 z
{0x00,0x08,0x36,0x41,0x00}, //123 {
{0x00,0x00,0x7F,0x00,0x00}, //124 |
{0x00,0x41,0x36,0x08,0x00}, //125 }
{0x08,0x04,0x08,0x10,0x08}, //126 ~
{0x00,0x00,0x00,0x00,0x00} //127
};
static inline void spi_init(void) {
PORTA.DIRSET = PIN4_bm | PIN6_bm | PIN7_bm;
PORTA.OUTSET = PIN7_bm;
PORTA.OUTCLR = PIN6_bm;
SPI0.CTRLA = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_CLK2X_bm | SPI_PRESC_DIV16_gc;
SPI0.CTRLB = SPI_MODE_0_gc;
}
static inline uint8_t spi_tx(uint8_t v) {
SPI0.DATA = v;
while(!(SPI0.INTFLAGS & SPI_IF_bm)) {;}
return SPI0.DATA;
}
static inline void display_cmd(uint8_t c) {
PORTA.OUTCLR = PIN2_bm;
PORTA.OUTCLR = PIN7_bm;
spi_tx(c);
PORTA.OUTSET = PIN7_bm;
}
static inline void display_data(uint8_t d) {
PORTA.OUTSET = PIN2_bm;
PORTA.OUTCLR = PIN7_bm;
spi_tx(d);
PORTA.OUTSET = PIN7_bm;
}
static inline void display_set_xy(uint8_t x, uint8_t y) {
display_cmd(0x80 | (x & 0x7F));
display_cmd(0x40 | (y & 0x07));
}
static void display_reset_gpio_init(void) {
PORTA.DIRSET = PIN1_bm | PIN2_bm;
PORTA.OUTSET = PIN1_bm;
_delay_ms(1);
PORTA.OUTCLR = PIN1_bm; _delay_ms(5);
PORTA.OUTSET = PIN1_bm; _delay_ms(5);
}
static void display_init(void) {
spi_init();
display_reset_gpio_init();
display_cmd(0x21);
display_cmd(0xB1);
display_cmd(0x04);
display_cmd(0x14);
display_cmd(0x20);
display_cmd(0x0C);
}
static void display_clear(void) {
display_set_xy(0,0);
PORTA.OUTSET = PIN2_bm;
PORTA.OUTCLR = PIN7_bm;
for(uint16_t i=0;i<504;i++)
spi_tx(0x00);
PORTA.OUTSET = PIN7_bm;
}
static void display_char(char c) {
if (c < 32 || (uint8_t)c > 127)
c = '?';
const uint8_t *g = font5x7[c - 32];
PORTA.OUTSET = PIN2_bm;
PORTA.OUTCLR = PIN7_bm;
for (uint8_t i=0;i<5;i++)
spi_tx(g[i]);
spi_tx(0x00);
PORTA.OUTSET = PIN7_bm;
}
static void display_print(const char *s) {
while(*s)
display_char(*s++);
}
int main(void) {
display_init();
display_clear();
display_set_xy(0,0);
display_print("PCD8544");
while(1){}
}
|