-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD_I2C.C
94 lines (82 loc) · 1.78 KB
/
LCD_I2C.C
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
#include "LCD_I2C.H"
uint8_t LCDOUT=0x00;
void pulseEN();
void send();
void set_backlight(){
LCDOUT|=BL;
}
void lcd_write(uint8_t value,uint8_t mode){
if(mode)
LCDOUT |= RS;
else
LCDOUT &= ~RS;
LCDOUT=((LCDOUT & 0x0F)|(value & 0xF0));
pulseEN();
LCDOUT=((LCDOUT & 0x0F)|(value<<4 & 0xF0));
pulseEN();
}
void lcd_print(char *s){
while(*s){
lcd_write(*s,DATA);
s++;
}
}
void delay(uint16_t t){
for(t;t>0;t--){
__delay_cycles(100);
}
}
void pulseEN(){
LCDOUT|=EN;
send();
delay(1);
LCDOUT&=~EN;
send();
delay(1);
}
void send(){
UCB0CTL1&=~UCTXSTP;
UCB0CTL1 |= UCTXSTT;
while(UCB0CTL1&UCTXSTT);
UCB0CTL1&=~UCTXSTP;
}
void lcd_init(uint8_t addr){
P1SEL|=BIT6+BIT7;
P1SEL2|=BIT6+BIT7;
UCB0CTL1 |= UCSWRST;
UCB0CTL0|=UCMST+UCMODE_3+UCSYNC;
UCB0CTL1 = UCSSEL_2 + UCSWRST+UCTR;
UCB0BR0 = 11;
UCB0BR1 = 0;
UCB0I2CSA=addr;
UCB0CTL1 &= ~UCSWRST;
IE2 |= UCB0TXIE;
IFG2&=~IFG2;
__bis_SR_register(GIE);
delay(100);
lcd_write(0x33,CMD);
delay(50);
lcd_write(0x32,CMD);
delay(50);
lcd_write(0x28,CMD);
delay(50);
lcd_write(0x0C, CMD); // Display ON, Cursor OFF, Blink OFF
delay(10);
lcd_write(0x01,CMD);
delay(40);
lcd_write(0x06,CMD);
delay(1);
lcd_setcursor(0,0);
}
void lcd_setcursor(uint8_t row,uint8_t col){
uint8_t row_offset[]={0x00,0x40};
lcd_write(0x80 | (col + row_offset[row]),CMD);
delay(10);
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void){
UCB0TXBUF=LCDOUT;
UCB0CTL1&=~UCTXSTT;
UCB0CTL1|=UCTXSTP;
IFG2&=~UCB0TXIFG;
}