Subversion Repositories group.electronics

Rev

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