/
lcd_4bit.c
165 lines (133 loc) · 3.02 KB
/
lcd_4bit.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
#include "compiler_defs.h"
#include "C8051F380_defs.h"
#include "lcd_4bit.h"
#include "delay.h"
void send_command(unsigned char dat);
void send_data(char cmd);
void send_4bit(unsigned char dat);
U8 _line=1,_position=0;
//=============================================================
//===============================LCD===========================
//=============================================================
void PulseE()
{
P4=P4|0x08; //E=1
delay_us(10);
P4=P4& ~0x08; //E=0
}
//=======================CONFIG THE LCD=======================
void lcd_begin() //Initialization of The LCD
{
delay_ms(100);
P4=0;
delay_ms(20);
P4=0x30;//send_4bit(0x30);
PulseE();
delay_ms(10);
P4=0x30;
PulseE();
delay_ms(10);
P4=0x30;
PulseE();
delay_ms(10);
P4=0x20;
PulseE();
delay_ms(10);
send_command(0x28); //4bit,2 line,5*7 dot
delay_ms(10);
send_command(0x0e); //display on ,cursor on,cursor blink
delay_ms(10);
send_command(0x01); //clear_display
delay_ms(10);
send_command(0x06); // increment cursor,no display shift
delay_ms(10);
_line=1; _position=0;
}
void lcd_clearScreen() //Initialization of The LCD
{
send_command(0x01); //clear_display
delay_ms(10);
send_command(0x02);// cursor at home
delay_ms(10);
_line=1; _position=0;
}
//===========================LCD LINE NUMBER & POSITION====================
void lcd_setCursor(U8 line_number,U8 p)
{
if((line_number>0) && (line_number<=2) && (p<16)){
_line=line_number; _position=p;
switch(line_number)
{
case 1:
send_command(0x80+p);
break;
case 2:
send_command(0xc0+p);
break;
}
delay_us(50);
}
}
//================DISPLAY THE VARIABLE ON THE LCD=================
void lcd_write(int v)
{
char buf[1];
int i,j=10000;
bit flag=0;
if(v<0) { lcd_print("-"); v=-v;}
for(i=0;i<5;i++)
{
buf[0]=v/j;
if(buf[0]) flag=1;
v=v%j;
j=j/10;
if(flag || j==0) { buf[0]+='0'; lcd_print(buf);}
}
}
//================ PRINT STRING =================
void lcd_print(char str[])
{
int i=0;
while(str[i] )
{
if(_position<16)
{
send_data(str[i++]);
_position++;
}
else
{
if(_line==1) { _line=2; send_command(0xc0); _position=0;}
else { _line=1; send_command(0x80); _position=0;}
}
delay_us(50);
}
}
void lcd_println(char str[])
{
lcd_print(str);
if(_line==1) { _line=2; send_command(0xc0); _position=0;}
else { _line=1; send_command(0x80); _position=0;}
delay_us(50);
}
//================ PRINT STRING WITH POSITION =================
void lcd_print_Position(U8 line_number,U8 p,char str[])
{
lcd_setCursor(line_number,p);
if((line_number>0) && (line_number<=2) && (p<16))
lcd_print(str);
}
void send_command (char cmd)
{
P4=cmd&0xf0;//RS=0 4bit MSB
PulseE();
P4=cmd<<4;//RS=0 4bit LSB
PulseE();
}
void send_data (unsigned char dat)
{
P4=(dat&0xf0)| 0x02;//RS=1 4bit MSB
PulseE();
P4=(dat<<4)| 0x02;//RS=1 4bit LSB
PulseE();
}