-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdwenguino_lcd.c
More file actions
159 lines (119 loc) · 3.1 KB
/
dwenguino_lcd.c
File metadata and controls
159 lines (119 loc) · 3.1 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
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
/*
* dwenguinoLCD.c
*
* Created on: Feb 17, 2021
* Author: Tom Neutens
*/
#include "dwenguino_lcd.h"
#include "dwenguino_board.h"
// Keeps track of curent line number and character position.
struct lcd_info_type lcd_info;
void initLCD(void){
LCD_DATA_DIR = 0xff; // Set LCD backlight als output.
_delay_ms(5);
LCD_DATA_DIR = 0x00; // Set port as input
LCD_DATA = 0;
LCD_RS_OUT;
LCD_RW_OUT;
LCD_EN_OUT;
LCD_RS_LOW;
LCD_RW_LOW;
LCD_EN_LOW;
_delay_ms(15);
LCD_DATA_DIR = 0xff; //set port as output
commandLCD(0b00110000);
commandLCD(0b00110000);
commandLCD(0b00110000);
commandLCD(0b00111000);
commandLCD(0b00000110);
commandLCD(0b00011000);
commandLCD(0b00001100);
lcd_info.line = lcd_info.pos = 0;
// Makes sure the LCD is cleared and the cursor is set to (0, 0).
clearLCD();
}
void clearLCD(void){
commandLCD(0b00000001);
//_delay_ms(2);
setCursorLCD(0, 0);
}
void commandLCD(const BYTE c) {
BYTE temp;
temp = LCD_DATA; // Save the current state on datapins for LED's
// Write command to LCD
LCD_RW_LOW;
LCD_RS_LOW;
LCD_EN_HIGH; // Bring the enable pin high
LCD_DATA = c; // Write command data to data port
LCD_EN_LOW; // Data is clocked at falling edge of enable pin
LCD_DATA = temp; // Restore data on datapins
_delay_us(45); // Wait until display has processed the data
}
void setCursorLCD(BYTE l, BYTE p) {
BYTE c;
// check if input is valid
if (l>LCD_LASTLINE) l = LCD_LASTLINE;
if (p>LCD_LASTPOS) p = LCD_LASTPOS;
lcd_info.line = l;
lcd_info.pos = p;
if (l == 1)
c = 0b11000000;
else
c = 0b10000000;
// position offset
p &= 0b00001111;
c |= p;
commandLCD(c);
// wait until display has processed the command.
// If this wait is not long enough, the cursor might not change!
_delay_ms(2);
}
void appendCharToLCD(const char c) {
unsigned temp = LCD_DATA; // Save current data on datapins for LED's
if (lcd_info.pos>LCD_LASTPOS){
if (lcd_info.line){
clearLCD();
setCursorLCD(0, 0);
}else{
setCursorLCD(1, 0);
}
}
// Write char to LCD
LCD_RW_LOW;
LCD_RS_HIGH;
LCD_EN_HIGH; // Bring enable pin high
LCD_DATA = c; // Write data to data port
LCD_EN_LOW; // data is clocked at the falling edge of enable pin
_delay_us(45); // wait until display has processed the data
lcd_info.pos++; // increment lcd cursor position
LCD_DATA = temp; // restore data on datapins
}
void printCharToLCD(const char c, BYTE l, BYTE p) {
// set cursor to selected position
setCursorLCD(l,p);
// append to this position
appendCharToLCD(c);
}
void appendStringToLCD_(const char* message) {
while (*message) {
appendCharToLCD(*message++);
}
}
void printStringToLCD(char* message, BYTE l, BYTE p) {
// set cursor to selected position
setCursorLCD(l,p);
// append to this position
appendStringToLCD(message);
}
void appendIntToLCD(int i) {
char buffer[7];
itoa(i,buffer, 10);
buffer[6] = 0;
appendStringToLCD_(buffer);
}
void printIntToLCD(int i, BYTE l, BYTE p) {
// set cursor to selected position
setCursorLCD(l,p);
// append to this position
appendIntToLCD(i);
}