Shop OBEX P1 Docs P2 Docs Learn Events
Looking for very Basic Code for SPI ST7735 — Parallax Forums

Looking for very Basic Code for SPI ST7735

Christof Eb.Christof Eb. Posts: 1,214
edited 2023-03-16 10:49 in General Discussion

Hi,
perhaps somebody has got a link or some very, very basic code to drive SPI ST7735?
For my new project I would like to use P2 with a ST7735 display. As I want to use Taqoz, I have to (re)write a driver for ST7735. There is a pixel based driver for VGA in Taqoz and I want to copy the framebuffer to ST7735. I am aware, that I must convert colorcode for every pixel.
While I have found some very elaborate drivers for ST7735, for example by adafruit, I have not yet found a very simple minimal driver. I would like to have something, that just shows setup and set a pixel.
Thanks in advance!
Christof

Edit: Now found something: technoblogy.com/show?2LMJ Will try to start based on this.

Comments

  • This is for P1, but since you just want example code to rewrite for Taqoz, it might help...

    https://forums.parallax.com/discussion/174147/parallax-font-on-st7735-tft

    dgately

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2023-03-16 17:59

    @Christof Eb.

    Formatting is a little skewed

    This example is typscript for a 128 x 160 display but really easy to port.

    Command constants

    TFTCommands {
            NOP = 0x00,
            SWRESET = 0x01,
            SLPOUT = 0x11,
            NORON = 0x13,
            INVOFF = 0x20,
            DISPOFF = 0x28,
            DISPON = 0x29,
            CASET = 0x2A,
            RASET = 0x2B,
            RAMWR = 0x2C,
            MADCTL = 0x36,
            COLMOD = 0x3A,
            FRMCTR1 = 0xB1,
            FRMCTR2 = 0xB2,
            FRMCTR3 = 0xB3,
            INVCTR = 0xB4,
            PWCTR1 = 0xC0,
            PWCTR2 = 0xC1,
            PWCTR3 = 0xC2,
            PWCTR4 = 0xC3,
            PWCTR5 = 0xC4,
            VMCTR1 = 0xC5,
            GMCTRP1 = 0xE0,
            GMCTRN1 = 0xE1,
            DELAY = 0xFFFF,
            TEST = 0xF0,
            PWRSAV = 0xF6,
            VSCRDEF = 0x33,
            VSCRSADD = 0x37
        }
    
    

    Init configures the display ready to accept graphics methods,

    ```
    // set SPI frequency
    pins.spiFrequency(16000000)

        //Toggle reset pin
        pins.digitalWritePin(DigitalPin.P8, 1)
        basic.pause(100)
        pins.digitalWritePin(DigitalPin.P8, 0)
        basic.pause(100)
        pins.digitalWritePin(DigitalPin.P8, 1)
    
        // Frame rate control 
        send(TFTCommands.FRMCTR1, [0x01, 0x2C, 0x2D])
        send(TFTCommands.FRMCTR2, [0x01, 0x2C, 0x2D])
        send(TFTCommands.FRMCTR3, [0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D])
    
        // Display inversion control
        send(TFTCommands.INVCTR, [0x07])
    
        // Display power control
        send(TFTCommands.PWCTR1, [0xA2, 0x02, 0x84])
        send(TFTCommands.PWCTR2, [0xC5])
        send(TFTCommands.PWCTR3, [0x0A, 0x00])
        send(TFTCommands.PWCTR4, [0x8A, 0x2A])
        send(TFTCommands.PWCTR5, [0x8A, 0xEE])
        send(TFTCommands.VMCTR1, [0x0E])
    
        // Set Gamma
        send(TFTCommands.GMCTRP1, [0x0F, 0x1A, 0x0F, 0x18, 0x2F, 0x28, 0x20, 0x22, 0x1F, 0x1B, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
        send(TFTCommands.GMCTRN1, [0x0F, 0x1B, 0x0F, 0x17, 0x33, 0x2C, 0x29, 0x2E, 0x30, 0x30, 0x39, 0x3F, 0x00, 0x07, 0x03, 0x10])
    
        send(TFTCommands.TEST, [0x01])
        send(TFTCommands.PWRSAV, [0x00])
        send(TFTCommands.COLMOD, [0x05])
        send(TFTCommands.MADCTL, [0xA0])
        send(TFTCommands.SLPOUT, [])
    
        basic.pause(100)
        // Turn display on
        send(TFTCommands.DISPON, [])
    

    export function drawPixel(x: number, y: number, color: Color): void {
    setWindow(x, y, x + 1, y + 1)
    send(TFTCommands.RAMWR, [color >> 8, color])
    }

    function setWindow(x0: number, y0: number, x1: number, y1: number): void {
    x0 = (x0 & 0xFF) + 1
    x1 = ((x1 - 1) & 0xFF) + 1
    y0 = (y0 & 0xFF) + 2
    y1 = ((y1 - 1) & 0xFF) + 2
    send(TFTCommands.CASET, [0x00, x0, 0x00, x1])
    send(TFTCommands.RASET, [0x00, y0, 0x00, y1])
    send(TFTCommands.RAMWR, [])
    }
    ```

  • The send function used in the initialization transmits the various commands followed by data over SPI.

    Here is a link to the datasheet that details the commands in detail

    https://www.displayfuture.com/Display/datasheet/controller/ST7735.pdf

  • Thank you both for your input!
    I have managed to get first results.
    Christof

  • Once the display is initialized setWindow is extremely important for the graphics functions and speed for shapes, images and fonts. When the window has been defined pixel data can be drawn to the display from a buffer or file in a sequential manner, here for example a horizontal line.

    function drawHorizontal(x_0: number, y_0: number, length: number, color: Color): void {
            setWindow(x_0, y_0, x_0 + length, y_0)
            pins.digitalWritePin(DigitalPin.P12, 1) //EN LCD data
            pins.digitalWritePin(DigitalPin.P16, 0)  //EN LCD CS
            for (let i = 0; i < length; i++) {
                pins.spiWrite(color >> 8 & 0xFF)
                pins.spiWrite(color & 0xFF)
            }
            pins.digitalWritePin(DigitalPin.P16, 1)  //remove LCD CS
        }
    

    The format is RGB 565 so there are 2 bytes for each pixel, if using images they usually have to be converted to 565.

    I can help with the basic drawing, font and image functions.

Sign In or Register to comment.