-
Notifications
You must be signed in to change notification settings - Fork 3
/
display.cpp
166 lines (118 loc) · 3.52 KB
/
display.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "display.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#define Takt_TWI 400000
#define TWI_BAUD(F_SYS, F_TWI) ((F_SYS / (2 * F_TWI)) - 5)
#define TWI_BAUDRATE TWI_BAUD(F_CPU, Takt_TWI)
uint8_t displayfunction = 0x00;
uint8_t displaycontrol = 0x00;
uint8_t displaymode = 0x00;
uint8_t backlightVal = LCD_BACKLIGHT;
void setupTWI(void) {
TWIC.MASTER.BAUD = TWI_BAUDRATE;
TWIC.MASTER.CTRLA = TWI_MASTER_WIF_bm | TWI_MASTER_ENABLE_bm | TWI_MASTER_INTLVL_gm; //Enable Write Interupt Flag, Enable Master, Interrupt Level 0
//RESET Bus State
TWIC.MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
}
void twiWrite(unsigned char byte) {
TWIC.MASTER.ADDR = LCD_ADDR;
while((TWIC.MASTER.STATUS & TWI_MASTER_WIF_bm) == 0);
TWIC.MASTER.DATA = byte;
while((TWIC.MASTER.STATUS & TWI_MASTER_WIF_bm) == 0);
}
void send(uint8_t value, uint8_t mode);
void write(uint8_t value) {
send(value, Rs);
}
void writeDirect(uint8_t b) {
twiWrite(b | backlightVal);
}
void pulseEnable(uint8_t _data){
writeDirect(_data | En); // En high
_delay_us(1); // enable pulse must be >450ns
writeDirect(_data & ~En); // En low
_delay_us(38); // commands need > 37us to settle
}
void write4bits(uint8_t data) {
writeDirect(data);
pulseEnable(data);
}
void send(uint8_t value, uint8_t mode) {
uint8_t highnib=value&0xf0;
uint8_t lownib=(value<<4)&0xf0;
write4bits((highnib)|mode);
write4bits((lownib)|mode);
}
void command(uint8_t value) {
send(value, 0);
}
void writeStr(const char value[]) {
while(*value != 0) {
send(*value++, Rs);
}
}
void writeInt(uint8_t value) {
char str[4];
itoa(value, str, 10);
writeStr(str);
}
void writeIntWidth(uint8_t value, uint8_t width) {
char str[4];
itoa(value, str, 10);
if(width >= 3 && value < 100) {
write(' ');
}
if(width >= 2 && value < 10) {
write(' ');
}
writeStr(str);
}
/**
* Setup the Display
*/
void setupDisplay(void) {
displayfunction = LCD_4BITMODE | LCD_2LINE | LCD_5x8DOTS;
//put the LCD into 4 bit mode
// this is according to the hitachi HD44780 datasheet
// figure 24, pg 46
//_delay_ms(1000);
// we start in 8bit mode, try to set 4 bit mode
write4bits(0x03 << 4);
_delay_ms(5); // wait min 4.1ms
// second try
write4bits(0x03 << 4);
_delay_ms(5); // wait min 4.1ms
// third go!
write4bits(0x03 << 4);
_delay_ms(1);
// finally, set to 4-bit interface
write4bits(0x02 << 4);
// set # lines, font size, etc.
command(LCD_FUNCTIONSET | displayfunction);
// turn the display on with no cursor or blinking default
displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
command(LCD_DISPLAYCONTROL | displaycontrol);
command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
_delay_ms(20); // this command takes a long time!
// Initialize to default text direction (for roman languages)
displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
// set the entry mode
command(LCD_ENTRYMODESET | displaymode);
command(LCD_RETURNHOME); // set cursor position to zero
_delay_ms(20); // this command takes a long time!
backlightVal = LCD_BACKLIGHT;
}
void setCursor(uint8_t col, uint8_t row) {
int numlines = 2;
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if ( row > numlines ) {
row = numlines-1; // we count rows starting w/0
}
command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
}
void clearDisplay(void) {
command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
_delay_ms(20); // this command takes a long time!
}