display.c 6.03 KB
Newer Older
Petter Breedveld's avatar
Petter Breedveld committed
1
2
3
4
5
6
7
//
// display.c
//

#define F_CPU 3333333UL


Petter Breedveld's avatar
Petter Breedveld committed
8
9
#define DSP_MODE_bm 1<<5 
#define DSP_MODE_PORT PORTC
Petter Breedveld's avatar
Petter Breedveld committed
10

Petter Breedveld's avatar
Petter Breedveld committed
11
12
#define DSP_RST_bm 1<<5 
#define DSP_RST_PORT PORTB
Petter Breedveld's avatar
Petter Breedveld committed
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


// System headers
#include <avr/io.h>
#include <util/delay.h>
#include <stdbool.h>

// Project headers
#include "display.h"
#include "spi.h"
#include "font8x8.h"


// This file contains a simple driver for the display so that one can print characters and strings.
// You shouldn't need to do anything in this file for things to work.
// Of course, you're welcome to check it out if you want to see how things work under the hood.

void DISP_rst();
void DISP_transmit_cmd(uint8_t cmd);
void DISP_transmit_data(uint8_t data);
void DISP_set_mode(bool mode);

int cursor_row = 0;
int cursor_col = 0;

void DISP_print(const char* str)
{
40
41
42
43
44
	while(*str != '\0')
	{
		DISP_putc(*str);
		str++;
	}
Petter Breedveld's avatar
Petter Breedveld committed
45
46
47
48
49
}

void DISP_putc(char c)
{

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	if(c == '\n')
	{
		if(cursor_col == 0)
		{
			DISP_putc(' ');
		}

		while(cursor_col != 0)
		{
			DISP_putc(' ');
		}
		return;
	}

	else if(c == '\t')
	{
		int count = 3 - (cursor_col % 4);

		for(int i = 0; i < count; i++)
		{
			DISP_putc(' ');
		}
		return;
	}
Petter Breedveld's avatar
Petter Breedveld committed
74
	
75
76
77
78
79
80
	else if(c < 0x20)
	{
		return;
	}
	else if(c < 0x7F)
	{
Petter Breedveld's avatar
Petter Breedveld committed
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

		
		uint8_t d[8] = {
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][0])),
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][1])),
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][2])),
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][3])),
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][4])),
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][5])),
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][6])),
			pgm_read_byte(&(font8x8_basic[(int)c - 0x20][7]))
		};
		
		for(uint8_t b = 0; b < 8; b++)
		{
			uint8_t db = 0;
			db |= (((d[0] >> b) & 0x1) << 0);
			db |= (((d[1] >> b) & 0x1) << 1);
			db |= (((d[2] >> b) & 0x1) << 2);
			db |= (((d[3] >> b) & 0x1) << 3);
			db |= (((d[4] >> b) & 0x1) << 4);
			db |= (((d[5] >> b) & 0x1) << 5);
			db |= (((d[6] >> b) & 0x1) << 6);
			db |= (((d[7] >> b) & 0x1) << 7);

			DISP_transmit_data(db);
		}
108
109
110
111
112
113
114
	}
	else if(c < 0xA0)
	{
		return;
	}
	else if(c > 0xC0)
	{
Petter Breedveld's avatar
Petter Breedveld committed
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

		
		uint8_t d[8] = {
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][0])),
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][1])),
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][2])),
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][3])),
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][4])),
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][5])),
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][6])),
			pgm_read_byte(&(font8x8_ext_latin[(int)c - 0xc0][7]))
		};
		
		for(uint8_t b = 0; b < 8; b++)
		{
			uint8_t db = 0;
			db |= (((d[0] >> b) & 0x1) << 0);
			db |= (((d[1] >> b) & 0x1) << 1);
			db |= (((d[2] >> b) & 0x1) << 2);
			db |= (((d[3] >> b) & 0x1) << 3);
			db |= (((d[4] >> b) & 0x1) << 4);
			db |= (((d[5] >> b) & 0x1) << 5);
			db |= (((d[6] >> b) & 0x1) << 6);
			db |= (((d[7] >> b) & 0x1) << 7);

			DISP_transmit_data(db);
		}
