Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / R

Experimenting with ST7735 1.8-inch 128×160 Color LCD on a PIC Microcontroller

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
7 May 2023CPOL3 min read 2.9K  
More about ST7735 1.8-inch 128×160 Color LCD on a PIC Microcontroller
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:

  1. GND
  2. VCC
  3. NC
  4. NC
  5. NC
  6. LCD RESET
  7. LCD A0 (R/S)
  8. LCD SDA
  9. LCD SCK
  10. LCD CS
  11. SD SCK
  12. SD MISO
  13. SD MOSI
  14. SD CS
  15. LED+
  16. 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:

C++
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:

C++
#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:

C++
writecommand(ST7735_MADCTL);

// R and B byte are swapped
// writedata(0xC8);

// normal R G B order
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Writer
Singapore Singapore
Since 2008, ToughDev has been publishing technical sharing articles on a wide range of topics from software development to electronics design. Our interests include, but are not limited to, Android/iOS programming, VoIP products, embedded design using Arduino/PIC microcontrollers, reverse-engineering, retro-computing, and many others. We also perform product reviews, both on new and vintage products, and share our findings with the community. In addition, our team also develops customized software/hardware solutions highly adapted to suit your needs. Contact us for more information.

Comments and Discussions

 
-- There are no messages in this forum --