Subversion Repositories group.electronics

Rev

Rev 54 | 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) {
55 pfowler 76
	uint8_t c = 1;
77
	char* oldstr = str;
78
        while ((++str)[0] != 0x00)
79
                c++;
54 pfowler 80
 
55 pfowler 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]);
54 pfowler 88
}
89
 
52 pfowler 90
uint8_t lcd_print(char* str) {
91
        uint8_t c = 0;
53 pfowler 92
        while ((str++)[0] != 0x00) {
93
                lcd_char((uint8_t) (str-1)[0]);
52 pfowler 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;
53 pfowler 101
        lcd_setCursor(col, row);
52 pfowler 102
 
103
        uint8_t size = lcd_print(str);
104
        for (i=size; i<=len; i++)
105
                lcd_char(0x20);
106
}
107
 
108
 
109
/************************/
45 pfowler 110
/** Helper functions **/
52 pfowler 111
/************************/
46 pfowler 112
 
113
inline void lcd_clear() {
45 pfowler 114
	lcd_command(LCD_CLEARDISPLAY);
115
}
116
 
46 pfowler 117
inline void lcd_home() {
45 pfowler 118
	lcd_command(LCD_RETURNHOME);
119
}
120
 
46 pfowler 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() {
45 pfowler 130
	lcd.display |= LCD_DISPLAYON;
131
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
132
}
133
 
46 pfowler 134
void lcd_noDisplay() {
45 pfowler 135
	lcd.display &= ~LCD_DISPLAYON;
136
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
137
}
138
 
53 pfowler 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
 
48 pfowler 149
void lcd_autoscroll() {
150
        lcd.display |= LCD_ENTRYSHIFTINCREMENT;
151
        lcd_command(LCD_ENTRYMODESET | lcd.display);
152
}
153
 
52 pfowler 154
void lcd_noAutoscroll() {
155
        lcd.display &= ~LCD_ENTRYSHIFTINCREMENT;
156
        lcd_command(LCD_ENTRYMODESET | lcd.display);
157
}
158
 
46 pfowler 159
void lcd_setCursor(uint8_t col, uint8_t row) {
160
	uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
48 pfowler 161
	lcd_command(LCD_SETDDRAMADDR | ((col - 1)+ row_offsets[row]));
45 pfowler 162
}
163
 
52 pfowler 164
/************************/
165
/** Low level functions **/
166
/************************/
48 pfowler 167
 
42 pfowler 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;
44 pfowler 179
	lcd_pulse((high)|mode);
180
	lcd_pulse((low)|mode);
42 pfowler 181
}
182
 
47 pfowler 183
void lcd_pulse(uint8_t data) {
42 pfowler 184
	i2c_beginTransmission(LCD_ADDR);
47 pfowler 185
	i2c_writeByte((int) data | LCD_MODE_EN | lcd.backlight);
45 pfowler 186
	i2c_writeByte((int) data | lcd.backlight);
42 pfowler 187
	i2c_endTransmission(1);
47 pfowler 188
	_delay_us(5);
42 pfowler 189
}
48 pfowler 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
}