-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd.c
More file actions
139 lines (118 loc) · 2.75 KB
/
lcd.c
File metadata and controls
139 lines (118 loc) · 2.75 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
#include <msp430.h>
#define BL BIT3
#define RS BIT0
#define EN BIT2
#define BR10K 105
#define BR100K 11
#define TRUE 1
#define FALSE 0
typedef unsigned char uint8;
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
ledsconfig();
setUSCIB0master();
UCB0I2CSA = 0x3F;
i2c_write(0);
lcd_inic();
char b[] = "Vini\nBolinhas";
lcdWrite(b);
while(1);
return 0;
}
void setUSCIB0master(){
UCB0CTL1 = UCSWRST;
UCB0CTL0 = UCMST | UCSYNC | UCMODE_3; // Mestre/ Síncrono / I2C
UCB0BRW = BR100K; // Divisor de Freq
UCB0CTL1 = UCSSEL_3; // SMCLK
//Configuração dos pinos (3.0 e 3.1)
P3SEL |= BIT1 | BIT0; // Selecionar pino para função USCI
P3REN |= BIT1 | BIT0;
P3OUT |= BIT1 | BIT0;
}
void lcdbyte(char byte, char rs){
LCDWriteNB(((byte>>4)&0x0F),rs);
LCDWriteNB((byte&0x0F),rs);
}
void ledsconfig(){
P1DIR |= BIT0;
P1OUT &= ~BIT0;
P4DIR |= BIT7;
P4OUT |= ~BIT7;
}
void delay(long x){
volatile long i;
for(i = 0; i < x; i++);
}
void i2c_write(char dado){
UCB0CTL1 |= UCTR | UCTXSTT;
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = dado;
while((UCB0CTL1 & UCTXSTT) == UCTXSTT);
if((UCB0IFG & UCNACKIFG) == UCNACKIFG){
P1OUT |= BIT0;
while(1);
}
P4OUT |= BIT7;
UCB0CTL1 |= UCTXSTP;
while((UCB0CTL1 & UCTXSTP) == UCTXSTP);
}
void LCDWriteNB(uint8 nibble, uint8 ischar){
nibble <<= 4;
i2c_write(nibble | ischar | BL);
i2c_write(nibble | ischar | BL | EN);
i2c_write(nibble | ischar | BL);
}
void lcd_aux(char dado){
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = ((dado<<4)&0XF0) | BL;
delay(50);
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = ((dado<<4)&0XF0) | BL | EN;
delay(50);
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = ((dado<<4)&0XF0) | BL;
}
void lcd_inic(){
UCB0CTL1 |= UCTR | UCTXSTT;
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = 0;
while((UCB0CTL1 & UCTXSTT) == UCTXSTT);
if((UCB0IFG & UCNACKIFG) == UCNACKIFG){
P1OUT |= BIT0;
while(1);
}
lcd_aux(0);
delay(20000);
lcd_aux(3);
delay(10000);
lcd_aux(3);
delay(10000);
lcd_aux(3);
delay(10000);
lcd_aux(2);
//Entrou em modo de 4 bits
lcd_aux(2); lcd_aux(8);
lcd_aux(0); lcd_aux(8);
lcd_aux(0); lcd_aux(1);
lcd_aux(0); lcd_aux(6);
lcd_aux(0); lcd_aux(0xC);
while((UCB0IFG & UCTXIFG) == 0);
UCB0CTL1 |= UCTXSTP;
while((UCB0CTL1 & UCTXSTP) == UCTXSTP);
delay(50);
}
void lcdWrite(char *str){
int cont = 0;
while (!(*str == '\0')){
if ((cont == 16) || (*str == '\n')){
lcdbyte(0xC0, 0);
cont = 0;
str++;
continue;
}
lcdbyte(*str, 1);
str++;
cont++;
}
}