-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlcd.c
More file actions
212 lines (138 loc) · 3.86 KB
/
lcd.c
File metadata and controls
212 lines (138 loc) · 3.86 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* lcd.c
* Author: C2C Caleb Ziegler
* Date: 22 Oct 2013
* Description: Implementation for lcd functions
*/
#include "lcd.h"
#include <msp430.h>
#define RS_MASK 0x40
char LCDCON = 0;
void writeCommandNibble(char commandNibble);
void set_SS_hi();
void set_SS_lo();
void SPI_send(char byteToSend);
void rotateString(char * string);
void initSPI() {
UCB0CTL1 |= UCSWRST;
UCB0CTL0 |= UCCKPH | UCMSB | UCMST | UCSYNC;
UCB0CTL1 |= UCSSEL1; //Select clock to use
UCB0STAT |= UCLISTEN; //Enables internal loopback
P1DIR |= BIT0;
P1SEL |= BIT5;
P1SEL2 |= BIT5; //Make UCBOCLK available on P1.5
P1SEL |= BIT7;
P1SEL2 |= BIT7; //Make UCBOSSIMO available on P1.7
P1SEL |= BIT6;
P1SEL2 |= BIT6; //Make UCAOSSOMI available on P1.6
P1DIR |= BIT4;
UCB0CTL1 &= ~UCSWRST; //Enable subsystem
}
void set_SS_hi() {
P1OUT |= BIT4;
}
void set_SS_lo() {
P1OUT &= ~BIT4;
}
void LCDinit() {
writeCommandNibble(0x03);
writeCommandNibble(0x03);
writeCommandNibble(0x03);
writeCommandNibble(0x02);
writeCommandByte(0x28);
writeCommandByte(0x0C);
writeCommandByte(0x01);
writeCommandByte(0x06);
writeCommandByte(0x01);
writeCommandByte(0x02);
SPI_send(0);
__delay_cycles(45);
}
void LCDclear() {
writeCommandByte(1);
}
//Thanks to C2C Jason Mossing for helping me figure this one out
void LCD_write_4(char nibbleToSend) {
unsigned char sendnibble = nibbleToSend;
sendnibble &= 0x0F; //Clear upper half of byte but save lower half
sendnibble |= LCDCON; //Set LCD control nibble
sendnibble &= 0x7F; //Set E low
SPI_send(sendnibble);
__delay_cycles(45);
sendnibble |= 0x80; //Set E high
SPI_send(sendnibble);
__delay_cycles(45);
sendnibble &= 0x7F; //Set E low
SPI_send(sendnibble);
__delay_cycles(45);
}
void LCD_write_8(char byteToSend) {
unsigned char sendByte = byteToSend;
sendByte &= 0xF0;
sendByte = sendByte >> 4; // rotate to the right 4 times
LCD_write_4(sendByte);
sendByte = byteToSend;
sendByte &= 0x0F;
LCD_write_4(sendByte);
}
void SPI_send(char byteToSend) {
char readByte;
set_SS_lo();
UCB0TXBUF = byteToSend;
while (!(UCB0RXIFG & IFG2)) {
// wait until you've received a byte
}
readByte = UCB0RXBUF;
set_SS_hi();
}
void writeCommandNibble(char commandNibble) {
LCDCON &= ~RS_MASK;
LCD_write_4(commandNibble);
__delay_cycles(1824);
}
void writeCommandByte(char commandByte) {
LCDCON &= ~RS_MASK;
LCD_write_8(commandByte);
__delay_cycles(1824);
}
void writeDataByte(char dataByte) {
LCDCON |= RS_MASK;
LCD_write_8(dataByte);
__delay_cycles(1824);
}
void MoveCursorLineOne() {
writeCommandByte(0x02); //0x02 sets cursor at home
}
void MoveCursorLineTwo() {
writeCommandByte(0xC0); //Set cursor at lower left spot
}
void writeString(char *string) {
int i;
for(i = 0; i < 8; i++) //Will only write the first 8 characters of a string to the LCD
{
writeCharacter(string[i]); //Leverages writeCharacter to write characters of the string to the LCD
}
}
void writeCharacter(char asiiCharacter) {
writeDataByte(asiiCharacter);
}
void rotateString(char * string)
{
char firstChar = string[0]; //Saves first character in string for later use
int i;
for (i = 0; string[i+1] != 0; i++) //Stop once the next character in string is null case stop
{
string[i] = string[i+1]; //Rotate string to right by making the current character in string equal the next character in string
}
string[i] = firstChar; //Move previously first character in the string to the end of the string to preserve that character and perform a correct rotate right
}
void scrollString(char *string1, char *string2) {
while (1) {
MoveCursorLineOne();
writeString(string1);
rotateString(string1);
MoveCursorLineTwo();
writeString(string2);
rotateString(string2);
}
}