Subversion Repositories group.electronics

Rev

Rev 44 | Rev 46 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 44 Rev 45
Line 2... Line 2...
2
#include "wire.h"
2
#include "wire.h"
3
#include "lcd.h"
3
#include "lcd.h"
4
#include "macros.h"
4
#include "macros.h"
5
#include <util/delay.h>
5
#include <util/delay.h>
6
 
6
 
-
 
7
struct {
-
 
8
	uint8_t display;
-
 
9
	uint8_t function;
-
 
10
	uint8_t backlight;
-
 
11
} lcd;
7
 
12
 
8
void lcd_init() {
13
void lcd_init() {
-
 
14
	lcd.backlight = LCD_BACKLIGHT;
-
 
15
	lcd.display = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
-
 
16
	lcd.function = LCD_2LINE;
-
 
17
 
9
	lcd_sendNib(0x30);	
18
	lcd_pulse(0x30);	
10
	_delay_ms(150);
19
	_delay_ms(150);
11
 
20
 
12
	lcd_sendNib(0x30);	
21
	lcd_pulse(0x30);	
13
	_delay_ms(150);
22
	_delay_ms(150);
14
 
23
 
15
	lcd_sendNib(0x30);	
24
	lcd_pulse(0x30);	
16
	_delay_ms(50);
25
	_delay_ms(50);
17
 
26
 
18
	lcd_sendNib(0x20);	
27
	lcd_pulse(0x20);	
19
	_delay_ms(50);
28
	_delay_ms(50);
20
 
29
 
21
	lcd_command(LCD_FUNCTIONSET | LCD_2LINE);
30
	lcd_command(LCD_FUNCTIONSET | lcd.function);
22
	_delay_ms(50);
31
	_delay_ms(50);
23
 
32
 
24
	lcd_command(LCD_DISPLAYCONTROL | LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF);
33
	lcd_command(LCD_DISPLAYCONTROL | lcd.display);
25
	_delay_ms(50);
34
	_delay_ms(50);
26
 
35
 
27
	lcd_command(LCD_CLEARDISPLAY);
36
	i2c_clear();	
28
	_delay_ms(50);
37
	_delay_ms(50);
29
 
38
 
30
	lcd_command(LCD_RETURNHOME);
39
	i2c_home();
31
	_delay_ms(50);
40
	_delay_ms(50);
32
}
41
}
33
 
42
 
-
 
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
 
34
void lcd_command(uint8_t data) {
74
void lcd_command(uint8_t data) {
35
	lcd_send(data, LCD_MODE_CM);
75
	lcd_send(data, LCD_MODE_CM);
36
}
76
}
37
 
77
 
38
void lcd_char(uint8_t data) {
78
void lcd_char(uint8_t data) {
39
	lcd_send(data, LCD_MODE_RS);
79
	lcd_send(data, LCD_MODE_RS);
40
}
80
}
41
 
81
 
42
 
-
 
43
void lcd_send(uint8_t value, uint8_t mode) {
82
void lcd_send(uint8_t value, uint8_t mode) {
44
	uint8_t high = value & 0xf0;
83
	uint8_t high = value & 0xf0;
45
	uint8_t low = (value << 4) & 0xf0;
84
	uint8_t low = (value << 4) & 0xf0;
46
	lcd_pulse((high)|mode);
85
	lcd_pulse((high)|mode);
47
	lcd_pulse((low)|mode);
86
	lcd_pulse((low)|mode);
48
}
87
}
49
 
88
 
50
void lcd_sendNib(uint8_t nib) {
-
 
51
	//lcd_write(nib);
-
 
52
	lcd_pulse(nib);
-
 
53
}
-
 
54
 
-
 
55
void lcd_write(uint8_t data) {
89
void lcd_write(uint8_t data) {
56
	i2c_beginTransmission(LCD_ADDR);
90
	i2c_beginTransmission(LCD_ADDR);
57
	i2c_writeByte((int) data | 0x08);
91
	i2c_writeByte((int) data | lcd.backlight);
58
	i2c_endTransmission(1);
92
	i2c_endTransmission(1);
59
}
93
}
60
 
94
 
61
void lcd_pulse(uint8_t data) {
95
void lcd_pulse(uint8_t data) {
62
	lcd_write(data | LCD_MODE_EN);
96
	lcd_write(data | LCD_MODE_EN);
63
	_delay_us(40);
97
	_delay_us(40);
64
 
98
 
65
	//lcd_write(data & ~LCD_MODE_EN);
99
	//lcd_write(data & ~LCD_MODE_EN);
66
	lcd_write(data );
100
	lcd_write(data);
67
	_delay_us(40);
101
	_delay_us(40);
68
}
102
}