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
| #include "driver_lcd.h" #include "hal_delay.h"
void LCD_Init(void) { HAL_GPIO_Init(LCD_CS_PORT, LCD_CS_PIN, GPIO_MODE_OUTPUT); HAL_GPIO_Init(LCD_RST_PORT, LCD_RST_PIN, GPIO_MODE_OUTPUT); HAL_GPIO_Init(LCD_DC_PORT, LCD_DC_PIN, GPIO_MODE_OUTPUT); HAL_GPIO_Init(LCD_BLK_PORT, LCD_BLK_PIN, GPIO_MODE_OUTPUT);
HAL_SPI_Init(LCD_SPI_DEVICE, 10000000, SPI_CPOL_LOW, SPI_CPHA_1EDGE, SPI_DATA_SIZE_8BIT);
HAL_GPIO_WritePin(LCD_RST_PORT, LCD_RST_PIN, 0); HAL_DelayMs(10); HAL_GPIO_WritePin(LCD_RST_PORT, LCD_RST_PIN, 1); HAL_DelayMs(10);
LCD_WriteCommand(0x01); LCD_WriteData(0x00); HAL_DelayMs(5);
LCD_Clear(LCD_COLOR_BLACK); LCD_SetBacklight(100); }
void LCD_WriteCommand(uint8_t command) { HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, 0); HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, 0); HAL_SPI_SendByte(LCD_SPI_DEVICE, command); HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, 1); }
void LCD_WriteData(uint8_t data) { HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, 0); HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, 1); HAL_SPI_SendByte(LCD_SPI_DEVICE, data); HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, 1); }
void LCD_WriteData16(uint16_t data) { HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, 0); HAL_GPIO_WritePin(LCD_DC_PORT, LCD_DC_PIN, 1); HAL_SPI_SendByte(LCD_SPI_DEVICE, (data >> 8) & 0xFF); HAL_SPI_SendByte(LCD_SPI_DEVICE, data & 0xFF); HAL_GPIO_WritePin(LCD_CS_PORT, LCD_CS_PIN, 1); }
void LCD_SetRegion(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end) { LCD_WriteCommand(0x2A); LCD_WriteData16(x_start); LCD_WriteData16(x_end); LCD_WriteCommand(0x2B); LCD_WriteData16(y_start); LCD_WriteData16(y_end); LCD_WriteCommand(0x2C); }
void LCD_Clear(uint16_t color) { LCD_SetRegion(0, 0, LCD_WIDTH - 1, LCD_HEIGHT - 1); for (uint32_t i = 0; i < LCD_WIDTH * LCD_HEIGHT; i++) { LCD_WriteData16(color); } }
void LCD_DrawPixel(uint16_t x, uint16_t y, uint16_t color) { if (x >= LCD_WIDTH || y >= LCD_HEIGHT) return; LCD_SetRegion(x, y, x, y); LCD_WriteData16(color); }
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { }
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { LCD_DrawLine(x1, y1, x2, y1, color); LCD_DrawLine(x2, y1, x2, y2, color); LCD_DrawLine(x2, y2, x1, y2, color); LCD_DrawLine(x1, y2, x1, y1, color); }
void LCD_FillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { if (x1 > x2 || y1 > y2) return; for (uint16_t y = y1; y <= y2; y++) { LCD_DrawLine(x1, y, x2, y, color); } }
void LCD_DrawChar(uint16_t x, uint16_t y, char ch, uint16_t color, uint16_t bgcolor, uint8_t char_size) { }
void LCD_DrawString(uint16_t x, uint16_t y, const char *str, uint16_t color, uint16_t bgcolor, uint8_t char_size) { uint16_t x_pos = x; while (*str) { LCD_DrawChar(x_pos, y, *str++, color, bgcolor, char_size); x_pos += (char_size == 16) ? 8 : 6; if (x_pos > LCD_WIDTH - (char_size == 16 ? 8 : 6)) break; } }
void LCD_SetBacklight(uint8_t brightness) { if (brightness > 50) { HAL_GPIO_WritePin(LCD_BLK_PORT, LCD_BLK_PIN, 1); } else { HAL_GPIO_WritePin(LCD_BLK_PORT, LCD_BLK_PIN, 0); } }
|