-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdutils.c
161 lines (139 loc) · 3.81 KB
/
lcdutils.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
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
/** \file lcdutils.c:
*
* \brief Created on: 10/19/2016
* Author: Eric Freudenthal & David Pruitt
* Derived from EduKit code by RobG
* Chip select: P1.0
* Data/Cmd: P1.4
* Buzzer: P2.6 (default)
*/
#include "lcdutils.h"
#include "msp430.h"
u_char _orientation = 0;
/** LCD pin definitions*/
/** SCLK & MOSI*/
#define LCD_SPI_OUT P1OUT
#define LCD_SPI_DIR P1DIR
#define LCD_SPI_SEL P1SEL
#define LCD_SPI_SEL2 P1SEL2
#define LCD_SCLK_PIN BIT5
#define LCD_MOSI_PIN BIT7
/** Chip select */
#define LCD_CS_PIN BIT0
#define LCD_CS_DIR P1DIR
#define LCD_CS_OUT P1OUT
/** CS convenience defines */
#define LCD_SELECT() LCD_CS_OUT &= ~LCD_CS_PIN
#define LCD_DESELECT()
/** Data/command */
#define LCD_DC_PIN BIT4
#define LCD_DC_DIR P1DIR
#define LCD_DC_OUT P1OUT
/** D/C convenience defines */
#define LCD_DC_LO() LCD_DC_OUT &= ~LCD_DC_PIN
#define LCD_DC_HI() LCD_DC_OUT |= LCD_DC_PIN
/** LCD driver IC specific defines */
#define SWRESET 0x01
#define SLEEPOUT 0x11
#define DISPON 0x29
#define CASETP 0x2A
#define PASETP 0x2B
#define RAMWRP 0x2C
#define MADCTL 0x36
#define COLMOD 0x3A
#define GMCTRP1 0xE0
#define GMCTRN1 0xE1
/** Set up onboard LCD's SPI and control pins */
static void setUpSPIforLCD() {
LCD_DC_OUT |= LCD_DC_PIN;
LCD_DC_DIR |= LCD_DC_PIN;
LCD_CS_OUT |= LCD_CS_PIN;
LCD_CS_DIR |= LCD_CS_PIN;
LCD_SPI_OUT |= LCD_SCLK_PIN;
LCD_SPI_DIR |= LCD_SCLK_PIN;
LCD_SPI_OUT |= LCD_MOSI_PIN;
LCD_SPI_DIR |= LCD_MOSI_PIN;
LCD_SPI_SEL |= LCD_SCLK_PIN + LCD_MOSI_PIN;
LCD_SPI_SEL2 |= LCD_SCLK_PIN + LCD_MOSI_PIN;
UCB0CTL1 |= UCSWRST;
UCB0CTL0 = UCCKPH + UCMSB + UCMST + UCSYNC; /**< 3-pin, 8-bit SPI master */
UCB0CTL1 |= UCSSEL_2; /**< SMCLK */
UCB0BR0 |= 0x01; /**< 1:1 */
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST;
LCD_SELECT();
}
/** Screen dimensions */
/** Write data to LCD */
static inline void
lcd_writeData(u_char data)
{
while (UCB0STAT & UCBUSY); /**< wait for previous transfer to complete */
LCD_DC_HI(); /**< specify sending data */
UCB0TXBUF = data; /**< send data */
}
typedef union {
u_char colorBytes[2];
u_int colorBGRWord;
} ColorBGR;
void lcd_writeColor(u_int colorBGR)
{
ColorBGR colorU = {.colorBGRWord = colorBGR};
lcd_writeData(colorU.colorBytes[1]);
lcd_writeData(colorU.colorBytes[0]);
}
/** Write command to LCD (private) */
void _writeCommand(u_char command)
{
while (UCB0STAT & UCBUSY); /**< wait for previous transfer to complete */
LCD_DC_LO(); /**< specify sending a command */
UCB0TXBUF = command; /**< send command */
}
/** Long delay (private) */
void _delay(u_char x10ms) {
while (x10ms > 0) {
__delay_cycles(160000);
x10ms--;
}
}
/** Set area to draw to */
void lcd_setArea(u_char colStart, u_char rowStart, u_char colEnd, u_char rowEnd)
{
_writeCommand(CASETP);
lcd_writeData(0);
lcd_writeData(colStart);
lcd_writeData(0);
lcd_writeData(colEnd);
_writeCommand(PASETP);
lcd_writeData(0);
lcd_writeData(rowStart);
lcd_writeData(0);
lcd_writeData(rowEnd);
_writeCommand(RAMWRP);
}
/** Initialize onboard LCD */
void lcd_init()
{
setUpSPIforLCD();
_writeCommand(SWRESET); /**< software reset */
_delay(20);
_writeCommand(SLEEPOUT); /**< exit sleep */
_delay(20);
_writeCommand(COLMOD); /**< Set Color Format 16bit */
lcd_writeData(0x05);
_writeCommand(DISPON); /**< display ON */
_writeCommand(MADCTL);
switch (ORIENTATION) {
case ORIENTATION_HORIZONTAL:
lcd_writeData(0x68);
break;
case ORIENTATION_VERTICAL_ROTATED:
lcd_writeData(0x08);
break;
case ORIENTATION_HORIZONTAL_ROTATED:
lcd_writeData(0xA8);
break;
default:
lcd_writeData(0xC8);
}
}