-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd.c
executable file
·115 lines (98 loc) · 3.49 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
#include "lcd.h"
void LCD_init(void)
{
_delay_ms(30);
LCD_DATA_PORT_DIR = 0xFF; /* Configure the data port as output port */
LCD_CTRL_PORT_DIR |= (1<<E) | (1<<RS) | (1<<RW); /* Configure the control pins(E,RS,RW) as output pins */
LCD_sendCommand(SINGLE_LINE_LCD_Four_BIT_MODE); /* use 2-line lcd + 8-bit Data Mode + 5*7 dot display Mode */
LCD_sendCommand(CURSOR_OFF); /* cursor off */
LCD_sendCommand(CLEAR_COMMAND); /* clear LCD at the beginning */
_delay_ms(30);
}
void LCD_sendCommand(uint8 command)
{
unsigned char a,b;
a = command & 0xf0; b = command << 4;
CLEAR_BIT(LCD_CTRL_PORT,RS); /* Instruction Mode RS=0 */
CLEAR_BIT(LCD_CTRL_PORT,RW); /* write data to LCD so RW=0 */
_delay_ms(1); /* delay for processing Tas = 50ns */
SET_BIT(LCD_CTRL_PORT,E); /* Enable LCD E=1 */
_delay_ms(1); /* delay for processing Tpw - Tdws = 190ns */
LCD_DATA_PORT = a; /* out the required command to the data bus D0 --> D7 */
_delay_ms(1); /* delay for processing Tdsw = 100ns */
CLEAR_BIT(LCD_CTRL_PORT,E); /* disable LCD E=0 */
_delay_ms(1); /* delay for processing Th = 13ns */
SET_BIT(LCD_CTRL_PORT,E); /* Enable LCD E=1 */
_delay_ms(1); /* delay for processing Tpw - Tdws = 190ns */
LCD_DATA_PORT = b; /* out the required command to the data bus D0 --> D7 */
_delay_ms(1); /* delay for processing Tdsw = 100ns */
CLEAR_BIT(LCD_CTRL_PORT,E); /* disable LCD E=0 */
_delay_ms(1); /* delay for processing Th = 13ns */
}
void LCD_displayCharacter(uint8 data)
{
unsigned char a,b;
a = data & 0xf0; b = data << 4;
SET_BIT(LCD_CTRL_PORT,RS); /* Data Mode RS=1 */
CLEAR_BIT(LCD_CTRL_PORT,RW); /* write data to LCD so RW=0 */
_delay_ms(1); /* delay for processing Tas = 50ns */
SET_BIT(LCD_CTRL_PORT,E); /* Enable LCD E=1 */
_delay_ms(1); /* delay for processing Tpw - Tdws = 190ns */
LCD_DATA_PORT = a; /* out the required data char to the data bus D0 --> D7 */
_delay_ms(1); /* delay for processing Tdsw = 100ns */
CLEAR_BIT(LCD_CTRL_PORT,E); /* disable LCD E=0 */
_delay_ms(1); /* delay for processing Th = 13ns */
_delay_ms(1); /* delay for processing Tas = 50ns */
SET_BIT(LCD_CTRL_PORT,E); /* Enable LCD E=1 */
_delay_ms(1); /* delay for processing Tpw - Tdws = 190ns */
LCD_DATA_PORT = b; /* out the required data char to the data bus D0 --> D7 */
_delay_ms(1); /* delay for processing Tdsw = 100ns */
CLEAR_BIT(LCD_CTRL_PORT,E); /* disable LCD E=0 */
_delay_ms(1); /* delay for processing Th = 13ns */
}
void LCD_displayString(const char *Str)
{
uint8 i = 0;
while(Str[i] != '\0')
{
LCD_displayCharacter(Str[i]);
i++;
}
}
void LCD_goToRowColumn(uint8 row,uint8 col)
{
uint8 Address;
switch(row)
{
case 0:
Address=col;
break;
case 1:
Address=col+0x40;
break;
case 2:
Address=col+0x10;
break;
case 3:
Address=col+0x50;
break;
}
/* to write to a specific address in the LCD
* we need to apply the corresponding command 0b10000000+Address */
LCD_sendCommand(Address | SET_CURSOR_LOCATION);
}
void LCD_displayStringRowColumn(uint8 row,uint8 col,const char *Str)
{
LCD_goToRowColumn(row,col); /* go to to the required LCD position */
LCD_displayString(Str); /* display the string */
}
void LCD_intgerToString(int data)
{
char buff[16]; /* String to hold the ascii result */
itoa(data,buff,10); /* 10 for decimal */
LCD_displayString(buff);
}
void LCD_clearScreen(void)
{
LCD_sendCommand(CLEAR_COMMAND); //clear display
}