-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD_mega128.h
205 lines (176 loc) · 4.58 KB
/
LCD_mega128.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/****************************************************************************************
FileName : LCD_mega128.h
ProjectName : LCD TEST
Target : ATmega128
CPU Clock : 16Mhz
Author : TEAM
Description : This is a header file for LCD.
****************************************************************************************/
/* LCD.h
*
* This header files have implementation of some common functions of the LCD.
* Include this header file in your C code. Call the lcd_init() function before
calling any other functions to initialize the lcd.
lcd_init() ----- Call the lcd_init() function before calling any other functions
to initialize the lcd.
lcd_cmd() ------ This function is used to give any command instructions to the
LCD. For e.g lcd_cmd(0x01) will give the clear command.
lcd_char() ----- This function will display a single character on the LCD display.
For example lcd_char(0x61) will display A. Again lcd_char('b')
will display b.
lcd_string()---- This function will display a string. An example of this would be
like lcd_string("This is AVR")
lcd_showvalue()- This will show a 3-digit decimal value on the LCD. For example if
we give lcd_showvalue(0xFF) then 255 will be displayed.
lcd_gotoxy1()--- Will set the cursor at a particular position on LINE1 of the LCD.
So lcd_gotoxy1(3) will set the cursor at the 3+1=4th column in
the 1st line of the LCD.
lcd_gotoxy2()--- Will set the cursor at a particular position on LINE1 of the LCD.
So lcd_gotoxy2(0) will set the cursor at the 0+1=1st column in
the 2nd line of the LCD.
lcd_exit() ----- You may call this function after you are over with your LCD.While
calling the lcd functions, there would be some changes to the
PORTC. This restores the original PORTC configuration before
calling the lcd_init() function. This is not absolutely necessary.
You may or may not use this function.
*
*
*
*/
#define _LCD_MEGA128_INCLUDED_
#define BIT(x) (1 << (x))
#define CHECKBIT(x,b) (x&b) //Checks bit status
#define SETBIT(x,b) x|=b; //Sets the particular bit
#define CLEARBIT(x,b) x&=~b; //Sets the particular bit
#define TOGGLEBIT(x,b) x^=b; //Toggles the particular bit
#define LINE1 0x80
#define LINE2 0xC0
#define PORTUSED PORTA
#define DDRUSED DDRA
unsigned char k=0,j=0;
unsigned char save=0;
void lcd_init(void);
void lcd_cmd(unsigned char cmd);
void toggleE(void);
void dely_ns(unsigned char tim);
void lcd_char(unsigned char single);
void lcd_string(unsigned char str[32]);
void lcd_showvalue(unsigned char num);
void lcd_gotoxy1(unsigned char pos);
void lcd_gotoxy2(unsigned char pos);
void lcd_exit(void);
void WaitMs(unsigned int ms);
void lcd_init(void) //init the lcd
{
save=PORTUSED;
DDRUSED=0xFF;
//DDRB=0xFF;
WaitMs(15);
CLEARBIT(PORTUSED,BIT(0)+BIT(1)+BIT(2));
PORTUSED=0x20;
lcd_cmd(0x01);
lcd_cmd(0x0F);
lcd_cmd(0x02);
lcd_cmd(LINE1);
WaitMs(15);
}
void toggleE(void)
{
SETBIT(PORTUSED,BIT(2));
dely_ns(250);
CLEARBIT(PORTUSED,BIT(2));
}
void dely_ns(unsigned char tim)
{
for(j=0;j<tim;j++)
{
for(k=0;k<10;k++)
{};
}
}
void lcd_cmd(unsigned char cmd)
{
CLEARBIT(PORTUSED,BIT(0)+BIT(1)+BIT(2));
PORTUSED=((cmd&0xF0));
toggleE();
PORTUSED=(((cmd&(0x0F))*16));
toggleE();
}
void lcd_putchar(unsigned char sig)
{
PORTUSED=0x01;
CLEARBIT(PORTUSED,BIT(0)+BIT(2));
PORTUSED=(sig&0xF0)|0x01;
toggleE();
PORTUSED=((sig&(0x0F))*16)|0x01;
toggleE();
}
void lcd_char(unsigned char single)
{
int i;
if(single =='\t')
{
for(i =0 ; i<2 ; i++)
{
lcd_putchar(single);
}
}
else if(single=='\n')
{
lcd_gotoxy2(0);
}
else
{
lcd_putchar(single);
}
}
void lcd_string(unsigned char str[32])
{
unsigned char k=0;
PORTUSED=0x01;
CLEARBIT(PORTUSED,BIT(0)+BIT(2));
while(str[k]!='\0') //Till null character is encountered
{
if(k==16)
lcd_cmd(LINE2);
PORTUSED=((str[k])&0xF0)|0x01;
toggleE();
PORTUSED=((str[k]&(0x0F))*16)|0x01;
toggleE();
k++;
}
}
void lcd_showvalue(unsigned char num) //prints the decimal 3digit value of num
{
unsigned char H=0,T=0,O=0;
H=num/100;
T=(num - (H*100))/10;
O=(num - (H*100) - (T*10));
lcd_char(H+48);
lcd_char(T+48);
lcd_char(O+48);
}
void lcd_gotoxy1(unsigned char pos)
{
lcd_cmd(LINE1+pos);
}
void lcd_gotoxy2(unsigned char pos)
{
lcd_cmd(LINE2+pos);
}
void lcd_exit(void)
{
PORTUSED=save;
}
/* waits (pauses) for ms milliseconds (assumes clock at 16MHz) */
void WaitMs(unsigned int ms)
{
int i;
while (ms-- > 0)
{
/* 16380 (16k) clock cycles for 1ms; each time through loop
is 5 cycles (for loop control + nop) */
for (i = 0; i < 3276; ++i)
asm("nop");
}
}