Subversion Repositories group.electronics

Rev

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