NHD-2.4-240320AF-CSXP SPI 3-line

Comments

2 comments

  • MCS

    I have tested the screen with parallel 8 and 16 bit interface and it works fine, but I want to try with SPI and it doesn't work.

    0
  • Engineering Support
    Community moderator

    Hello,

    Unfortunately, we do not currently have example code for the NHD-2.4-240320AF-CSXP display using 3-wire SPI and ESP32-S3.

     

    However, below is an example code that works with our  2.4-inch display using 3-wire SPI. I hope this helps you.


    //2.4 TFT IPS using MEGA
    #define SDA 28  //MOSI signal connected to Arduino pin 28
    #define SCL 29  //SCL signal connected to Arduino pin 29
    #define CS 23   // CS signal connected to Arduino pin 23
    #define RES 24  //RES signal connected to Arduino pin 24
    #define IM0 32  //IM0 signal connected to Arduino pin 32
    #define IM2 33  //IM1 signal connected to Arduino pin 33
    /////////////SD Card /////////////
    const int ChipSelect = 53;
    #include <SD.h>
    #include <SPI.h>
    File myFile;
    /////////////////////////////
    void TFT_SDbmp(unsigned char image)  // function to show bmp from SD card
    {
      unsigned char dummy;
      switch (image) {
        case 1:
          myFile = SD.open("image1.bmp");
          break;
        case 2:
          myFile = SD.open("image2.bmp");
          break;
        case 3:
          myFile = SD.open("image3.bmp");
          break;
        case 4:
          myFile = SD.open("image4.bmp");
          break;
        case 5:
          myFile = SD.open("image5.bmp");
          break;
        default:
          break;
      }
      window_set(0, 239, 0, 319);
      comm_out(0x2C);
      if (myFile) {
        for (unsigned int i = 0; i < 54; i++) {
          dummy = myFile.read();
        }
        //for(unsigned int i=0;i<20480;i++)
        while (myFile.available()) {
          data_out(myFile.read());
        }
        myFile.close();
      }
    }
    void comm_out(unsigned char c) {
      PORTA &= ~(1 << PORTA6);  //MOSI = 1 for D/C Bit
      PORTA |= (1 << PORTA7);   //SCL = 0
      PORTA &= ~(1 << PORTA7);  //SCL = 1
      PORTA |= (1 << PORTA7);   //SCL = 0
      for (int i = 0; i < 8; i++) {
        if ((c & 0x80) == 0x80)
          PORTA |= (1 << PORTA6);  //MOSI = 1
        else
          PORTA &= ~(1 << PORTA6);  //MOSI = 0
        c = (c << 1);
        PORTA |= (1 << PORTA7);   //SCL = 0
        PORTA &= ~(1 << PORTA7);  //SCL = 1
        PORTA |= (1 << PORTA7);   //SCL = 0
      }
    }
    void data_out(unsigned char d) {
      PORTA |= (1 << PORTA6);   //MOSI = 1 for D/C Bit
      PORTA |= (1 << PORTA7);   //SCL = 0
      PORTA &= ~(1 << PORTA7);  //SCL = 1
      PORTA |= (1 << PORTA7);   //SCL = 0
      for (int i = 0; i < 8; i++) {
        if ((d & 0x80) == 0x80)
          PORTA |= (1 << PORTA6);  //MOSI = 1
        else
          PORTA &= ~(1 << PORTA6);  //MOSI = 0
        d = (d << 1);
        PORTA |= (1 << PORTA7);   //SCL = 0
        PORTA &= ~(1 << PORTA7);  //SCL = 1
        PORTA |= (1 << PORTA7);   //SCL = 0
      }
    }
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    //  Window Set Function
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    void window_set(unsigned s_x, unsigned e_x, unsigned s_y, unsigned e_y) {
      comm_out(0x2a);  //SET column address
      digitalWrite(RES, HIGH);
      data_out((s_x) >> 8);  //SET start column address
      data_out(s_x);
      data_out((e_x) >> 8);  //SET end column address
      data_out(e_x);

      comm_out(0x2b);  //SET page address
      digitalWrite(RES, HIGH);
      data_out((s_y) >> 8);  //SET start page address
      data_out(s_y);
      data_out((e_y) >> 8);  //SET end page address
      data_out(e_y);
    }

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    //  Fill Screen (All Red -> All Green -> All Blue) Function
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    void disp() {
      {
        unsigned int i;
        comm_out(0x2C);              //command to begin writing to frame memory
        for (i = 0; i < 38400; i++)  //fill screen with blue pixels
        {
          data_out(0x00);
          data_out(0x1F);
          data_out(0x00);
          data_out(0x1F);
        }
        for (i = 0; i < 38400; i++)  //fill screen with green pixels
        {
          data_out(0x07);
          data_out(0xE0);
          data_out(0x07);
          data_out(0xE0);
        }

        for (i = 0; i < 38400; i++)  //fill screen with red pixels
        {
          data_out(0xF8);
          data_out(0x00);
          data_out(0xF8);
          data_out(0x00);
        }
        delay(300);
      }
    }

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    //  Fill Screen (Red, Green & Blue Lines) Function
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    void disp2() {
      {
        unsigned int i;
        comm_out(0x2C);              //command to begin writing to frame memory
        for (i = 0; i < 12800; i++)  //fill screen with blue pixels
        {
          data_out(0x00);  //R
          data_out(0x1F);  //R
          data_out(0x00);  //R
          data_out(0x1F);  //R
          data_out(0xF8);  //B
          data_out(0x00);  //B
          data_out(0xF8);  //B
          data_out(0x00);  //B
          data_out(0x07);  //G
          data_out(0xE0);  //G
          data_out(0x07);  //G
          data_out(0xE0);  //G
        }
      }
    }

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    //  Border and Fill Function
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    void Border_Fill() {
      unsigned int i, j;
      window_set(0, 239, 0, 0);
      comm_out(0x2C);
      digitalWrite(RES, HIGH);
      for (i = 0; i < 240; i++)  //Bottom White Border
      {
        for (j = 0; j < 1; j++) {
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
        }
      }
      delay(100);
      window_set(0, 0, 0, 319);
      comm_out(0x2C);
      digitalWrite(RES, HIGH);
      for (i = 0; i < 1; i++)  //Left White Border
      {
        for (j = 0; j < 320; j++) {
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
        }
      }
      delay(100);
      window_set(0, 239, 319, 319);
      comm_out(0x2C);
      digitalWrite(RES, HIGH);
      for (i = 0; i < 240; i++)  //Top White Border
      {
        for (j = 0; j < 1; j++) {
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
        }
      }
      delay(100);
      window_set(239, 239, 0, 319);
      comm_out(0x2C);
      digitalWrite(RES, HIGH);
      for (i = 0; i < 1; i++)  //Right White Border
      {
        for (j = 0; j < 240; j++) {
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
          data_out(0xFF);
        }
      }
      delay(100);
      window_set(1, 238, 1, 318);
      comm_out(0x2C);
      digitalWrite(RES, HIGH);
      for (i = 0; i < 238; i++)  //fill inside with Black Pixels
      {
        for (j = 0; j < 318; j++) {
          data_out(0x00);
          data_out(0x00);
          data_out(0x00);
          data_out(0x00);
        }
      }
      delay(250);
      window_set(0, 239, 0, 319);
      comm_out(0x2C);
      digitalWrite(RES, HIGH);
      for (i = 0; i < 38400; i++)  //fill screen with White Pixels
      {
        data_out(0xFF);
        data_out(0xFF);
        data_out(0xFF);
        data_out(0xFF);
      }
    }

    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    //  Fill Screen (All Black) Function
    //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    void disp3() {
      unsigned int i;
      window_set(0, 239, 0, 319);
      comm_out(0x2C);              //command to begin writing to frame memory
      for (i = 0; i < 38400; i++)  //fill screen with black pixels
      {
        data_out(0x00);
        data_out(0x00);
        data_out(0x00);
        data_out(0x00);
      }
    }

    /****************************************************
    *         Initialization and Setup Routine          *
    *****************************************************/
    void setup() {
      DDRA = 0xFF;  //Enable All outputs on PortA
      PORTA = 0x00;
      digitalWrite(IM0, HIGH);
      digitalWrite(IM2, HIGH);
      digitalWrite(CS, LOW);
      digitalWrite(RES, LOW);
      delay(250);
      digitalWrite(RES, HIGH);
      delay(250);
      comm_out(0x28);  //display off
      comm_out(0x11);  //exit SLEEP mode
      delay(100);

      comm_out(0x36);  //MADCTL: memory data access control
      data_out(0x88);
      comm_out(0x3A);  //COLMOD: Interface Pixel format  *** 65K-colors in 16bit/pixel (5-6-5) format when using 16-bit interface to allow 1-byte per pixel
      data_out(0x56);

      comm_out(0xB2);  //PORCTRK: Porch setting
      data_out(0x0C);
      data_out(0x0C);
      data_out(0x00);
      data_out(0x33);
      data_out(0x33);

      comm_out(0xB7);  //GCTRL: Gate Control
      data_out(0x35);
      comm_out(0xBB);  //VCOMS: VCOM setting
      data_out(0x2B);
      comm_out(0xC0);  //LCMCTRL: LCM Control
      data_out(0x2C);

      comm_out(0xC2);  //VDVVRHEN: VDV and VRH Command Enable
      data_out(0x01);
      data_out(0xFF);

      comm_out(0xC3);  //VRHS: VRH Set
      data_out(0x11);

      comm_out(0xC4);  //VDVS: VDV Set
      data_out(0x20);

      comm_out(0xC6);  //FRCTRL2: Frame Rate control in normal mode
      data_out(0x0F);
      comm_out(0xD0);  //PWCTRL1: Power Control 1
      data_out(0xA4);
      data_out(0xA1);
      comm_out(0xE0);  //PVGAMCTRL: Positive Voltage Gamma control
      data_out(0xD0);
      data_out(0x00);
      data_out(0x05);
      data_out(0x0E);
      data_out(0x15);
      data_out(0x0D);
      data_out(0x37);
      data_out(0x43);
      data_out(0x47);
      data_out(0x09);
      data_out(0x15);
      data_out(0x12);
      data_out(0x16);
      data_out(0x19);

      comm_out(0xE1);  //NVGAMCTRL: Negative Voltage Gamma control
      data_out(0xD0);
      data_out(0x00);
      data_out(0x05);
      data_out(0x0D);
      data_out(0x0C);
      data_out(0x06);
      data_out(0x2D);
      data_out(0x44);
      data_out(0x40);
      data_out(0x0E);
      data_out(0x1C);
      data_out(0x18);
      data_out(0x16);
      data_out(0x19);
      comm_out(0x2A);  //X address set
      data_out(0x00);
      data_out(0x00);
      data_out(0x00);
      data_out(0xEF);
      comm_out(0x2B);  //Y address set
      data_out(0x00);
      data_out(0x00);
      data_out(0x01);
      data_out(0x3F);
      delay(10);
      comm_out(0x21);  //Color inversion for ips
      comm_out(0x29);  //display ON
      delay(10);
      SD.begin(ChipSelect);
      //TFT_SDbmp_320240RGB2();
      TFT_SDbmp(1);
      delay(1000);
      TFT_SDbmp(2);
      delay(1000);
      TFT_SDbmp(3);
      delay(1000);
      TFT_SDbmp(4);
     delay(1000);
      TFT_SDbmp(5);
    }

    /*****************************************************
    *           Loop Function, to run repeatedly         *
    *****************************************************/

    void loop() {
    }
    0

Please sign in to leave a comment.