-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcd.h
114 lines (100 loc) · 2.94 KB
/
lcd.h
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
//Hitachi 44780 LCD Driver
//
//LCD (PortB)
#define E LATBbits.LATB3
//#define R_W LATBbits.LATB6
#define RS LATBbits.LATB2
#define LCDdata LATB
void command(unsigned char);
void write(unsigned char);
void Nybble(unsigned char);
void Lcd_init(void);
void Lcd_PutMessage(rom char *);
void Lcd_PutMessageArray(unsigned char[]);
void Lcd_PutMessageChar(unsigned char);
//Write a string to the LCD
void Lcd_PutMessage(rom char *Message){
rom char *Pos = Message;
while(*Pos!=0)
write(*Pos++);
}
void Lcd_PutMessageChar(unsigned char Message){
unsigned char *Pos = Message;
while(*Pos!=0)
write(*Pos++);
}
void Lcd_PutMessageArray(unsigned char m[]){
unsigned char Pos = 0;
while(m[Pos]!=0){
write(m[Pos]);
Pos++;
}
}
/**********************************************************/
//4-bit methods for LCD
/**********************************************************/
void command(unsigned char i)
{
RS =0;
//R_W =0; //R/W=LOW : Write
Nybble(i); //Send upper 4 bits
Nybble(i<<4); //Send lower 4 bits
Delay1KTCYx(2); //must wait at least 2mS (2*1000*4/1e6 = 8ms used)
}
void write(unsigned char i)
{
RS =1;
//R_W =0; //R/W=LOW : Write
Nybble(i); //Send upper 4 bits
Nybble(i<<4); //Send lower 4 bits
Delay1KTCYx(2); //must wait 2mS
}
/**********************************************************/
void Nybble(unsigned char dat)
{
dat &= 0xf0; //clear bottom bits of dat
LCDdata &= 0x0f; //clear top bits of port (interested only in DB7-DB4)
LCDdata |= dat; //or the two and store at port
E = 1;
Delay1TCY(); //enable pulse width >= 300ns (used 4uS)
E = 0; //Clock enable: falling edge
}
/**********************************************************/
void Lcd_init(void)
{
LCDdata=0x00;
Delay1KTCYx(15); //Wait >15 msec after power is applied (used 20mS)
Nybble(0x3); //command 0x30 = Wake up
Delay1KTCYx(5); //must wait 160us, busy flag not available (used 160uS)
Nybble(0x3); //command 0x30 = Wake up #2
Delay1KTCYx(5); //must wait 160us, busy flag not available (used 160uS)
command(0x20); //Function set: 4-bit/2-line
Delay1KTCYx(1);
command(0x2c); //Function set: 4-bit/2-line
Delay1KTCYx(1);
command(0x10); //Set cursor
Delay1KTCYx(1);
command(0x01); //Clear Display (Added)
Delay1KTCYx(1);
command(0x06); //Entry Mode set
Delay1KTCYx(1);
command(0x0c);
}
void Lcd_clearDisplay()
{
command(0x01);
Delay1KTCYx(1);
}
void Lcd_cursorHome()
{
command(0x02);
Delay1KTCYx(1);
}
void Lcd_newLine()
{
command(0xc0);
Delay1KTCYx(1);
}
/**********************************************************/
//End methods for LCD
/**********************************************************/