Subversion Repositories group.electronics

Rev

Rev 49 | Rev 52 | 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
 
51 pfowler 43
void lcd_pergraph(uint8_t val, uint8_t col, uint8_t row) {
49 pfowler 44
	uint8_t i = 0;
45
	uint8_t c = val / 10;	
51 pfowler 46
	lcd_setCursor(col, row);
47
	for (i = 0; i<10; i++) {
48
		if (i < c)
49 pfowler 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;
51 pfowler 95
	lcd_setCursor(col, row);
48 pfowler 96
	uint8_t size = lcd_print(str);
51 pfowler 97
	for (i=size; i<len; i++)
48 pfowler 98
		lcd_char(0x20);	
99
}
100
 
45 pfowler 101
/** End helpers **/
102
 
103
 
42 pfowler 104
void lcd_command(uint8_t data) {
105
	lcd_send(data, LCD_MODE_CM);
106
}
107
 
108
void lcd_char(uint8_t data) {
109
	lcd_send(data, LCD_MODE_RS);
110
}
111
 
48 pfowler 112
uint8_t lcd_print(char* str) {
113
	uint8_t c = 0;
114
	while (str[0] != 0x00) {
115
		lcd_char((uint8_t) str[0]);
116
		str++;
117
		c++;
118
	}
119
	return c;
120
}
121
 
42 pfowler 122
void lcd_send(uint8_t value, uint8_t mode) {
123
	uint8_t high = value & 0xf0;
124
	uint8_t low = (value << 4) & 0xf0;
44 pfowler 125
	lcd_pulse((high)|mode);
126
	lcd_pulse((low)|mode);
42 pfowler 127
}
128
 
47 pfowler 129
void lcd_pulse(uint8_t data) {
42 pfowler 130
	i2c_beginTransmission(LCD_ADDR);
47 pfowler 131
	i2c_writeByte((int) data | LCD_MODE_EN | lcd.backlight);
45 pfowler 132
	i2c_writeByte((int) data | lcd.backlight);
42 pfowler 133
	i2c_endTransmission(1);
47 pfowler 134
	_delay_us(5);
42 pfowler 135
}
48 pfowler 136
 
137
 
138
void lcd_createChar(uint8_t location, uint8_t charmap[]) {
139
        location &= 0x7; // we only have 8 locations 0-7
140
        lcd_command(LCD_SETCGRAMADDR | (location << 3));
141
	uint8_t i = 0;
142
        for (i=0; i<8; i++) {
143
                lcd_char(charmap[i]);
144
        }
145
}