-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
145 lines (135 loc) · 3.14 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
#define LCD_data P2 // LCD Data port
//Function Declarations
void LCD_Init();
void LCD_DataWrite(char dat);
void LCD_CmdWrite(char cmd);
void LCD_WriteString(char * str, unsigned char len);
void LCD_Ready();
void sdelay(int delay);
void delay_ms(int delay);
sbit LCD_rs = P0^0; // LCD Register Select
sbit LCD_rw = P0^1; // LCD Read/Write
sbit LCD_en = P0^2; // LCD Enable
sbit LCD_busy = P2^7; // LCD Busy Flag
//Global Variables
bit transmit_completed= 0; // To check if spi data transmit is complete
char serial_data, data_save_high, data_save_low;
// void main(void)
// {
// P2 = 0x00; // Make Port 2 output
// LCD_Init();
// while(1) // endless
// {
// /* Code for displaying "Hello World"
// on the LCD to be written here */
// delay_ms(5000);
// }
// }
/**
* FUNCTION_PURPOSE:LCD Initialization
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: none
*/
void LCD_Init()
{
sdelay(100);
LCD_CmdWrite(0x38); // LCD 2lines, 5*7 matrix
LCD_CmdWrite(0x0E); // Display ON cursor ON Blinking off
LCD_CmdWrite(0x01); // Clear the LCD
LCD_CmdWrite(0x80); // Cursor to First line First Position
}
/**
* FUNCTION_PURPOSE: Write Command to LCD
* FUNCTION_INPUTS: cmd- command to be written
* FUNCTION_OUTPUTS: none
*/
void LCD_CmdWrite(char cmd)
{
LCD_Ready();
LCD_data=cmd; // Send the command to LCD
LCD_rs=0; // Select the Command Register by pulling LCD_rs LOW
LCD_rw=0; // Select the Write Operation by pulling RW LOW
LCD_en=1; // Send a High-to-Low Pusle at Enable Pin
sdelay(5);
LCD_en=0;
sdelay(5);
}
/**
* FUNCTION_PURPOSE: Write Command to LCD
* FUNCTION_INPUTS: dat- data to be written
* FUNCTION_OUTPUTS: none
*/
void LCD_DataWrite( char dat)
{
LCD_Ready();
LCD_data=dat; // Send the data to LCD
LCD_rs=1; // Select the Data Register by pulling LCD_rs HIGH
LCD_rw=0; // Select the Write Operation by pulling RW LOW
LCD_en=1; // Send a High-to-Low Pusle at Enable Pin
sdelay(5);
LCD_en=0;
sdelay(5);
}
/**
* FUNCTION_PURPOSE: Write a string on the LCD Screen
* FUNCTION_INPUTS: 1. str - pointer to the string to be written,
2. length - length of the array
* FUNCTION_OUTPUTS: none
*/
void LCD_WriteString( char * str, unsigned char length)
{
while(length>0)
{
LCD_DataWrite(*str);
str++;
length--;
}
}
/**
* FUNCTION_PURPOSE: To check if the LCD is ready to communicate
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: none
*/
void LCD_Ready()
{
LCD_data = 0xFF;
LCD_rs = 0;
LCD_rw = 1;
LCD_en = 0;
sdelay(5);
LCD_en = 1;
while(LCD_busy == 1)
{
LCD_en = 0;
LCD_en = 1;
}
LCD_en = 0;
}
/**
* FUNCTION_PURPOSE: A delay of 15us for a 24 MHz crystal
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: none
*/
void sdelay(int delay)
{
char d=0;
while(delay>0)
{
for(d=0;d<5;d++);
delay--;
}
}
/**
* FUNCTION_PURPOSE: A delay of around 1000us for a 24MHz crystel
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: none
*/
void delay_ms(int delay)
{
int d=0;
while(delay>0)
{
for(d=0;d<382;d++);
delay--;
}
}