Subversion Repositories group.electronics

Rev

Rev 47 | Rev 49 | 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"
43 pfowler 4
#include "macros.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
 
45 pfowler 43
/** Helper functions **/
46 pfowler 44
 
45
inline void lcd_clear() {
45 pfowler 46
	lcd_command(LCD_CLEARDISPLAY);
47
}
48
 
46 pfowler 49
inline void lcd_home() {
45 pfowler 50
	lcd_command(LCD_RETURNHOME);
51
}
52
 
46 pfowler 53
inline void lcd_backlight() {
54
	lcd.backlight = LCD_BACKLIGHT;
55
}
56
 
57
inline void lcd_noBacklight() {
58
	lcd.backlight = LCD_NOBACKLIGHT;
59
}
60
 
61
void lcd_display() {
45 pfowler 62
	lcd.display |= LCD_DISPLAYON;
63
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
64
}
65
 
46 pfowler 66
void lcd_noDisplay() {
45 pfowler 67
	lcd.display &= ~LCD_DISPLAYON;
68
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
69
}
70
 
48 pfowler 71
void lcd_autoscroll() {
72
        lcd.display |= LCD_ENTRYSHIFTINCREMENT;
73
        lcd_command(LCD_ENTRYMODESET | lcd.display);
74
}
75
 
46 pfowler 76
void lcd_setCursor(uint8_t col, uint8_t row) {
77
	uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
48 pfowler 78
	lcd_command(LCD_SETDDRAMADDR | ((col - 1)+ row_offsets[row]));
45 pfowler 79
}
80
 
48 pfowler 81
void lcd_overprint(char * str, uint8_t len, uint8_t col, uint8_t row) {
82
	uint8_t i = 0;
83
	for (i=0; i<=len; i++)
84
		lcd_char(0x20);	
85
 
86
	lcd_setCursor(row, col);
87
 
88
	uint8_t size = lcd_print(str);
89
	for (i=size; i<=len; i++)
90
		lcd_char(0x20);	
91
}
92
 
45 pfowler 93
/** End helpers **/
94
 
95
 
42 pfowler 96
void lcd_command(uint8_t data) {
97
	lcd_send(data, LCD_MODE_CM);
98
}
99
 
100
void lcd_char(uint8_t data) {
101
	lcd_send(data, LCD_MODE_RS);
102
}
103
 
48 pfowler 104
uint8_t lcd_print(char* str) {
105
	uint8_t c = 0;
106
	while (str[0] != 0x00) {
107
		lcd_char((uint8_t) str[0]);
108
		str++;
109
		c++;
110
	}
111
	return c;
112
}
113
 
42 pfowler 114
void lcd_send(uint8_t value, uint8_t mode) {
115
	uint8_t high = value & 0xf0;
116
	uint8_t low = (value << 4) & 0xf0;
44 pfowler 117
	lcd_pulse((high)|mode);
118
	lcd_pulse((low)|mode);
42 pfowler 119
}
120
 
47 pfowler 121
void lcd_pulse(uint8_t data) {
42 pfowler 122
	i2c_beginTransmission(LCD_ADDR);
47 pfowler 123
	i2c_writeByte((int) data | LCD_MODE_EN | lcd.backlight);
45 pfowler 124
	i2c_writeByte((int) data | lcd.backlight);
42 pfowler 125
	i2c_endTransmission(1);
47 pfowler 126
	_delay_us(5);
42 pfowler 127
}
48 pfowler 128
 
129
 
130
void lcd_createChar(uint8_t location, uint8_t charmap[]) {
131
        location &= 0x7; // we only have 8 locations 0-7
132
        lcd_command(LCD_SETCGRAMADDR | (location << 3));
133
	uint8_t i = 0;
134
        for (i=0; i<8; i++) {
135
                lcd_char(charmap[i]);
136
        }
137
}