ST7735 TFT with PIC18F46K22 and Arduino | Electronics Developer

Поділитися
Вставка
  • Опубліковано 25 жов 2024

КОМЕНТАРІ • 2

  • @ElectronicsDeveloper
    @ElectronicsDeveloper  2 роки тому +5

    Source code for PIC18F46K22 :
    // define ST7735 TFT module pin connections
    #define TFT_RST PIN_D0 // reset pin, optional!
    #define TFT_CS PIN_D1 // chip select pin
    #define TFT_DC PIN_D2 // data/command pin
    #include
    #fuses NOMCLR,NOLVP,NOBROWNOUT,PUT,NOXINST
    #use delay(internal = 32MHz)
    #use SPI(SPI1, MODE = 0, BITS = 8, STREAM = ST7735)
    #use TIMER(TIMER = 1, TICK = 1024us, ISR)
    #include // include ST7735 driver source code
    #include // include graphics library source code
    void testlines(uint16_t color) {
    uint8_t x, y;
    display_fillScreen(ST7735_BLACK);
    for (x=0; x < display_width; x+=6) {
    display_drawLine(0, 0, x, display_height-1, color);
    delay_ms(0);
    }
    for (y=0; y < display_height; y+=6) {
    display_drawLine(0, 0, display_width-1, y, color);
    delay_ms(0);
    }
    display_fillScreen(ST7735_BLACK);
    for (x=0; x < display_width; x+=6) {
    display_drawLine(display_width-1, 0, x, display_height-1, color);
    delay_ms(0);
    }
    for (y=0; y < display_height; y+=6) {
    display_drawLine(display_width-1, 0, 0, y, color);
    delay_ms(0);
    }
    display_fillScreen(ST7735_BLACK);
    for (x=0; x < display_width; x+=6) {
    display_drawLine(0, display_height-1, x, 0, color);
    delay_ms(0);
    }
    for (y=0; y < display_height; y+=6) {
    display_drawLine(0, display_height-1, display_width-1, y, color);
    delay_ms(0);
    }
    display_fillScreen(ST7735_BLACK);
    for (x=0; x < display_width; x+=6) {
    display_drawLine(display_width-1, display_height-1, x, 0, color);
    delay_ms(0);
    }
    for (y=0; y < display_height; y+=6) {
    display_drawLine(display_width-1, display_height-1, 0, y, color);
    delay_ms(0);
    }
    }
    void testdrawtext(char *text, uint16_t color) {
    display_setCursor(0, 0);
    display_setTextColor(color);
    display_setTextWrap(true);
    printf(display_print, text);
    }
    void testfastlines(uint16_t color1, uint16_t color2) {
    uint8_t x, y;
    display_fillScreen(ST7735_BLACK);
    for (y=0; y < display_height; y+=5) {
    display_drawHLine(0, y, display_width, color1);
    }
    for (x=0; x < display_width; x+=5) {
    display_drawVLine(x, 0, display_height, color2);
    }
    }
    void testdrawrects(uint16_t color) {
    uint8_t x;
    display_fillScreen(ST7735_BLACK);
    for (x=0; x < display_width; x+=6) {
    display_drawRect(display_width/2 -x/2, display_height/2 -x/2 , x, x, color);
    }
    }
    void testfillrects(uint16_t color1, uint16_t color2) {
    uint8_t x;
    display_fillScreen(ST7735_BLACK);
    for (x=display_width-1; x > 6; x-=6) {
    display_fillRect(display_width/2 -x/2, display_height/2 -x/2 , x, x, color1);
    display_drawRect(display_width/2 -x/2, display_height/2 -x/2 , x, x, color2);
    }
    }
    void testfillcircles(uint8_t radius, uint16_t color) {
    uint8_t x, y;
    for (x=radius; x < display_width; x+=radius*2) {
    for (y=radius; y < display_height; y+=radius*2) {
    display_fillCircle(x, y, radius, color);
    }
    }
    }
    void testdrawcircles(uint8_t radius, uint16_t color) {
    uint8_t x, y;
    for (x=0; x < display_width+radius; x+=radius*2) {
    for (y=0; y < display_height+radius; y+=radius*2) {
    display_drawCircle(x, y, radius, color);
    }
    }
    }
    void testtriangles() {
    display_fillScreen(ST7735_BLACK);
    uint16_t color = 0xF800;
    uint8_t t,
    w = display_width / 2,
    x = display_height - 1,
    y = 0,
    z = display_width;
    for(t = 0 ; t

  • @ElectronicsDeveloper
    @ElectronicsDeveloper  2 роки тому +3

    Source code for Arduino :
    #include // Core graphics library
    #include // Hardware-specific library
    #include
    #define TFT_CS 9 // TFT CS pin is connected to arduino pin 9
    #define TFT_DC 10 // TFT DC pin is connected to arduino pin 10
    Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC);
    float p = 3.1415926;
    void setup(void) {
    tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
    uint16_t time = millis();
    tft.fillScreen(ST7735_BLACK);
    time = millis() - time;
    delay(500);
    // large block of text
    tft.fillScreen(ST7735_BLACK);
    testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", ST7735_WHITE);
    delay(1000);
    // tft print function!
    tftPrintTest();
    delay(4000);
    // a single pixel
    tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
    delay(500);
    // line draw test
    testlines(ST7735_YELLOW);
    delay(500);
    // optimized lines
    testfastlines(ST7735_RED, ST7735_BLUE);
    delay(500);
    testdrawrects(ST7735_GREEN);
    delay(500);
    testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
    delay(500);
    tft.fillScreen(ST7735_BLACK);
    testfillcircles(10, ST7735_BLUE);
    testdrawcircles(10, ST7735_WHITE);
    delay(500);
    testroundrects();
    delay(500);
    testtriangles();
    delay(500);
    mediabuttons();
    delay(500);
    delay(1000);
    }
    void loop() {
    tft.invertDisplay(true);
    delay(500);
    tft.invertDisplay(false);
    delay(500);
    }
    void testlines(uint16_t color) {
    tft.fillScreen(ST7735_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6) {
    tft.drawLine(0, 0, x, tft.height()-1, color);
    }
    for (int16_t y=0; y < tft.height(); y+=6) {
    tft.drawLine(0, 0, tft.width()-1, y, color);
    }
    tft.fillScreen(ST7735_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6) {
    tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
    }
    for (int16_t y=0; y < tft.height(); y+=6) {
    tft.drawLine(tft.width()-1, 0, 0, y, color);
    }
    tft.fillScreen(ST7735_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6) {
    tft.drawLine(0, tft.height()-1, x, 0, color);
    }
    for (int16_t y=0; y < tft.height(); y+=6) {
    tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
    }
    tft.fillScreen(ST7735_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6) {
    tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
    }
    for (int16_t y=0; y < tft.height(); y+=6) {
    tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
    }
    }
    void testdrawtext(char *text, uint16_t color) {
    tft.setCursor(0, 0);
    tft.setTextColor(color);
    tft.setTextWrap(true);
    tft.print(text);
    }
    void testfastlines(uint16_t color1, uint16_t color2) {
    tft.fillScreen(ST7735_BLACK);
    for (int16_t y=0; y < tft.height(); y+=5) {
    tft.drawFastHLine(0, y, tft.width(), color1);
    }
    for (int16_t x=0; x < tft.width(); x+=5) {
    tft.drawFastVLine(x, 0, tft.height(), color2);
    }
    }
    void testdrawrects(uint16_t color) {
    tft.fillScreen(ST7735_BLACK);
    for (int16_t x=0; x < tft.width(); x+=6) {
    tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
    }
    }
    void testfillrects(uint16_t color1, uint16_t color2) {
    tft.fillScreen(ST7735_BLACK);
    for (int16_t x=tft.width()-1; x > 6; x-=6) {
    tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
    tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
    }
    }
    void testfillcircles(uint8_t radius, uint16_t color) {
    for (int16_t x=radius; x < tft.width(); x+=radius*2) {
    for (int16_t y=radius; y < tft.height(); y+=radius*2) {
    tft.fillCircle(x, y, radius, color);
    }
    }
    }
    void testdrawcircles(uint8_t radius, uint16_t color) {
    for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
    for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
    tft.drawCircle(x, y, radius, color);
    }
    }
    }
    void testtriangles() {
    tft.fillScreen(ST7735_BLACK);
    int color = 0xF800;
    int t;
    int w = tft.width()/2;
    int x = tft.height()-1;
    int y = 0;
    int z = tft.width();
    for(t = 0 ; t