cursada_mc2
Base de control de temperatura para EDU-CIAA-NXP
Loading...
Searching...
No Matches
lcd_driver.c
Go to the documentation of this file.
1
6#include "lcd_driver.h"
7#include "delay_driver.h"
8
9#define LCD_PORT 4
10#define LCD4 10
11#define LCD3 6
12#define LCD2 5
13#define LCD1 4
14#define LCD_RS 8
15#define LCD_EN 9
16
17#define LCD_IS_DATA 1
18#define LCD_IS_COMMAND 0
19
20void lcd_send(uint8_t nibble, bool is_data)
21{
22 Chip_GPIO_SetPinState(LPC_GPIO_PORT, 5, LCD_RS + 4, is_data);
23
24 const bool bit_0 = (bool) (nibble & 0x01);
25 Chip_GPIO_SetPinState(LPC_GPIO_PORT, 2, LCD1, bit_0);
26
27 const bool bit_1 = (bool) ((nibble >> 1) & 0x01);
28 Chip_GPIO_SetPinState(LPC_GPIO_PORT, 2, LCD2, bit_1);
29
30 const bool bit_2 = (bool) ((nibble >> 2) & 0x01);
31 Chip_GPIO_SetPinState(LPC_GPIO_PORT, 2, LCD3, bit_2);
32
33 const bool bit_3 = (bool) ((nibble >> 3) & 0x01);
34 Chip_GPIO_SetPinState(LPC_GPIO_PORT, 5, LCD4 + 4, bit_3);
35
43 Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, 5, LCD_EN + 4);
45 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 5, LCD_EN + 4);
46 driver_delay_us(50U);
47}
48
49
50void send_byte(uint8_t payload, bool data_type)
51{
58 const uint8_t upper_nibble = (payload >> 4) & 0x0F;
59 lcd_send(upper_nibble, data_type);
60 const uint8_t lower_nibble = payload & 0x0F;
61 lcd_send(lower_nibble, data_type);
62}
63
64
66{
67 Chip_SCU_PinMux(LCD_PORT, LCD_RS, MD_PLN, FUNC4);
68 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, 5, LCD_RS + 4, 1);
69
70 Chip_SCU_PinMux(LCD_PORT, LCD_EN, MD_PLN, FUNC4);
71 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, 5, LCD_EN + 4, 1);
72
73 Chip_SCU_PinMux(LCD_PORT, LCD4, MD_PLN, FUNC4);
74 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, 5, LCD4 + 4, 1);
75
76 Chip_SCU_PinMux(LCD_PORT, LCD3, MD_PLN, FUNC0);
77 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, 2, LCD3, 1);
78
79 Chip_SCU_PinMux(LCD_PORT, LCD2, MD_PLN, FUNC0);
80 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, 2, LCD2, 1);
81
82 Chip_SCU_PinMux(LCD_PORT, LCD1, MD_PLN, FUNC0);
83 Chip_GPIO_SetPinDIR(LPC_GPIO_PORT, 2, LCD1, 1);
84
85 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 5, LCD_RS + 4);
86 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 5, LCD_EN + 4);
87 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 5, LCD4 + 4);
88 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 2, LCD3);
89 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 2, LCD2);
90 Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, 2, LCD1);
91}
92
93
95{
98 driver_delay_ms(50U);
99
107 driver_delay_ms(10U);
109 driver_delay_ms(10U);
111 driver_delay_ms(5U);
113 driver_delay_ms(5U);
114
115 send_byte(0x28, LCD_IS_COMMAND); // 4-bit, 2 lines, 5x8 font
116 driver_delay_ms(2U);
117 send_byte(0x08, LCD_IS_COMMAND); // Display off
118 driver_delay_ms(2U);
119 send_byte(0x01, LCD_IS_COMMAND); // Display clear
120 driver_delay_ms(5U);
121 send_byte(0x06, LCD_IS_COMMAND); // 0b0000 ' 0 1 I/D S - Increment by 1, no shift
122 driver_delay_ms(2U);
123 send_byte(0x0C, LCD_IS_COMMAND); // Display on, cursor off
124 driver_delay_ms(2U);
125}
126
127
128void driver_lcd_set_position(uint8_t x, uint8_t y)
129{
130 if (x < 1 || x > 16) return;
131 if (y < 1 || y > 2) return; // checks limits of display (16x2)
132
133 uint8_t address_position = (y == 1) ? 0x80 : 0xC0;
134 address_position += (uint8_t) (x - 1);
135 send_byte(address_position, LCD_IS_COMMAND);
136}
137
138
140{
141 switch (C) {
142 case '\f':
143 break;
144 case '\n':
145 break;
146 case '\b':
147 send_byte(0x01, LCD_IS_COMMAND); // Clear display screen
148 driver_delay_ms(5U);
149 break;
150 default:
152 break;
153 }
154}
155
156
157void driver_lcd_printf(const char* string)
158{
159 uint16_t i = 0;
160 while (string[i] != '\0') {
161 driver_lcd_write_char(string[i]);
162 i++;
163 }
164}
void driver_delay_us(uint32_t microseconds)
Realiza una espera bloqueante expresada en microsegundos.
void driver_delay_ms(uint32_t milliseconds)
Realiza una espera bloqueante expresada en milisegundos.
void driver_delay_init(void)
Inicializa la base de tiempos usada por los delays bloqueantes.
Interfaz comun de delays bloqueantes basada en stopwatch de LPCOpen.
#define LCD_IS_COMMAND
Definition lcd_driver.c:18
void driver_lcd_init_port()
Definition lcd_driver.c:65
#define LCD_EN
Definition lcd_driver.c:15
void driver_lcd_init(void)
Inicializa el LCD y ejecuta la secuencia de arranque.
Definition lcd_driver.c:94
#define LCD1
LCD1 = D4 on lcd pinout.
Definition lcd_driver.c:13
void driver_lcd_write_char(char C)
Escribe un caracter en la posicion actual del LCD.
Definition lcd_driver.c:139
#define LCD_RS
Definition lcd_driver.c:14
void driver_lcd_set_position(uint8_t x, uint8_t y)
Posiciona el cursor dentro de la matriz visible del LCD.
Definition lcd_driver.c:128
#define LCD4
LCD4 = D7 on lcd pinout.
Definition lcd_driver.c:10
void driver_lcd_printf(const char *string)
Escribe una cadena a partir de la posicion actual del cursor.
Definition lcd_driver.c:157
#define LCD3
LCD3 = D6 on lcd pinout.
Definition lcd_driver.c:11
#define LCD_PORT
Definition lcd_driver.c:9
#define LCD_IS_DATA
Definition lcd_driver.c:17
void send_byte(uint8_t payload, bool data_type)
Definition lcd_driver.c:50
#define LCD2
LCD2 = D5 on lcd pinout.
Definition lcd_driver.c:12
void lcd_send(uint8_t nibble, bool is_data)
Definition lcd_driver.c:20
Interfaz del driver para LCD alfanumerico.