Subversion Repositories group.electronics

Rev

Rev 44 | Rev 46 | 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
 
45 pfowler 36
	i2c_clear();	
43 pfowler 37
	_delay_ms(50);
42 pfowler 38
 
45 pfowler 39
	i2c_home();
43 pfowler 40
	_delay_ms(50);
42 pfowler 41
}
42
 
45 pfowler 43
/** Helper functions **/
44
void i2c_clear() {
45
	lcd_command(LCD_CLEARDISPLAY);
46
}
47
 
48
void i2c_home() {
49
	lcd_command(LCD_RETURNHOME);
50
}
51
 
52
void i2c_display() {
53
	lcd.display |= LCD_DISPLAYON;
54
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
55
}
56
 
57
void i2c_noDisplay() {
58
	lcd.display &= ~LCD_DISPLAYON;
59
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
60
}
61
 
62
void i2c_backlight() {
63
	lcd.backlight = LCD_BACKLIGHT;
64
}
65
 
66
void i2c_noBacklight() {
67
	lcd.backlight = LCD_NOBACKLIGHT;
68
}
69
 
70
 
71
/** End helpers **/
72
 
73
 
42 pfowler 74
void lcd_command(uint8_t data) {
75
	lcd_send(data, LCD_MODE_CM);
76
}
77
 
78
void lcd_char(uint8_t data) {
79
	lcd_send(data, LCD_MODE_RS);
80
}
81
 
82
void lcd_send(uint8_t value, uint8_t mode) {
83
	uint8_t high = value & 0xf0;
84
	uint8_t low = (value << 4) & 0xf0;
44 pfowler 85
	lcd_pulse((high)|mode);
86
	lcd_pulse((low)|mode);
42 pfowler 87
}
88
 
89
void lcd_write(uint8_t data) {
90
	i2c_beginTransmission(LCD_ADDR);
45 pfowler 91
	i2c_writeByte((int) data | lcd.backlight);
42 pfowler 92
	i2c_endTransmission(1);
93
}
94
 
95
void lcd_pulse(uint8_t data) {
96
	lcd_write(data | LCD_MODE_EN);
43 pfowler 97
	_delay_us(40);
42 pfowler 98
 
43 pfowler 99
	//lcd_write(data & ~LCD_MODE_EN);
45 pfowler 100
	lcd_write(data);
43 pfowler 101
	_delay_us(40);
42 pfowler 102
}