-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.c
More file actions
134 lines (108 loc) · 3.23 KB
/
Display.c
File metadata and controls
134 lines (108 loc) · 3.23 KB
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
#include "Display.h"
char *message[100]; // initialize array for holding message in case it is long enough to scroll
int current_line = 0;
int last_line = 1;
int line;
void display_init() {
// Make pins GPIO
uint32_t k;
for (k=0; k<4; k++) {
PORTB->PCR[k] &= ~PORT_PCR_MUX_MASK; // low 4 LCD data bits
PORTB->PCR[k] |= PORT_PCR_MUX(1);
}
for (k=8; k<12; k++) {
PORTB->PCR[k] &= ~PORT_PCR_MUX_MASK; // upper 4 LCD data bits
PORTB->PCR[k] |= PORT_PCR_MUX(1);
}
for (k=0; k<3; k++) {
PORTE->PCR[k] &= ~PORT_PCR_MUX_MASK; // LCD control 3-bits
PORTE->PCR[k] |= PORT_PCR_MUX(1);
}
PTE->PDDR |= MASK(DISPLAY_ENABLE) | MASK(DISPLAY_RW) | MASK(DISPLAY_RS); // set pins to output
PTB->PDDR |= 0x00000F0F; // set pins to output
delayMs(30);
LCD_command(0x30);
delayMs(10);
LCD_command(0x30);
delayMs(1);
LCD_command(0x30);
LCD_command(0x38); // 8bit data, 2-line, 5x8 ch
LCD_command(0x06); // move cursor right
LCD_command(0x01); // clr screen, home cursor
LCD_command(0x0C); // display on without cursor
}
void LCD_command(uint32_t command) {
PTE->PCOR |= MASK(DISPLAY_RW ) | MASK(DISPLAY_RS) | MASK(DISPLAY_ENABLE);
PTB->PCOR = 0x00000F0F;
PTB->PSOR |= ((command & 0x000000F0) << 4) | (command & 0x0000000F); // get lower 8 bits and send them to portB0-3 and portB8-11
// trigger enable to write to display
PTE->PSOR |= MASK(DISPLAY_ENABLE);
delayMs(0);
PTE->PCOR |= MASK(DISPLAY_ENABLE);
delayMs(0);
if (command < 4) {
delayMs(4); // command 1 and 2 need 1.64ms
} else {
delayMs(1); // all others 40us
}
}
void LCD_data(uint32_t data) {
PTE->PCOR = MASK(DISPLAY_RW) | MASK(DISPLAY_ENABLE); // clear R/W and EN
PTE->PSOR = MASK(DISPLAY_RS); // set RS
PTB->PCOR = 0x00000F0F; // clear output data bits to 0
PTB->PSOR |= ((data & 0x000000F0) << 4) | (data & 0x0000000F);// output command to portB0-3 and portB8-11
PTE->PSOR = MASK(DISPLAY_ENABLE); // assert enable
delayMs(0);
PTE->PCOR = MASK(DISPLAY_ENABLE); // de-assert enable
delayMs(1);
}
void delayMs(uint32_t n) {
uint32_t i;
uint32_t j;
for(i = 0; i < n; i++) {
for(j = 0; j < 7000; j++) {}
}
}
void send_multiline_message(char **display_lines) {
LCD_command(0x01); // Clear the screen
line = 0;
while(*display_lines)
{
message[line] = *display_lines;
// if this is either the first or second line that we want displayed then display it, otherwise skip it
if ((line == current_line) && ((1 == (last_line - current_line)) || (0 == (last_line - current_line)))) {
send_string(*display_lines);
LCD_command(0xC0); // Force the cursor to the beginning of the second line
current_line++;
}
display_lines++;
line++;
}
}
void send_message(char *string) {
LCD_command(0x01); // Clear the screen
send_string(string);
}
void send_string(char *s) {
while(*s)
{
LCD_data(*s);
s++;
}
}
void scroll_up() {
// if we can scroll up
if (current_line > 2) {
current_line = current_line - 3;
last_line = current_line + 1;
send_multiline_message(message);
}
}
void scroll_down() {
// if we can scroll down
if (line != last_line) {
current_line = last_line;
last_line++;
send_multiline_message(message);
}
}