-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
425 lines (348 loc) · 8.08 KB
/
lcd.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <intrinsics.h>
#include "io430.h"
#include "lcd.h"
//Define a porta onde o LCD será conectado
#define LCD_PSEL P1SEL
#define LCD_PDIR P1DIR
#define LCD_POUT P1OUT
#define LCD_PIN P1IN
//Define porta do backlight
#define LCD_BL_PSEL P4SEL
#define LCD_BL_PDIR P4DIR
#define LCD_BL_POUT P4OUT
//Bits de controle
#define LCD_E BIT0
#define LCD_CD BIT1
#define LCD_RW BIT2
#define LCD_CONTR BIT3
#define LCD_BL BIT7
//Bits de dados
#define LCD_D0 BIT4
#define LCD_D1 BIT5
#define LCD_D2 BIT6
#define LCD_D3 BIT7
#define LCD_LINES 2
#define LCD_CHARS_PER_LINE 16
struct lcdDisplay {
unsigned char line1[16];
unsigned char line2[16];
};
static struct lcdDisplay lcd;
unsigned char lcdTicks = 0;
//Funções de escopo local
static void lcdDelayDado(void);
static void lcdDelayInst(void);
static void lcdInitIO(void);
static void lcdInstructionWrite(unsigned char);
static void lcdDataWrite(unsigned char);
static void lcdClear(void);
static void lcdInitTimer(unsigned int, unsigned int);
/*! brief Espera um tempo até que o LCD possa ser configurado */
void lcdWait(void)
{
int i,j;
for (i=0;i<30000;i++)
for(j=0;j<100;j++);
}
/*! \brief Delay entre o envio de dados */
static void lcdDelayDado(void)
{
unsigned int i;
for (i = 800; i > 0; i--);
}
/*! \brief Delay entre instruções
*/
static void lcdDelayInst(void)
{
unsigned int i;
for (i = 1200; i > 0; i--);
}
/*! \brief Inicia a porta responsável por comunicar com o LCD */
void lcdInitIO(void)
{
//Seleciona os pinos de controle como saída e configura o seu valor
LCD_PSEL &= ~(LCD_E + LCD_CD + LCD_RW);
LCD_PDIR |= (LCD_E + LCD_CD + LCD_RW);
LCD_POUT &= ~(LCD_CD + LCD_RW);
LCD_POUT &= ~(LCD_E);
//Seleciona os pinos de dados como saída
LCD_PSEL &= ~(LCD_D0 + LCD_D1 + LCD_D2 + LCD_D3);
LCD_PDIR |= (LCD_D0 + LCD_D1 + LCD_D2 + LCD_D3);
//Acende o backlight
LCD_BL_PSEL &= ~(LCD_BL);
LCD_BL_PDIR |= LCD_BL;
LCD_BL_POUT |= LCD_BL;
// configura o pino do contraste
LCD_PDIR |= LCD_CONTR;
}
/*! \brief Configura o PWM responsável pelo ajuste do contraste */
static void lcdInitTimer(unsigned int period, unsigned int periodOn)
{
/*
//Seleciona o pino como PWM e saída
LCD_PDIR |= LCD_CONTR;
LCD_PSEL |= LCD_CONTR;
//Configura o período total e o período ligado
TACCR0 = period;
TACCTL2 = OUTMOD_7;
TACCR2 = periodOn;
//Configura o timer
TACTL = TASSEL_2 + MC_1;
*/
}
/*! \brief Envia comando para o controlador do LCD
*/
static void lcdInstructionWrite(unsigned char inst)
{
// reseta o bit !C/D para indicar dado
LCD_POUT &= ~LCD_CD;
// coloca o primeiro nibble no barramento
LCD_POUT = (inst & 0xF0) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
//Coloca o segundo nibble no barramento
LCD_POUT = ((inst & 0x0F) << 4) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
lcdDelayInst();
}
/*! \brief Escreve uma caracter na posição atual do LCD
*/
static void lcdDataWrite(unsigned char data)
{
// seta o bit !C/D para indicar dado
LCD_POUT |= LCD_CD;
// coloca o dado no barramento
LCD_POUT = (data & 0xF0) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
//Coloca o segundo nibble no barramento
LCD_POUT = ((data & 0x0F) << 4) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
lcdDelayDado();
}
/*! \brief Envia comando para o controlador do LCD
*/
static void lcdInstructionWriteNoDelay(unsigned char inst)
{
// reseta o bit !C/D para indicar dado
LCD_POUT &= ~LCD_CD;
// coloca o primeiro nibble no barramento
LCD_POUT = (inst & 0xF0) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
//Coloca o segundo nibble no barramento
LCD_POUT = ((inst & 0x0F) << 4) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
}
/*! \brief Escreve uma caracter na posição atual do LCD
*/
static void lcdDataWriteNoDelay(unsigned char data)
{
// seta o bit !C/D para indicar dado
LCD_POUT |= LCD_CD;
// coloca o dado no barramento
LCD_POUT = (data & 0xF0) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
//Coloca o segundo nibble no barramento
LCD_POUT = ((data & 0x0F) << 4) + (LCD_POUT & 0x0F);
// pulsa o bit CS (ENABLE)
LCD_POUT |= LCD_E;
LCD_POUT &= ~LCD_E;
}
/*! \brief Limpa o display */
static void lcdClear(void)
{
lcdInstructionWrite(0x01);
lcdDelayDado();
}
/*! \brief Rotina de iniciação do display */
void lcdInit(void)
{
//Inicia o hw
lcdInitIO();
//Inicia o pwm do contraste
lcdInitTimer(PWM_PERIOD, PWM_PERIOD_ON);
//Delay até que a tensão de alimentação se normalize
lcdDelayInst();
lcdDelayInst();
lcdDelayInst();
lcdDelayInst();
lcdClear();
// 8 bits, 2 linhas e 5x8 dots
//lcdInstructionWrite(0x38);
lcdInstructionWrite(0x28);
lcdDelayInst();
// 8 bits, 2 linhas e 5x8 dots
//lcdInstructionWrite(0x38);
lcdInstructionWrite(0x28);
lcdDelayInst();
// Sentido de movimentação do vetor
lcdInstructionWrite(0x06);
lcdDelayInst();
// Liga display e cursor
lcdInstructionWrite(0x0C);
lcdDelayInst();
}
/*! \brief Armazena em memória o valor a ser colocado na tela
* \param dataMsg Mensagem a ser gravada
* \param row Posição da linha
* \param col Posição da coluna
*/
void lcdPutString(unsigned char * dataMsg,unsigned char row, unsigned char col)
{
unsigned char countMsg;
unsigned char countLcd;
if(row < LCD_LINES)
{
countMsg = 0;
countLcd = col;
while((dataMsg[countMsg] != 0x00) && (countLcd < (LCD_CHARS_PER_LINE)))
{
if(row == 0)
{
lcd.line1[countLcd] = dataMsg[countMsg];
}
else
{
lcd.line2[countLcd] = dataMsg[countMsg];
}
countMsg++;
countLcd++;
}
}
lcdUpdate();
}
/*! \brief Coloca na tela a mensagem */
void lcdUpdate(void)
{
char i;
lcdInstructionWrite(0x80);
for(i = 0; i < 16; i++)
{
lcdDataWrite(lcd.line1[i]);
}
lcdInstructionWrite(0xC0);
for(i = 0; i < 16; i++)
{
lcdDataWrite(lcd.line2[i]);
}
}
/*! \brief Liga o backlight */
void lcdBacklightOn(void)
{
LCD_BL_POUT |= LCD_BL;
}
/*! \brief Desliga o backlight */
void lcdBacklightOff(void)
{
LCD_BL_POUT &= ~LCD_BL;
}
/*! \brief Atualiza lcd no fundo utilizando o timer para controlar o tempo
* \return Retorna 1 se o procedimento de atualização foi finalizado
*/
unsigned char lcdLowUpdate(void)
{
static unsigned char state = 0;
static unsigned char count = 0;
switch(state)
{
case 0:
lcdInstructionWriteNoDelay(0x80);
state = 1;
lcdTicks = 0;
break;
case 1:
if(lcdTicks >= 2)
{
state = 2;
}
break;
case 2:
lcdDataWriteNoDelay(lcd.line1[count]);
count++;
state = 3;
lcdTicks = 0;
if(count == 16)
{
count = 0;
state = 4;
}
break;
case 3:
if(lcdTicks >= 2)
{
state = 2;
}
break;
case 4:
if(lcdTicks >= 2)
{
state = 5;
}
break;
case 5:
lcdInstructionWriteNoDelay(0xC0);
state = 6;
lcdTicks = 0;
break;
case 6:
if(lcdTicks >= 2)
{
state = 7;
}
break;
case 7:
lcdDataWriteNoDelay(lcd.line2[count]);
count++;
state = 8;
lcdTicks = 0;
if(count == 16)
{
count = 0;
state = 0;
return 1;
}
break;
case 8:
if(lcdTicks >= 2)
{
state = 7;
}
break;
}
return 0;
}
unsigned char lcdGetChar(unsigned char num)
{
return (lcd.line2[num]);
}
void lcdSetChar(unsigned char val, unsigned char num, unsigned char col)
{
if(col)
{
lcd.line2[num] = val;
}
else
{
lcd.line1[num] = val;
}
}
void lcdContrSet(void)
{
LCD_POUT |= LCD_CONTR;
}
void lcdContrClear(void)
{
LCD_POUT &= ~(LCD_CONTR);
}