In this post, we experiment with ST7735 1.8-inch 128×160 Color LCD on PIC Microcontroller.
This tiny 1.8-inch LCD module is the second color LCD that I successfully attempted (the first is the Nokia 3510i LCD). The breakout board which I purchased from eBay also comes with an SD card socket:
The pinout for the board is as follows, inclusive of the SD card connections:
- GND
- VCC
- NC
- NC
- NC
- LCD RESET
- LCD A0 (R/S)
- LCD SDA
- LCD SCK
- LCD CS
- SD SCK
- SD MISO
- SD MOSI
- SD CS
- LED+
- LED-
Interface via SP
This LCD controller, ST7735, uses SPI for communication and requires just 5 data lines, namely RESET, A0, SDA, SCK and CS. Of particular note is the A0 line, also known as R/S, which indicates whether the bytes being transferred should be interpreted as command or as pixel data. Although SPI communication should preferably be done using the hardware SPI module (there are two on my PIC24FJ64GA002) for faster display speed, it can also be done via bit-banging if hardware SPI is not available. The following function will send a byte via SPI using software:
void write_spi_byte(unsigned char c){
char x;
for(x=0;x<8;x++){
LCD_SCK = 0;
LCD_SDA = 0;
if(c & 0x80)
LCD_SDA = 1;
LCD_SCK = 1;
c <<= 1;
}
}
I converted the Adafruit’s Arduino library for this LCD to compile under Microchip C30 compiler for my PIC24FJ64GA002 and the LCD is able to draw some graphics nicely:
Using the bundled SD card socket and Microchip MDD library, together with my custom 5×7 font, I was able to display some SD card information on the LCD:
The board I purchased has an AMS1117 on-board regulator and expects at least 5V to be supplied to VCC to be able to generate 3.3V for the LCD and SD card to work. I did not know this and supplied 3.3V to VCC initially, only to find out that the SD card worked intermittently while the LCD still worked well. If you have problems with the SD card on this module, check if this is the case.
Setting the Color Model
The ST7735 controller supports up to 262,144 (218) colors. However, to be able to use 262K colors, for each pixel, 18-bit of data have to be transferred via SPI. Since this increases the complexity, I have decided to stay with 65,536 colors (16-bit) colors, where pixel data can be transferred nicely just by using 2 SPI writes.
In 16-bit color mode, the LCD expects pixel data to be in RGB565 format. The following will convert from the well-known RGB888 (24-bit color) format to RGB565:
#define RGB565(r,g,b) ((((r>>3)<<11) | ((g>>2)<<5) | (b>>3)))
An interesting point to note about this LCD is that there seem to be two variants with slightly different behaviors. If your module comes with a black tab, the BLUE and RED byte of each pixel will be swapped, resulting in the wrong color being displayed. If your module has a red or green tab, the byte order for each pixel will be correct.
To fix this issue, you can change the above RGB565 macro to swap the red and blue byte, or you can change the value of the MADCTL register during initialization:
writecommand(ST7735_MADCTL);
writedata(0xC0);
Downloads
Various bitmaps from my SD card as shown on the LCD:
The C30 source code for this LCD can be downloaded here.