-
Notifications
You must be signed in to change notification settings - Fork 1
/
glcd.c
235 lines (216 loc) · 8.52 KB
/
glcd.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*******************************************************************************************************
* [FILE NAME] : <glcd.c> *
* [AUTHOR] : <Eslam EL-Naggar> *
* [DATE CREATED]: <JAN 1, 2020> *
* [Description} : <Source file for graphical LCD interface driver> *
******************************************************************************************************/
/*-----------------------------------------INCLUDES----------------------------------------------------*/
#include "glcd.h"
#include "font_config.h"
/*----------------------------------------------------------------------------------------------------
[Function Name]: GLCD_sendCommand
[Description] : This function is responsible for sending command to GLCD
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void GLCD_sendCommand(char Command) /* GLCD command function */
{
GLCD_DATA_PORT = Command; /* Copy command on data pin */
GLCD_CTRL_PORT &= ~(1 << RS); /* Make RS LOW for command register*/
GLCD_CTRL_PORT &= ~(1 << RW); /* Make RW LOW for write operation */
GLCD_CTRL_PORT |= (1 << EN); /* HIGH-LOW transition on Enable */
_delay_us(5);
GLCD_CTRL_PORT &= ~(1 << EN);
_delay_us(5);
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GLCD_sendData
[Description] : This function is responsible for sending data to GLCD
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void GLCD_sendData(char Data) /* GLCD data function */
{
GLCD_DATA_PORT = Data; /* Copy data on data pin */
GLCD_CTRL_PORT |= (1 << RS); /* Make RS HIGH for data register */
GLCD_CTRL_PORT &= ~(1 << RW); /* Make RW LOW for write operation */
GLCD_CTRL_PORT |= (1 << EN); /* HIGH-LOW transition on Enable */
_delay_us(5);
GLCD_CTRL_PORT &= ~(1 << EN);
_delay_us(5);
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GLCD_readData
[Description] : This function is responsible for reading data from GLCD
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
uint8 GLCD_readData() /* GLCD data function */
{
uint8 Data;
GLCD_DATA_PORT_DIRECTION = 0x00;
_delay_ms(5);
GLCD_CTRL_PORT |= (1 << RS); /* Make RS HIGH for data register */
GLCD_CTRL_PORT |= (1 << RW); /* Make RW LOW for write operation */
GLCD_CTRL_PORT |= (1 << EN); /* HIGH-LOW transition on Enable */
_delay_us(5);
Data = GLCD_DATA_PIN;
_delay_us(5);
GLCD_CTRL_PORT &= ~(1 << EN);
_delay_us(5);
GLCD_DATA_PORT_DIRECTION = 0xFF;
return Data;
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GLCD_Init
[Description] : This function is responsible for initializing GLCD
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void GLCD_Init() /* GLCD initialize function */
{
GLCD_DATA_PORT_DIRECTION = 0xFF;
GLCD_CTRL_PORT_DIRECTION = 0xFF;
/* Select both left & right half of display & Keep reset pin high */
GLCD_CTRL_PORT |= (1 << CS1) | (1 << CS2) | (1 << RST);
_delay_ms(20);
GLCD_sendCommand(0x3E); /* Display OFF */
GLCD_sendCommand(0x40); /* Set Y address (column=0) */
GLCD_sendCommand(0xB8); /* Set x address (page=0) */
GLCD_sendCommand(0xC0); /* Set z address (start line=0) */
GLCD_sendCommand(0x3F); /* Display ON */
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GLCD_clearScreen
[Description] : This function is responsible for clearing the screen
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void GLCD_clearScreen() /* GLCD all display clear function */
{
{
int column,page,page_add=0xB8,k=0;
float page_inc= 0.5 ;
char byte;
GLCD_sendCommand(0x40); /* Set Y address (column=0) */
GLCD_sendCommand(0xB8); /* Set x address (page=0) */
GLCD_CTRL_PORT |= (1 << CS1); /* Select Left half of display */
GLCD_CTRL_PORT &= ~(1 << CS2);
for(page=0;page<16;page++) /* Print pages(8 page of each half)*/
{
for(column=0;column<64;column++)
{
GLCD_sendData(0);/* Print 64 column of each page */
}
GLCD_CTRL_PORT ^= (1 << CS1);/* Change segment controller */
GLCD_CTRL_PORT ^= (1 << CS2);
GLCD_sendCommand((page_add+page_inc));/* Increment page address*/
page_inc=page_inc+ 0.5 ; /* Increment pointer */
}
GLCD_sendCommand(0x40); /* Set Y address (column=0) */
GLCD_sendCommand(0xB8); /* Set x address (page=0) */
}
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GLCD_displayImage
[Description] : This function is responsible for displaying the image
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void GLCD_displayImage(const char* image) /* GLCD string write function */
{
int column,page,page_add=0xB8,k=0;
float page_inc=0.5;
char byte;
GLCD_CTRL_PORT |= (1 << CS1); /* Select Left half of display */
GLCD_CTRL_PORT &= ~(1 << CS2);
for(page=0;page<16;page++) /* Print pages(8 page of each half)*/
{
for(column=0;column<64;column++)
{
byte = pgm_read_byte(&image[k+column]);
GLCD_sendData(byte);/* Print 64 column of each page */
}
GLCD_CTRL_PORT ^= (1 << CS1);/* Change segment controller */
GLCD_CTRL_PORT ^= (1 << CS2);
GLCD_sendCommand((page_add+page_inc));/* Increment page address*/
page_inc=page_inc+0.5;
k=k+64; /* Increment pointer */
}
GLCD_sendCommand(0x40); /* Set Y address (column=0) */
GLCD_sendCommand(0xB8); /* Set x address (page=0) */
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GLCD_displayString
[Description] : This function is responsible for displaying the string
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void GLCD_displayString(char page_no, char *str)/* GLCD string write function */
{
unsigned int i, column;
unsigned int Page = ((0xB8) + page_no);
unsigned int Y_address = 0;
float Page_inc = 0.5;
GLCD_CTRL_PORT |= (1 << CS1); /* Select Left half of display */
GLCD_CTRL_PORT &= ~(1 << CS2);
GLCD_sendCommand(Page);
for(i = 0; str[i] != 0; i++) /* Print char in string till null */
{
if (Y_address > (1024-(((page_no)*128)+FontWidth)))
break;
if (str[i]!=32) /* space value */
{
for (column=1; column<=FontWidth; column++)
{
if ((Y_address+column)==(128*((int)(Page_inc+0.5))))
{
if (column == FontWidth)
break;
GLCD_sendCommand(0x40); /* set Y-address to zero */
Y_address = Y_address + column;
GLCD_CTRL_PORT ^= (1 << CS1);
GLCD_CTRL_PORT ^= (1 << CS2);
GLCD_sendCommand(Page + Page_inc);
Page_inc = Page_inc + 0.5;
}
}
}
if (Y_address>(1024-(((page_no)*128)+FontWidth)))
break;
if((font[((str[i]-32)*FontWidth)+4])==0 || str[i]==32)
{
for(column=0; column<FontWidth; column++)
{
GLCD_sendData(font[str[i]-32][column]);
if((Y_address+1)%64==0)
{
GLCD_CTRL_PORT ^= (1 << CS1);
GLCD_CTRL_PORT ^= (1 << CS2);
GLCD_sendCommand((Page+Page_inc));
Page_inc = Page_inc + 0.5;
}
Y_address++;
}
}
else
{
for(column=0; column<FontWidth; column++)
{
GLCD_sendData(font[str[i]-32][column]);
if((Y_address+1)%64==0)
{
GLCD_CTRL_PORT ^= (1 << CS1);
GLCD_CTRL_PORT ^= (1 << CS2);
GLCD_sendCommand((Page+Page_inc));
Page_inc = Page_inc + 0.5;
}
Y_address++;
}
GLCD_sendData(0);
Y_address++;
if((Y_address)%64 == 0)
{
GLCD_CTRL_PORT ^= (1 << CS1);
GLCD_CTRL_PORT ^= (1 << CS2);
GLCD_sendCommand((Page+Page_inc));
Page_inc = Page_inc + 0.5;
}
}
}
GLCD_sendCommand(0x40); /* Set Y address (column=0) */
GLCD_sendCommand(0xB8); /* Set x address (page=0) */
}