142
	}
Petter Breedveld's avatar
Petter Breedveld committed
143

144
	
Petter Breedveld's avatar
Petter Breedveld committed
145

146
147
	cursor_col++;
	cursor_col %= 16;
Petter Breedveld's avatar
Petter Breedveld committed
148

149
150
151
152
153
	if(cursor_col == 0)
	{
		cursor_row++;
		cursor_row %= 4;
	}
Petter Breedveld's avatar
Petter Breedveld committed
154
155
156
157
}

void DISP_clear()
{
158
159
160
161
162
163
164
165
166
167
168
169
170
	if(cursor_row == 0)
	{
		DISP_print("\n");
	}
	while(cursor_row != 0)
	{
		DISP_print("\n");
	}

	for(int i = 0; i < 4 * 16; i++)
	{
		DISP_putc(' ');
	}
Petter Breedveld's avatar
Petter Breedveld committed
171
172
173
174
175
}

void DISP_write_bitmap(const uint8_t* bm)
{
	
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
	for(uint8_t y = 0; y < 4; y++)
	{
		for(uint8_t x = 0; x < 16; x++)
		{
			uint8_t d[8] = {
				pgm_read_byte(&(bm[(8 * y + 0) * 16 + x])),
				pgm_read_byte(&(bm[(8 * y + 1) * 16 + x])),
				pgm_read_byte(&(bm[(8 * y + 2) * 16 + x])),
				pgm_read_byte(&(bm[(8 * y + 3) * 16 + x])),
				pgm_read_byte(&(bm[(8 * y + 4) * 16 + x])),
				pgm_read_byte(&(bm[(8 * y + 5) * 16 + x])),
				pgm_read_byte(&(bm[(8 * y + 6) * 16 + x])),
				pgm_read_byte(&(bm[(8 * y + 7) * 16 + x]))
			};

			for(uint8_t b = 0; b < 8; b++)
			{
				uint8_t db = 0;
				db |= (((d[0] >> b) & 0x1) << 0);
				db |= (((d[1] >> b) & 0x1) << 1);
				db |= (((d[2] >> b) & 0x1) << 2);
				db |= (((d[3] >> b) & 0x1) << 3);
				db |= (((d[4] >> b) & 0x1) << 4);
				db |= (((d[5] >> b) & 0x1) << 5);
				db |= (((d[6] >> b) & 0x1) << 6);
				db |= (((d[7] >> b) & 0x1) << 7);

				DISP_transmit_data(db);
			}
		}
	}
Petter Breedveld's avatar
Petter Breedveld committed
207
208
209
210
}

void DISP_transmit_cmd(uint8_t cmd)
{
211
212
	DISP_set_mode(0);
	SPI_MasterTransmit(cmd);
Petter Breedveld's avatar
Petter Breedveld committed
213
214
215
216
}

void DISP_transmit_data(uint8_t data)
{
217
218
	DISP_set_mode(1);
	SPI_MasterTransmit(data);
Petter Breedveld's avatar
Petter Breedveld committed
219
220
221
222
223
}

// mode: 1 = data, 0 = cmd
void DISP_set_mode(bool mode)
{
224
225
226
227
228
229
230
231
232
	if(mode)
	{
		DSP_MODE_PORT.OUTSET = DSP_MODE_bm;
	}
	else
	{
		DSP_MODE_PORT.OUTCLR = DSP_MODE_bm;

	}
Petter Breedveld's avatar
Petter Breedveld committed
233
234
235
236
}

