-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcd16x2_msp43x.c
261 lines (206 loc) · 8.82 KB
/
lcd16x2_msp43x.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************************
* LCD 16x2 / 16x4 using 4bit mode - lcd16x2_msp43x.c
* - Developed focusing on the MSP430 and MSP432, but can be adapted to other hw
* - Easy to configure and adapt in each new project (changing some #define in .h)
* - with configuration error detection (will show some error if something go wrong)
* - with simple conversion of integer and float to print on lcd
* - - I encourage you to use my "Embedded_Printf" together (convert the number to string)
*
* author: Haroldo Amaral - agaelema@gmail.com
* v0.2.1 - 2017/12/28
***************************************************************************************
* log:
* v0.1 . Initial version without configuration macros
* v0.2.1 + add macros to facilitate the configuration
* . update some functions
* v0.2.2 - Remove unused parts
***************************************************************************************/
#include "lcd16x2_msp43x.h"
#include <stdint.h>
/***************************************************************************************
* MACROS - Do not change
***************************************************************************************/
#define _SET_PIN(port, pin) P ## port ## OUT |= BIT ## pin
#define _CLEAR_PIN(port, pin) P ## port ## OUT &= ~BIT ## pin
#define _EN_SET(port, pin) _SET_PIN(port, pin)
#define EN_SET _EN_SET(_EN_PORT, _EN_PIN) // macro to set EN pin
#define _EN_CLEAR(port, pin) _CLEAR_PIN(port, pin)
#define EN_CLEAR _EN_CLEAR(_EN_PORT, _EN_PIN) // macro to clear EN pin
#define _RS_SET(port, pin) _SET_PIN(port, pin)
#define RS_SET _RS_SET(_RS_PORT, _RS_PIN) // macro to set RS pin
#define _RS_CLEAR(port, pin) _CLEAR_PIN(port, pin)
#define RS_CLEAR _RS_CLEAR(_RS_PORT, _RS_PIN) // macro to clear RS pin
#define _LCD_DATA_PORT(port) P ## port ## OUT
#define _LCD_DATA_OUT(port) _LCD_DATA_PORT(port)
#define LCD_DATA_OUT _LCD_DATA_OUT(_LCD_4BIT_PORT) // macro defining data port
#define LCD_EN_STROBE EN_CLEAR; EN_SET; EN_CLEAR;
/***************************************************************************************
*
***************************************************************************************/
static void lcd16x2_Wait(volatile uint_fast16_t wait_1)
{
volatile int wait_2;
for (wait_1; wait_1>1; wait_1--)
{
for (wait_2 = 110; wait_2 >= 0; wait_2--);
}
}
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_Init(void)
{
EN_CLEAR;
RS_CLEAR;
lcd16x2_Wait(1100); // initialization delay - wait +- 15ms
lcd16x2_Cmd(LCD_CMD_INIT_4BIT); // force 4bit mode after power up
LCD_EN_STROBE;
lcd16x2_Wait(150);
LCD_EN_STROBE;
lcd16x2_Wait(150);
LCD_EN_STROBE;
lcd16x2_Wait(150);
lcd16x2_Cmd(LCD_CMD_RETURN_HOME); // move cursor to home
LCD_EN_STROBE;
lcd16x2_Wait(100);
lcd16x2_Cmd(LCD_CMD_CLEAR_LCD); // clear display
lcd16x2_Wait(100);
lcd16x2_Cmd(LCD_CMD_2LINES_4BIT); // configure in 4bit mode 2 lines
lcd16x2_Wait(100);
lcd16x2_Cmd(LCD_CMD_DISPLAY_ON_CURSOR_ON); // display ON, cursor ON
lcd16x2_Wait(100);
// lcd16x2_Cmd(LCD_CMD_DISPLAY_ON_CURSOR_BLINK); // display ON, cursor BLINK
// lcd16x2_Wait(100);
lcd16x2_Cmd(LCD_CMD_ENTRY_INCREMENT_SHIFT_OFF); // increment address without shift
lcd16x2_Wait(100);
lcd16x2_Cmd(LCD_CMD_CURSOR_1ST_LINE); // put cursor at first line
lcd16x2_Wait(100);
}
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_Cmd(uint_fast8_t Data)
{
RS_CLEAR;
EN_CLEAR;
LCD_DATA_OUT &= ~(_LCD_4BIT_HIGH << _LCD_DATA_BASE); // reset data pins
LCD_DATA_OUT |= (((Data >> 4) & _LCD_4BIT_HIGH) << _LCD_DATA_BASE); // send MSB first
EN_SET;
lcd16x2_Wait(6);
EN_CLEAR;
LCD_DATA_OUT &= ~(_LCD_4BIT_HIGH << _LCD_DATA_BASE); // reset data pins
LCD_DATA_OUT |= (Data & _LCD_4BIT_HIGH) << _LCD_DATA_BASE; // send the LSB
EN_SET;
lcd16x2_Wait(6);
EN_CLEAR;
}
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_Data(uint_fast8_t char_lcd)
{
RS_SET;
EN_CLEAR;
LCD_DATA_OUT &= ~ (_LCD_4BIT_HIGH << _LCD_DATA_BASE); // reset data pins
LCD_DATA_OUT |= (((char_lcd >> 4) & _LCD_4BIT_HIGH) << _LCD_DATA_BASE); // send MSB first
EN_SET;
lcd16x2_Wait(6);
EN_CLEAR;
LCD_DATA_OUT &= ~ (_LCD_4BIT_HIGH << _LCD_DATA_BASE); // reset data pins
LCD_DATA_OUT |= ((char_lcd & _LCD_4BIT_HIGH) << _LCD_DATA_BASE); // send LSB
EN_SET;
lcd16x2_Wait(6);
EN_CLEAR;
}
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_SetPosition(uint_fast8_t row, uint_fast8_t coll)
{
if(row) coll |= 0x40;
coll |=0x80;
lcd16x2_Cmd(coll);
}
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_PrintString(char *str)
{
while (*str) // while different of "\0"
{
lcd16x2_Data(*str); // print the char
str++; // increment pointer
}
}
/*
* versão muito limitada
* - indicar o uso da embedded_printf
* Converte um número inteiro para ser escrito no LCD de -9999 a 9999
*/
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_PrintInt(volatile int_fast16_t iNumber)
{
uint_fast8_t thousands, hundreds, tens, ones;
if (iNumber < 0)
{
lcd16x2_Data('-');
iNumber = iNumber * (-1);
}
thousands = iNumber / 1000;
lcd16x2_Data(thousands + '0'); // convert the number in ascii code
hundreds = ((iNumber - thousands*1000)-1) / 100;
lcd16x2_Data(hundreds + '0'); // convert the number in ascii code
tens = (iNumber % 100) / 10;
lcd16x2_Data(tens + '0'); // convert the number in ascii code
ones = iNumber % 10;
lcd16x2_Data(ones + '0'); // convert the number in ascii code
}
/*
* utiliza "car_float" como double para garantir que o número não seja arredondado
* rotina atual aceita caracteres de -9999.9999 a 9999.9999
*/
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_PrintFloat(volatile float var_float)
{
int_fast16_t var_int;
if (var_float < 0) // if negative
{
var_float = var_float * (-1); // multiply by -1
lcd16x2_Data('-'); // put minus symbol
}
var_int = (int_fast16_t) var_float; // typecast - convert to integer
lcd16x2_PrintInt(var_int); // use integer function
lcd16x2_Wait(2);
lcd16x2_Data('.'); // write the dot "." - 0x2E
var_float = (var_float - var_int)*10000; // multiply the residual
var_int = (int_fast16_t) var_float; // typecast - convert to integer
lcd16x2_PrintInt(var_int); // use integer function
}
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_ShitftRigh(uint_fast8_t times)
{
while(times > 0)
{
lcd16x2_Cmd(LCD_CMD_SHIFT_DISPLAY_RIGHT);
lcd16x2_Wait(2);
times--;
}
}
/***************************************************************************************
*
***************************************************************************************/
void lcd16x2_ShitftLeft(uint_fast8_t times)
{
while(times)
{
lcd16x2_Cmd(LCD_CMD_SHIFT_DISPLAY_LEFT);
lcd16x2_Wait(2);
times--;
}
}