Subversion Repositories group.electronics

Rev

Rev 53 | Rev 55 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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