Subversion Repositories group.electronics

Rev

Rev 45 | Rev 47 | 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);	
43 pfowler 19
	_delay_ms(150);
20
 
45 pfowler 21
	lcd_pulse(0x30);	
43 pfowler 22
	_delay_ms(150);
23
 
45 pfowler 24
	lcd_pulse(0x30);	
43 pfowler 25
	_delay_ms(50);
26
 
45 pfowler 27
	lcd_pulse(0x20);	
43 pfowler 28
	_delay_ms(50);
42 pfowler 29
 
45 pfowler 30
	lcd_command(LCD_FUNCTIONSET | lcd.function);
43 pfowler 31
	_delay_ms(50);
32
 
45 pfowler 33
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
43 pfowler 34
	_delay_ms(50);
42 pfowler 35
 
46 pfowler 36
	lcd_clear();	
43 pfowler 37
	_delay_ms(50);
42 pfowler 38
 
46 pfowler 39
	lcd_home();
43 pfowler 40
	_delay_ms(50);
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
 
46 pfowler 71
void lcd_setCursor(uint8_t col, uint8_t row) {
72
	uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
73
	lcd_command(LCD_SETDDRAMADDR | (col+ row_offsets[row - 1]));
45 pfowler 74
}
75
 
76
/** End helpers **/
77
 
78
 
42 pfowler 79
void lcd_command(uint8_t data) {
80
	lcd_send(data, LCD_MODE_CM);
81
}
82
 
83
void lcd_char(uint8_t data) {
84
	lcd_send(data, LCD_MODE_RS);
85
}
86
 
87
void lcd_send(uint8_t value, uint8_t mode) {
88
	uint8_t high = value & 0xf0;
89
	uint8_t low = (value << 4) & 0xf0;
44 pfowler 90
	lcd_pulse((high)|mode);
91
	lcd_pulse((low)|mode);
42 pfowler 92
}
93
 
94
void lcd_write(uint8_t data) {
95
	i2c_beginTransmission(LCD_ADDR);
45 pfowler 96
	i2c_writeByte((int) data | lcd.backlight);
42 pfowler 97
	i2c_endTransmission(1);
98
}
99
 
100
void lcd_pulse(uint8_t data) {
101
	lcd_write(data | LCD_MODE_EN);
43 pfowler 102
	_delay_us(40);
42 pfowler 103
 
43 pfowler 104
	//lcd_write(data & ~LCD_MODE_EN);
45 pfowler 105
	lcd_write(data);
43 pfowler 106
	_delay_us(40);
42 pfowler 107
}