A minimal ST7735 LCD driver for CH32V003 that supports both ch32v003fun and PlatformIO CH32V. It should also work in MounRiver Studio.
DMA is used to accelerate the data transmission where possible.
ST7735 8-pin module
CH32V003 | ST7735 | Power | Description |
---|---|---|---|
1 - LEDA |
Connect to 3V3 via a 15-100Ω resistor | ||
2 - GND |
GND |
GND | |
PC2 |
3 - RESET |
Reset | |
PC3 |
4 - RS |
DC (Data / Command) | |
PC6 (SPI MOSI) |
5 - SDA |
SPI MOSI (Master Output Slave In) | |
PC5 (SPI SCLK) |
6 - SCL |
SPI SCLK (Serial Clock) | |
7 - VDD |
3V3 |
VDD | |
PC4 |
8 - CS |
SPI CS/SS (Chip/Slave Select) |
Note: To not use CS, define the ST7735_NO_CS
macro in st7735.h
. Make ST7735 always enabled by pulling CS to ground.
#define ST7735_NO_CS
Include the header file.
#include "st7735.h"
Then invoke tft_init()
function to initialize the driver.
tft_init();
Print a string.
tft_set_color(RED);
tft_set_background_color(BLACK);
tft_set_cursor(2, 2);
tft_print("Hello World!");
Print integers.
tft_set_color(RED);
tft_set_background_color(BLACK);
tft_set_cursor(2, 10);
tft_print_number(123, 0); // Align left as the width is less thant the number.
tft_set_cursor(2, 20);
tft_print_number(-123, 30); // Align right as the width is greater than the number.
Draw a pixel.
tft_draw_pixel(80, 30, RED);
Draw a line.
tft_draw_line(10, 10, 30, 30, BLUE);
Draw a rectangle.
tft_draw_rect(10, 10, 30, 30, BLUE);
Fill a rectangle area.
tft_fill_rect(10, 10, 30, 30, BLUE);
Depends on which ST7735 variants you have, it may require different configurations. You can configure the behavior in st7735.h
or st7735.c
.
// st7735.h
#define ST7735_WIDTH 160
#define ST7735_HEIGHT 80
#define ST7735_X_OFFSET 1
#define ST7735_Y_OFFSET 26
// st7735.h
#define RGB565(r, g, b) ((((r)&0xF8) << 8) | (((g)&0xFC) << 3) | ((b) >> 3))
#define BGR565(r, g, b) ((((b)&0xF8) << 8) | (((g)&0xFC) << 3) | ((r) >> 3))
#define RGB RGB565
// st7735.c
void tft_init(void)
{
...
// Set rotation
write_command_8(ST7735_MADCTL);
write_data_8(ST7735_MADCTL_MY | ST7735_MADCTL_MV | ST7735_MADCTL_BGR); // 0 - Horizontal
// write_data_8(ST7735_MADCTL_BGR); // 1 - Vertical
// write_data_8(ST7735_MADCTL_MX | ST7735_MADCTL_MV | ST7735_MADCTL_BGR); // 2 - Horizontal
// write_data_8(ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_BGR); // 3 - Vertical
...
}
// st7735.c
void tft_init(void)
{
...
// Invert display
write_command_8(ST7735_INVON);
// write_command_8(ST7735_INVOFF);
...
}
- (Fixed)
The CS pin does not work understand PlatformIO. Pull the CS pin to ground as a temporary solution. - (Fixed)
The draw text function creates partial text on a diagonal line from top left, I thought it is caused by hardware, it turns out I set a mistake the Y coordinate as X coordinate.
- moononournation: Arduino_GFX
- Morita_Hikaru: CH32V003/Driver/CH32V003_LCD_Driver.zip
- E. Brombaugh: SPI OLED demonstration
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0).
- Attribution - You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
- NonCommercial - You may not use the material for commercial purposes.
- ShareAlike - If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.