Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
117 pfowler 1
#include <avr/io.h>
2
#include <stdlib.h>
3
#include "wire.h"
4
#include "lcd.h"
5
#include "avrutil.h"
6
#include <util/delay.h>
7
 
8
void lcd_init() {
9
	lcd.backlight = LCD_BACKLIGHT;
10
	lcd.display = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
11
	lcd.function = LCD_2LINE;
12
 
13
	lcd_pulse(0x30);	
14
	_delay_ms(50);
15
 
16
	lcd_pulse(0x30);	
17
	_delay_ms(50);
18
 
19
	lcd_pulse(0x30);	
20
	_delay_ms(50);
21
 
22
	lcd_pulse(0x20);	
23
	_delay_ms(10);
24
 
25
	lcd_command(LCD_FUNCTIONSET | lcd.function);
26
	_delay_ms(10);
27
 
28
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
29
	_delay_ms(10);
30
 
31
	lcd_clear();	
32
	_delay_ms(10);
33
 
34
	lcd_home();
35
	_delay_ms(10);
36
}
37
 
38
 
39
/************************/
40
/** Fancy functions    **/
41
/************************/
42
void lcd_percent_graph(uint8_t val, uint8_t row, uint8_t col) {
43
	uint8_t i = 0;
44
	uint8_t c = val / 10;	
45
	lcd_setCursor(col, row);
46
	for (i = 0; i<10; i++) {
47
		if (i < c)
48
			lcd_char(0xff);
49
		else
50
			lcd_char(0x00);
51
	}
52
}
53
 
54
/************************/
55
/** Character Printing **/
56
/************************/
57
 
58
uint8_t lcd_print_right(char *str) {
59
	uint8_t c = 0;
60
	char* oldstr = str;
61
 
62
	// Find the null terminator
63
	while ((++str)[0] != 0x00)
64
		c++;
65
 
66
	// Print backwards to start 
67
	lcd_autocursorRight();
68
	while (--str != oldstr -1)
69
		lcd_char((uint8_t) str[0]);
70
	lcd_autocursorLeft();
71
 
72
	return c;
73
}
74
 
75
void lcd_overprint_right(char *str, uint8_t len, uint8_t col, uint8_t row) {
76
	uint8_t c = 1;
77
	char* oldstr = str;
78
	while ((++str)[0] != 0x00)
79
		c++;
80
 
81
	lcd_setCursor(col+1, row);
82
	uint8_t i = 0;
83
	for (i = 0; i<(len-c); i++) 
84
		lcd_char(0x20);
85
 
86
	for (i = 0; i<c; i++) 
87
		lcd_char(oldstr[i]);
88
}
89
 
90
uint8_t lcd_print(char* str) {
91
	uint8_t c = 0;
92
	while ((str++)[0] != 0x00) {
93
		lcd_char((uint8_t) (str-1)[0]);
94
		c++;
95
	}
96
	return c;
97
}
98
 
99
void lcd_overprint(char * str, uint8_t len, uint8_t col, uint8_t row) {
100
	uint8_t i = 0;
101
	lcd_setCursor(col, row);
102
 
103
	uint8_t size = lcd_print(str);
104
	for (i=size; i<=len; i++)
105
		lcd_char(0x20);
106
}
107
 
108
 
109
/************************/
110
/** Helper functions **/
111
/************************/
112
 
113
inline void lcd_clear() {
114
	lcd_command(LCD_CLEARDISPLAY);
115
}
116
 
117
inline void lcd_home() {
118
	lcd_command(LCD_RETURNHOME);
119
}
120
 
121
inline void lcd_backlight() {
122
	lcd.backlight = LCD_BACKLIGHT;
123
}
124
 
125
inline void lcd_noBacklight() {
126
	lcd.backlight = LCD_NOBACKLIGHT;
127
}
128
 
129
void lcd_display() {
130
	lcd.display |= LCD_DISPLAYON;
131
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
132
}
133
 
134
void lcd_noDisplay() {
135
	lcd.display &= ~LCD_DISPLAYON;
136
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
137
}
138
 
139
void lcd_autocursorLeft(void) {
140
	lcd.display |= LCD_ENTRYLEFT;
141
	lcd_command(LCD_ENTRYMODESET | lcd.display);
142
}
143
 
144
void lcd_autocursorRight(void) {
145
	lcd.display &= ~LCD_ENTRYLEFT;
146
	lcd_command(LCD_ENTRYMODESET | lcd.display);
147
}
148
 
149
void lcd_autoscroll() {
150
	lcd.display |= LCD_ENTRYSHIFTINCREMENT;
151
	lcd_command(LCD_ENTRYMODESET | lcd.display);
152
}
153
 
154
void lcd_noAutoscroll() {
155
	lcd.display &= ~LCD_ENTRYSHIFTINCREMENT;
156
	lcd_command(LCD_ENTRYMODESET | lcd.display);
157
}
158
 
159
void lcd_setCursor(uint8_t col, uint8_t row) {
160
	uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
161
	lcd_command(LCD_SETDDRAMADDR | ((col - 1)+ row_offsets[row]));
162
}
163
 
164
/************************/
165
/** Low level functions **/
166
/************************/
167
 
168
void lcd_command(uint8_t data) {
169
	lcd_send(data, LCD_MODE_CM);
170
}
171
 
172
void lcd_char(uint8_t data) {
173
	lcd_send(data, LCD_MODE_RS);
174
}
175
 
176
void lcd_send(uint8_t value, uint8_t mode) {
177
	uint8_t high = value & 0xf0;
178
	uint8_t low = (value << 4) & 0xf0;
179
	lcd_pulse((high)|mode);
180
	lcd_pulse((low)|mode);
181
}
182
 
183
void lcd_pulse(uint8_t data) {
184
	i2c_beginTransmission(LCD_ADDR);
185
	i2c_writeByte((int) data | LCD_MODE_EN | lcd.backlight);
186
	i2c_writeByte((int) data | lcd.backlight);
187
	i2c_endTransmission(1);
188
	_delay_us(5);
189
}
190
 
191
void lcd_createChar(uint8_t location, uint8_t charmap[]) {
192
	location &= 0x7; // we only have 8 locations 0-7
193
	lcd_command(LCD_SETCGRAMADDR | (location << 3));
194
	uint8_t i = 0;
195
	for (i=0; i<8; i++) {
196
		lcd_char(charmap[i]);
197
	}
198
}