-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd.c
92 lines (79 loc) · 2.29 KB
/
lcd.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
#include "i2c.h"
#include "lcd.h"
#include "msp430g2553.h"
void LcdInit()
{
LcdWriteCommand(LCD_INIT_BYTE, 0);
__delay_cycles(80000);
LcdWriteCommand(LCD_INIT_BYTE, 0);
LcdWriteCommand(LCD_INIT_BYTE, 0);
LcdWriteCommand(LCD_BUS_WIDTH_4BIT, 0);
LcdWriteCommand(LCD_4BITS_2LINES_5x8FONT, 1);
LcdWriteCommand(LCD_DISPLAY_OFF_CURSOR_OFF_BLINK_OFF, 1);
LcdWriteCommand(LCD_CLEAR, 1);
LcdWriteCommand(LCD_INCREMENT_NO_SHIFT, 1);
LcdWriteCommand(LCD_DISPLAY_ON_CURSOR_OFF, 1);
}
void LcdWriteCommand(unsigned char data, unsigned char cmdtype)
{
unsigned char byte;
byte = (HI_NIBBLE(data) | LCD_BL) | LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
byte = (HI_NIBBLE(data) | LCD_BL) & ~LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
// cmdtype = 0 -> One write cycle
// cmdtype = 1 -> Two write cycles (4 bit mode)
if (cmdtype)
{
byte = (LO_NIBBLE(data) | LCD_BL) | LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
byte = (LO_NIBBLE(data) | LCD_BL) | ~LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
}
__delay_cycles(80000);
}
void LcdWriteChar(unsigned char data)
{
unsigned char byte;
byte = (HI_NIBBLE(data) | LCD_BL | LCD_RS) | LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
byte = (HI_NIBBLE(data) | LCD_BL | LCD_RS) & ~LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
byte = (LO_NIBBLE(data) | LCD_BL | LCD_RS) | LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
byte = (LO_NIBBLE(data) | LCD_BL | LCD_RS) & ~LCD_EN;
I2cTransmit(0x27, byte);
while(I2cNotReady());
}
void LcdWriteString(char *s)
{
while (*s != '\0')
LcdWriteChar(*s++);
}
void LcdSetPosition(unsigned char row, unsigned char column)
{
switch(row)
{
case 1:
LcdWriteCommand(LCD_LINE1 + (column - 1), 1);
break;
case 2:
LcdWriteCommand(LCD_LINE2 + (column - 1), 1);
break;
case 3:
LcdWriteCommand(LCD_LINE3 + (column - 1), 1);
break;
case 4:
LcdWriteCommand(LCD_LINE4 + (column - 1), 1);
break;
default:
LcdWriteCommand(LCD_LINE1 + (column - 1), 1);
}
}