Subversion Repositories group.electronics

Rev

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