void DISP_init()
{
237
238
	DSP_RST_PORT.DIRSET = DSP_RST_bm;
	DSP_MODE_PORT.DIRSET = DSP_MODE_bm;
Petter Breedveld's avatar
Petter Breedveld committed
239

240
	SPI_MasterInit();
Petter Breedveld's avatar
Petter Breedveld committed
241

242
	DISP_set_mode(1);
Petter Breedveld's avatar
Petter Breedveld committed
243

244
	DISP_rst();
Petter Breedveld's avatar
Petter Breedveld committed
245

246
	DISP_transmit_cmd(0xAE); // display off
Petter Breedveld's avatar
Petter Breedveld committed
247

248
	// p32
Petter Breedveld's avatar
Petter Breedveld committed
249
250
251
	DISP_transmit_cmd(0xD5); // clock
	DISP_transmit_cmd(0xF0); // upper nibble is rate, lower nibble is divisor

252
	// p31
Petter Breedveld's avatar
Petter Breedveld committed
253
254
255
	DISP_transmit_cmd(0xA8); // mux ratio p31
	DISP_transmit_cmd(0x20); // rtfm

256
	// p31
Petter Breedveld's avatar
Petter Breedveld committed
257
258
259
	DISP_transmit_cmd(0xD3); // display offset
	DISP_transmit_cmd(0x00); // rtfm

260
	// p3 (appendix)
Petter Breedveld's avatar
Petter Breedveld committed
261
262
263
	DISP_transmit_cmd(0x8D); // charge pump
	DISP_transmit_cmd(0x14); // enable

264
	// p30, p34
Petter Breedveld's avatar
Petter Breedveld committed
265
266
267
	DISP_transmit_cmd(0x20); // memory addr mode
	DISP_transmit_cmd(0x00); // horizontal

268
	// p31
Petter Breedveld's avatar
Petter Breedveld committed
269
270
271
	DISP_transmit_cmd(0xA1); // segment remap
	DISP_transmit_cmd(0xA5); // display on

272
	// p31
Petter Breedveld's avatar
Petter Breedveld committed
273
274
	DISP_transmit_cmd(0xC8); // com scan direction

275
	// p31
Petter Breedveld's avatar
Petter Breedveld committed
276
277
	DISP_transmit_cmd(0x7F); // display start line

278
	// p31
Petter Breedveld's avatar
Petter Breedveld committed
279
280
281
282
	DISP_transmit_cmd(0x22); // set page address
	DISP_transmit_cmd(0x00);
	DISP_transmit_cmd(0x03);

283
	// p31, p40
Petter Breedveld's avatar
Petter Breedveld committed
284
285
286
	DISP_transmit_cmd(0xDA); // com hardware cfg
	DISP_transmit_cmd(0x02); // com cfg

287
	// p28
Petter Breedveld's avatar
Petter Breedveld committed
288
289
290
	DISP_transmit_cmd(0x81); // contrast aka current
	DISP_transmit_cmd(0xFF); // 128 is midpoint

291
	// p32
Petter Breedveld's avatar
Petter Breedveld committed
292
293
294
	DISP_transmit_cmd(0xD9); // precharge
	DISP_transmit_cmd(0x11); // rtfm

295
	// p32
Petter Breedveld's avatar
Petter Breedveld committed
296
297
298
299
300
301
302
	DISP_transmit_cmd(0xDB); // vcomh deselect level
	DISP_transmit_cmd(0x20); // rtfm

	DISP_transmit_cmd(0xA6); // non-inverted
	DISP_transmit_cmd(0xA4); // display scan on
	DISP_transmit_cmd(0xAF); // drivers on

303
	DISP_clear();
Petter Breedveld's avatar
Petter Breedveld committed
304
305
306
307
}

void DISP_rst()
{
308
309
310
311
	DSP_RST_PORT.OUTCLR = DSP_RST_bm;
	_delay_ms(10);
	DSP_RST_PORT.OUTSET = DSP_RST_bm;
	_delay_ms(10);
Petter Breedveld's avatar
Petter Breedveld committed
312
313
}