-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlcd.c
More file actions
155 lines (150 loc) · 4.88 KB
/
lcd.c
File metadata and controls
155 lines (150 loc) · 4.88 KB
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
//********************************************************************************************
//
// File : lcd.c implement for 16x2 LCD module
//
//********************************************************************************************
//
// Copyright (C) 2007
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
// This program is distributed in the hope that it will be useful, but
//
// WITHOUT ANY WARRANTY;
//
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin St, Fifth Floor, Boston, MA 02110, USA
//
// http://www.gnu.de/gpl-ger.html
//
//********************************************************************************************
#include "includes.h"
//********************************************************************************************
//
// Function : lcd_send_nibble
// Description : Send data (nibble) to lcd module
//
//********************************************************************************************
void lcd_send_nibble(unsigned char data)
{
data &= 0xF0;
LCD_DATA_PORT &= 0x0F;
LCD_DATA_PORT |= data;
_delay_us(1); // 1us
LCD_CONTROL_PORT |= _BV(LCD_EN_PIN);
_delay_us(2);
LCD_CONTROL_PORT &= ~_BV(LCD_EN_PIN);
}
//********************************************************************************************
//
// Function : lcd_send_byte
// Description : Send data (byte) to lcd module
//
//********************************************************************************************
void lcd_send_byte( char data_or_cmd, char data )
{
LCD_CONTROL_PORT &= ~_BV(LCD_RS_PIN);
if(data_or_cmd)
LCD_CONTROL_PORT |= _BV(LCD_RS_PIN);
else
LCD_CONTROL_PORT &= ~_BV(LCD_RS_PIN);
_delay_us(50); // 1us
LCD_CONTROL_PORT &= ~_BV(LCD_EN_PIN);
lcd_send_nibble(data & 0xF0);
lcd_send_nibble(data << 4);
}
//********************************************************************************************
//
// Function : lcd_init
// Description : Lcd module initiation.(4-bits mode)
//
//********************************************************************************************
void lcd_init(void)
{
char i;
LCD_DATA_DDR |= (_BV(LCD_D7) | _BV(LCD_D6) | _BV(LCD_D5) | _BV(LCD_D4));
LCD_CONTROL_DDR |= (_BV(LCD_RS_PIN) | _BV(LCD_RW_PIN) | _BV(LCD_EN_PIN));
LCD_DATA_PORT &= ~(_BV(LCD_D7) | _BV(LCD_D6) | _BV(LCD_D5) | _BV(LCD_D4));
LCD_CONTROL_PORT &= ~(_BV(LCD_RS_PIN) | _BV(LCD_RS_PIN) | _BV(LCD_RS_PIN));
_delay_ms(15); // 15 ms
for(i=1;i<=3;++i)
{
lcd_send_nibble(0x30);
_delay_ms(5); // 5 ms
}
lcd_send_nibble(0x20);
lcd_send_byte(WRITE_COMMAND, SET_FUNCTION);
lcd_send_byte(WRITE_COMMAND, DISPLAY_ON);
lcd_send_byte(WRITE_COMMAND, DISPLAY_CLR);
lcd_send_byte(WRITE_COMMAND, ENTRY_MODE);
}
//********************************************************************************************
//
// Function : lcd_gotoxy
// Description : Send SET_DDRAM command to lcd module
//
//********************************************************************************************
void lcd_gotoxy( unsigned char x, unsigned char y)
{
char address=0;
if(y!=1)
address = LCD_LINE_TWO;
address += x-1;
lcd_send_byte(WRITE_COMMAND, SET_DDRAM|address);
}
//********************************************************************************************
//
// Function : lcd_putc
// Description : Send data(byte) or command to lcd module
// '\f' is clear display command
// '\n' is new line (second line) command
// '\b' is cursor back command
//
//********************************************************************************************
void lcd_putc( unsigned char c)
{
if(c == '\f')
{
lcd_send_byte(WRITE_COMMAND, DISPLAY_CLR);
_delay_ms(2); // 2ms
}
else if(c == '\n')
lcd_gotoxy(1, 2);
else if(c == '\b')
lcd_send_byte(WRITE_COMMAND, CURSOR_BACK);
else
lcd_send_byte(WRITE_DATA, c);
}
//********************************************************************************************
//
// Function : lcd_print
// Description : print string from ram to lcd module
//
//********************************************************************************************
void lcd_print( BYTE *ptr )
{
while( *ptr )
{
lcd_putc(*ptr++);
}
}
//********************************************************************************************
//
// Function : lcd_print_p
// Description : print string from program memory to lcd module
//
//********************************************************************************************
void lcd_print_p( PGM_P ptr )
{
unsigned char c;
while( (c = pgm_read_byte ( ptr++ )) )
{
lcd_putc(c);
}
}