-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
116 lines (86 loc) · 2.75 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
// -----------------------------------------------------------------------
// Copyright (C) Rodrigo Almeida 2010
// -----------------------------------------------------------------------
// Arquivo: lcd.c
// Biblioteca de manipulação do LCD
// Autor: Rodrigo Maximiano Antunes de Almeida
// rodrigomax at unifei.edu.br
// Licença: GNU GPL 2
// -----------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// -----------------------------------------------------------------------
#include "lcd.h"
#include <pic18f4520.h>
#include "bits.h"
#include "io.h"
#define EN PIN_E1
#define RS PIN_E2
void Delay40us(void)
{
unsigned char i;
for(i=0; i < 25; i++); //valor aproximado
}
void Delay2ms(void)
{
unsigned char i;
for(i=0; i < 50; i++)
{
Delay40us();
}
}
void lcdCommand(unsigned char cmd)
{
unsigned char old_D;
old_D = PORTD;
//garantir compatibilidade
TRISD = 0x00;
digitalWrite(RS, LOW); //comando
PORTD = cmd;
digitalWrite(EN, HIGH); //Pulso no Enable
digitalWrite(EN, LOW);
PORTD= old_D;
if((cmd == 0x02)||(cmd == 0x01)){
Delay2ms();
}else{
Delay40us();
}
}
void lcdData(unsigned char valor)
{
//garantir compatibilidade
unsigned char old_D;
old_D = PORTD;
TRISD = 0x00;
digitalWrite(RS, HIGH); //comando
PORTD = valor;
digitalWrite(EN, HIGH); //Pulso no Enable
digitalWrite(EN, LOW);
PORTD= old_D;
Delay40us();
}
void lcdInit(void){
// configurações de direção dos terminais
pinMode(RS, OUTPUT);
pinMode(EN, OUTPUT);
TRISD = 0x00; //dados
// garante inicialização do LCD (+-10ms)
Delay2ms(); Delay2ms(); Delay2ms(); Delay2ms(); Delay2ms();
//precisa enviar 4x pra garantir 8 bits
lcdCommand(0x38); //8bits, 2 linhas, 5x8
Delay2ms(); Delay2ms();
lcdCommand(0x38); //8bits, 2 linhas, 5x8
Delay2ms();
lcdCommand(0x38); //8bits, 2 linhas, 5x8
lcdCommand(0x38); //8bits, 2 linhas, 5x8
lcdCommand(0x06); //modo incremental
//habilitar o cursor, trocar 0x0C por 0x0F;
lcdCommand(0x0C); //display e cursor on, com blink
lcdCommand(0x01); //limpar display
}