-
Notifications
You must be signed in to change notification settings - Fork 0
/
display1602.c
78 lines (63 loc) · 1.55 KB
/
display1602.c
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
#include <inttypes.h>
#include <avr/io.h>
#include <stdbool.h>
#include "display1602.h"
#include <util/delay.h>
void init_1602_pins() {
CONTROL_1602_PORT_DDR |= _BV(CONTROL_1602_E) | _BV(CONTROL_1602_RS) | _BV(CONTROL_1602_RW);
DATA_1602_PORT_DDR |= 0xFF; //TODO: 4-bit mode enable;
}
/**
* accepts line numbers
* valid arguments are
* CMD_1602_DDRAM_LINE1
* CMD_1602_DDRAM_LINE2
*
*/
void select_1602_line(uint8_t line) {
write_1602_command(CMD_1602_DDRAM_SET | line);
_delay_ms(2);
}
void write_1602_line(int count, char* buf) {
for(int i=0; i<count; i++) {
write_1602_char((uint8_t) buf[i]);
_delay_us(37);
}
}
//TODO: move cursor, etc
void init_display() {
_delay_ms(15); //wait for display initialization
write_1602_command(CMD_1602_FUNC_SET_BIT | CMD_1602_FUNC_DL | CMD_1602_FUNC_N);
_delay_ms(1);
// Turn on display
write_1602_command(CMD_1602_MODE_ON);
_delay_us(37);
// Clear screen
write_1602_command(CMD_1602_CLS);
_delay_ms(2);
//Home
write_1602_command(CMD_1602_HOME);
_delay_ms(2);
//Set Direction to Right
write_1602_command(CMD_1602_DIRECTION_RIGHT);
_delay_ms(1);
}
/**
* 0 - write
* 1 - read
*/
void write_1602_command(uint8_t command) {
//enable command
CONTROL_1602_PORT = (CONTROL_1602_PORT & ~CMD_MASK) | _BV(CONTROL_1602_E);
DATA_1602_PORT = command;
_delay_us(5);
CONTROL_1602_PORT &= ~CMD_MASK;
}
void write_1602_char(uint8_t data) {
CONTROL_1602_PORT = (CONTROL_1602_PORT & ~CMD_MASK)
| _BV(CONTROL_1602_RS)
| _BV(CONTROL_1602_E);
DATA_1602_PORT = data;
_delay_us(5);
CONTROL_1602_PORT &= ~CMD_MASK;
}