美文网首页
【雕爷学编程】Arduino动手做(153)---2.4寸TFT

【雕爷学编程】Arduino动手做(153)---2.4寸TFT

作者: 驴友花雕 | 来源:发表于2023-07-08 07:18 被阅读0次

    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。

    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
    实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

    01.jpg

    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

    实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

    项目之三:使用图形库随机绘制各种图形,三角函数以及英文字符串

    Arduino实验开源代码

    /*
    
    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
    
      实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板
    
     项目之三:使用图形库随机绘制各种图形,三角函数以及英文字符串
    
     模块直插,引脚用法如下:
    
     LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
    
     Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
    
     LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
    
     Arduino Uno 8 9 2 3 4 5 6 7
    
    */
    
    
    
    #include <LCDWIKI_GUI.h> //导入核心图形库
    
    #include <LCDWIKI_KBV.h> //导入特定硬件库
    
    //如果 IC 模型已知或模块不可读,则可以使用此构造函数
    
    LCDWIKI_KBV my_lcd(ILI9341, A3, A2, A1, A0, A4); //模型、CS、CD、WR、RD、重置
    
    //如果 IC 模型未知且模块可读,则可以使用此构造函数
    
    //LCDWIKI_KBV my_lcd(240,320,A3,A2,A1,A0,A4);//屏幕宽度、高度、cs、cd、wr、rd、重置
    
    void show_string(uint8_t *str, int16_t x, int16_t y, uint8_t csize, uint16_t fc, uint16_t bc, boolean mode) {
    
     my_lcd.Set_Text_Mode(mode);
    
     my_lcd.Set_Text_Size(csize);
    
     my_lcd.Set_Text_colour(fc);
    
     my_lcd.Set_Text_Back_colour(bc);
    
     my_lcd.Print_String(str, x, y);
    
    }
    
    //显示主图框
    
    unsigned long show_text(void) {
    
     unsigned long time_start = micros();
    
     my_lcd.Set_Draw_color(32, 0, 255);
    
     my_lcd.Fill_Rectangle(0, 0, my_lcd.Get_Display_Width() - 1, 14);
    
     show_string("---> Hello World <---", CENTER, 3, 1, 0x07E0, 0, 1);
    
     my_lcd.Set_Draw_color(128, 128, 128);
    
     my_lcd.Fill_Rectangle(0, my_lcd.Get_Display_Height() - 15, my_lcd.Get_Display_Width() - 1, my_lcd.Get_Display_Height() - 1);
    
     show_string("* Universal Color TFT Display Library *", CENTER, my_lcd.Get_Display_Height() - 11, 1, 0xFFFF, 0, 1);
    
     my_lcd.Set_Draw_color(255, 0, 0);
    
     my_lcd.Draw_Rectangle(0, 15, my_lcd.Get_Display_Width() - 1, my_lcd.Get_Display_Height() - 16);
    
     return micros() - time_start;
    
    }
    
    //显示三角函数
    
    unsigned long show_triangle_function(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     //绘制十字准线
    
     my_lcd.Set_Draw_color(0, 0, 255);
    
     my_lcd.Draw_Fast_VLine(my_lcd.Get_Display_Width() / 2 - 1, 16, my_lcd.Get_Display_Height() - 32);
    
     my_lcd.Draw_Fast_HLine(1, my_lcd.Get_Display_Height() / 2 - 1, my_lcd.Get_Display_Width() - 2);
    
     for (i = 1; i <= (my_lcd.Get_Display_Height() - 32) / 2 / 10; i++) {
    
      my_lcd.Draw_Fast_HLine(my_lcd.Get_Display_Width() / 2 - 1 - 2, my_lcd.Get_Display_Height() / 2 - 1 - i * 10, 5);
    
      my_lcd.Draw_Fast_HLine(my_lcd.Get_Display_Width() / 2 - 1 - 2, my_lcd.Get_Display_Height() / 2 - 1 + i * 10, 5);
    
     }
    
     for (i = 1; i <= (my_lcd.Get_Display_Width() - 2) / 2 / 10; i++) {
    
      my_lcd.Draw_Fast_VLine(my_lcd.Get_Display_Width() / 2 - 1 - i * 10, my_lcd.Get_Display_Height() / 2 - 1 - 2, 5);
    
      my_lcd.Draw_Fast_VLine(my_lcd.Get_Display_Width() / 2 - 1 + i * 10, my_lcd.Get_Display_Height() / 2 - 1 - 2, 5);
    
     }
    
     // 绘制罪恶线(Draw sin lines)
    
     show_string("sin", 5, 17, 1, 0x07FF, 0, 0);
    
     my_lcd.Set_Draw_color(0, 255, 255);
    
     for (i = 1; i < my_lcd.Get_Display_Width() - 2; i++) {
    
      my_lcd.Draw_Pixel(i, my_lcd.Get_Display_Height() / 2 - 1 + (sin(((i * 1.13) * 3.14) / 180) * 95));
    
     }
    
     // 画cos线(Draw cos lines)
    
     show_string("cos", 5, 25, 1, 0x07E0, 0, 0);
    
     my_lcd.Set_Draw_color(0, 255, 0);
    
     for (i = 1; i < my_lcd.Get_Display_Width() - 2; i++) {
    
      my_lcd.Draw_Pixel(i, my_lcd.Get_Display_Height() / 2 - 1 + (cos(((i * 1.13) * 3.14) / 180) * 95));
    
     }
    
     // 绘制棕褐色线(Draw tan lines)
    
     show_string("tan", 5, 33, 1, 0xFFE0, 0, 0);
    
     my_lcd.Set_Draw_color(255, 255, 0);
    
     for (i = 1; i < my_lcd.Get_Display_Width() - 2; i++) {
    
      my_lcd.Draw_Pixel(i, my_lcd.Get_Display_Height() / 2 - 1 + (tan(((i * 1.13) * 3.14) / 180) * 10));
    
     }
    
     // 绘制婴儿床线(Draw cot lines)
    
     show_string("cot", 5, 41, 1, 0xF800, 0, 0);
    
     my_lcd.Set_Draw_color(255, 0, 0);
    
     for (i = 1; i < my_lcd.Get_Display_Width() - 2; i++) {
    
      my_lcd.Draw_Pixel(i, my_lcd.Get_Display_Height() / 2 - 1 + 1 / (tan(((i * 1.13) * 3.14) / 180) * 0.1));
    
     }
    
     return micros() - time_start;
    
    }
    
    // 绘制移动的正弦波(Draw a moving sinewave)
    
    unsigned long show_sinewave(void) {
    
     uint16_t buf[my_lcd.Get_Display_Width() - 2], x = 1, i, y;
    
     unsigned long time_start = micros();
    
     int16_t wid = my_lcd.Get_Display_Width();
    
     int16_t t;
    
     float k;
    
     if (wid == 320) {
    
      t = 20;
    
      k = 1.1;
    
     }
    
     else {
    
      t = 15;
    
      k = 0.7;
    
     }
    
     my_lcd.Set_Draw_color(0, 0, 255);
    
     my_lcd.Draw_Fast_VLine(my_lcd.Get_Display_Width() / 2 - 1, 16, my_lcd.Get_Display_Height() - 32);
    
     my_lcd.Draw_Fast_HLine(1, my_lcd.Get_Display_Height() / 2 - 1, my_lcd.Get_Display_Width() - 2);
    
     for (i = 1; i < ((my_lcd.Get_Display_Width() - 2)*t); i++) {
    
      x++;
    
      if (x == my_lcd.Get_Display_Width() - 1) {
    
       x = 1;
    
      }
    
      if (i > my_lcd.Get_Display_Width() - 1) {
    
       if ((x == my_lcd.Get_Display_Width() / 2 - 1) || (buf[x - 1] == my_lcd.Get_Display_Height() / 2 - 1)) {
    
        my_lcd.Set_Draw_color(0, 0, 255);
    
       }
    
       else {
    
        my_lcd.Set_Draw_color(0, 0, 0);
    
       }
    
       my_lcd.Draw_Pixel(x, buf[x - 1]);
    
      }
    
      my_lcd.Set_Draw_color(255, 64, 255);
    
      y = my_lcd.Get_Display_Height() / 2 - 1 + (sin(((i * k) * 3.14) / 180) * (90 - (i / 100)));
    
      my_lcd.Draw_Pixel(x, y);
    
      buf[x - 1] = y;
    
     }
    
     return micros() - time_start;
    
    }
    
    // 绘制一些实心矩形(Draw some filled rectangles)
    
    unsigned long show_fill_rectangle(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     uint16_t side_len = (my_lcd.Get_Display_Height() - 40) / 5;
    
     uint16_t x_spec = (my_lcd.Get_Display_Width() - 5 * side_len) / 2;
    
     uint16_t y_spec = (my_lcd.Get_Display_Height() - 5 * side_len) / 2;
    
     for (i = 0; i < 5; i++)
    
     {
    
      switch (i)
    
      {
    
       case 0:
    
        my_lcd.Set_Draw_color(255, 0, 255);
    
        break;
    
       case 1:
    
        my_lcd.Set_Draw_color(255, 0, 0);
    
        break;
    
       case 2:
    
        my_lcd.Set_Draw_color(0, 255, 0);
    
        break;
    
       case 3:
    
        my_lcd.Set_Draw_color(0, 0, 255);
    
        break;
    
       case 4:
    
        my_lcd.Set_Draw_color(255, 255, 0);
    
        break;
    
       default:
    
        break;
    
      }
    
      my_lcd.Fill_Rectangle(x_spec + i * side_len - 1, y_spec + i * side_len - 1, x_spec + (i + 1)*side_len - 1, y_spec + (i + 1)*side_len - 1);
    
      my_lcd.Fill_Rectangle(x_spec + i * side_len - 1, y_spec + (5 - i)*side_len - 1, x_spec + (i + 1)*side_len - 1, y_spec + (4 - i)*side_len - 1);
    
     }
    
     return micros() - time_start;
    
    }
    
    // 绘制一些填充的圆角矩形(Draw some filled round rectangles)
    
    unsigned long show_fill_round_rectangle(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     uint16_t side_len = (my_lcd.Get_Display_Height() - 40) / 5;
    
     uint16_t x_spec = (my_lcd.Get_Display_Width() - 5 * side_len) / 2;
    
     uint16_t y_spec = (my_lcd.Get_Display_Height() - 5 * side_len) / 2;
    
     for (i = 0; i < 5; i++) {
    
      switch (i) {
    
       case 0:
    
        my_lcd.Set_Draw_color(255, 0, 255);
    
        break;
    
       case 1:
    
        my_lcd.Set_Draw_color(255, 0, 0);
    
        break;
    
       case 2:
    
        my_lcd.Set_Draw_color(0, 255, 0);
    
        break;
    
       case 3:
    
        my_lcd.Set_Draw_color(0, 0, 255);
    
        break;
    
       case 4:
    
        my_lcd.Set_Draw_color(255, 255, 0);
    
        break;
    
       default:
    
        break;
    
      }
    
      my_lcd.Fill_Round_Rectangle(x_spec + i * side_len - 1, y_spec + i * side_len - 1, x_spec + (i + 1)*side_len - 1, y_spec + (i + 1)*side_len - 1, 10);
    
      my_lcd.Fill_Round_Rectangle(x_spec + i * side_len - 1, y_spec + (5 - i)*side_len - 1, x_spec + (i + 1)*side_len - 1, y_spec + (4 - i)*side_len - 1, 10);
    
     }
    
     return micros() - time_start;
    
    }
    
    // 画一些实心圆圈(Draw some filled circles)
    
    unsigned long show_fill_circle(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     uint16_t r_len = (my_lcd.Get_Display_Height() - 40) / 5 / 2;
    
     uint16_t x_spec = (my_lcd.Get_Display_Width() - 5 * r_len * 2) / 2;
    
     uint16_t y_spec = (my_lcd.Get_Display_Height() - 5 * r_len * 2) / 2;
    
     for (i = 0; i < 5; i++) {
    
      switch (i) {
    
       case 0:
    
        my_lcd.Set_Draw_color(255, 0, 255);
    
        break;
    
       case 1:
    
        my_lcd.Set_Draw_color(255, 0, 0);
    
        break;
    
       case 2:
    
        my_lcd.Set_Draw_color(0, 255, 0);
    
        break;
    
       case 3:
    
        my_lcd.Set_Draw_color(0, 0, 255);
    
        break;
    
       case 4:
    
        my_lcd.Set_Draw_color(255, 255, 0);
    
        break;
    
       default:
    
        break;
    
      }
    
      my_lcd.Fill_Circle(x_spec + r_len + i * r_len * 2 - 1, y_spec + r_len + i * r_len * 2 - 1, r_len);
    
      my_lcd.Fill_Circle(x_spec + r_len + i * r_len * 2 - 1, y_spec + (5 - i)*r_len * 2 - r_len - 1, r_len);
    
     }
    
     return micros() - time_start;
    
    }
    
    // 画一些实心三角形(Draw some filled triangles)
    
    unsigned long show_fill_triangle(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     uint16_t h_len = (my_lcd.Get_Display_Height() - 40) / 5;
    
     uint16_t side_len = (h_len * 115) / 100;
    
     uint16_t x_spec = (my_lcd.Get_Display_Width() - 5 * side_len) / 2;
    
     uint16_t y_spec = (my_lcd.Get_Display_Height() - 5 * h_len) / 2;
    
     for (i = 0; i < 5; i++) {
    
      switch (i) {
    
       case 0:
    
        my_lcd.Set_Draw_color(255, 0, 255);
    
        break;
    
       case 1:
    
        my_lcd.Set_Draw_color(255, 0, 0);
    
        break;
    
       case 2:
    
        my_lcd.Set_Draw_color(0, 255, 0);
    
        break;
    
       case 3:
    
        my_lcd.Set_Draw_color(0, 0, 255);
    
        break;
    
       case 4:
    
        my_lcd.Set_Draw_color(255, 255, 0);
    
        break;
    
       default:
    
        break;
    
      }
    
      my_lcd.Fill_Triangle(x_spec + i * side_len - 1, y_spec + (i + 1)*h_len - 1, x_spec + side_len / 2 + i * side_len - 1, y_spec + i * h_len - 1, x_spec + (i + 1)*side_len - 1, y_spec + (i + 1)*h_len - 1);
    
      my_lcd.Fill_Triangle(x_spec + i * side_len - 1, y_spec + (5 - i)*h_len - 1, x_spec + side_len / 2 + i * side_len - 1, y_spec + (4 - i)*h_len - 1, x_spec + (i + 1)*side_len - 1, y_spec + (5 - i)*h_len - 1);
    
     }
    
     return micros() - time_start;
    
    }
    
    // 在图案中绘制一些线条(Draw some lines in a pattern)
    
    unsigned long show_grid_lines(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     int16_t wid = my_lcd.Get_Display_Width();
    
     float k;
    
     if (wid == 320) {
    
      k = 1.44;
    
     }
    
     else {
    
      k = 1.6;
    
     }
    
     my_lcd.Set_Draw_color(255, 0, 0);
    
     for (i = 16; i < my_lcd.Get_Display_Height() - 17; i += 5) {
    
      my_lcd.Draw_Line(1, i, (i * k) - 10, my_lcd.Get_Display_Height() - 17);
    
     }
    
     my_lcd.Set_Draw_color(255, 0, 0);
    
     for (i = my_lcd.Get_Display_Height() - 17; i > 16; i -= 5) {
    
      my_lcd.Draw_Line(my_lcd.Get_Display_Width() - 2, i, (i * k) - 11, 16);
    
     }
    
     my_lcd.Set_Draw_color(0, 255, 255);
    
     for (i = my_lcd.Get_Display_Height() - 16; i > 16; i -= 5) {
    
      my_lcd.Draw_Line(1, i, (my_lcd.Get_Display_Height() - 17)*k + 10 - (i * k), 16);
    
     }
    
     my_lcd.Set_Draw_color(0, 255, 255);
    
     for (int i = 15; i < my_lcd.Get_Display_Height() - 17; i += 5) {
    
      my_lcd.Draw_Line(my_lcd.Get_Display_Width() - 2, i, (my_lcd.Get_Display_Height() - 17)*k + 10 - (i * k), my_lcd.Get_Display_Height() - 17);
    
     }
    
     return micros() - time_start;
    
    }
    
    // 绘制一些随机像素(Draw some random pixels)
    
    unsigned long show_random_pixels(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     for (i = 0; i < 6000; i++) {
    
      my_lcd.Set_Draw_color(random(255), random(255), random(255));
    
      my_lcd.Draw_Pixel(2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34));
    
     }
    
     return micros() - time_start;
    
    }
    
    // 画一些随机线(Draw some random lines)
    
    unsigned long show_random_lines(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     for (i = 0; i < 300; i++) {
    
      my_lcd.Set_Draw_color(random(255), random(255), random(255));
    
      my_lcd.Draw_Line(2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34), 2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34));
    
     }
    
     return micros() - time_start;
    
    }
    
    // 绘制一些随机矩形(Draw some random rectangles)
    
    unsigned long show_random_rectangles(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     for (i = 0; i < 150; i++) {
    
      my_lcd.Set_Draw_color(random(255), random(255), random(255));
    
      my_lcd.Draw_Rectangle(2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34), 2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34));
    
     }
    
     return micros() - time_start;
    
    }
    
    // 画一些随机的圆圈(Draw some random circles)
    
    unsigned long show_random_circles(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     for (i = 0; i < 150; i++) {
    
      my_lcd.Set_Draw_color(random(255), random(255), random(255));
    
      my_lcd.Draw_Circle(41 + random(my_lcd.Get_Display_Width() - 82), 56 + random(my_lcd.Get_Display_Height() - 112), random(40));
    
     }
    
     return micros() - time_start;
    
    }
    
    // 画一些随机三角形(Draw some random triangles)
    
    unsigned long show_random_triangles(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     for (i = 0; i < 150; i++) {
    
      my_lcd.Set_Draw_color(random(255), random(255), random(255));
    
      my_lcd.Draw_Triangle(2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34), 2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34), 2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34));
    
     }
    
     return micros() - time_start;
    
    }
    
    // 画一些随机的圆角矩形(Draw some random round rectangles)
    
    unsigned long show_random_round_rectangles(void) {
    
     uint16_t i;
    
     unsigned long time_start = micros();
    
     for (i = 0; i < 150; i++) {
    
      my_lcd.Set_Draw_color(random(255), random(255), random(255));
    
      my_lcd.Draw_Round_Rectangle(2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34), 2 + random(my_lcd.Get_Display_Width() - 4), 17 + random(my_lcd.Get_Display_Height() - 34), 5);
    
     }
    
     return micros() - time_start;
    
    }
    
    // 绘制一些随机位图(Draw some random bit maps)
    
    unsigned long show_random_bit_map(void) {
    
     uint16_t buf[48], i;
    
     unsigned long time_start = micros();
    
     int16_t len = (my_lcd.Get_Display_Height() * 3 / 4) / 6;
    
     for (i = 0; i < 48; i++) {
    
      my_lcd.Set_Draw_color(random(255), random(255), random(255));
    
      buf[i] = my_lcd.Get_Draw_color();
    
     }
    
     for (i = 1; i <= 6; i++) {
    
      my_lcd.Draw_Bit_Map(my_lcd.Get_Display_Width() / 2 - 1 - ((len / 2) * 4 / 3)*i, my_lcd.Get_Display_Height() / 2 - 1 - (len / 2)*i, 8, 6, buf, i * (len / 6));
    
      delay(100);
    
     }
    
     return micros() - time_start;
    
    }
    
    //清屏运行统计(Clear the screen)
    
    void clear_screen(void) {
    
     delay(2000);
    
     my_lcd.Set_Draw_color(0, 0, 0);
    
     my_lcd.Fill_Rectangle(1, 16, my_lcd.Get_Display_Width() - 2, my_lcd.Get_Display_Height() - 17);
    
    }
    
    unsigned long (*show_function[])(void) {
    
     show_text,
    
     show_triangle_function,
    
     show_sinewave,
    
     show_fill_rectangle,
    
     show_fill_round_rectangle,
    
     show_fill_circle,
    
     show_fill_triangle,
    
     show_grid_lines,
    
     show_random_pixels,
    
     show_random_lines,
    
     show_random_rectangles,
    
     show_random_round_rectangles,
    
     show_random_circles,
    
     show_random_triangles,
    
     show_random_bit_map,
    
    };
    
    uint8_t *show_str[] =
    
    {
    
     "show text          :",
    
     "show triangle function    :",
    
     "show sinewave        :",
    
     "show fill rectangle     :",
    
     "show fill round rectangle  :",
    
     "show fill circle       :",
    
     "show fill triangle      :",
    
     "show grid lines       :",
    
     "show random pixels      :",
    
     "show random lines      :",
    
     "show random rectangles    :",
    
     "show random round rectangles :",
    
     "show random circles     :",
    
     "show random triangles    :",
    
     "show random bit_map     :"
    
    };
    
    //显示程序运行时间(display the running time of programs)
    
    unsigned long show_total_time(void) {
    
     uint16_t i;
    
     unsigned long buf[15];
    
     unsigned long time_start = micros();
    
     for (i = 0; i < 15; i++) {
    
      buf[i] = show_function[i]();
    
      clear_screen();
    
     }
    
     for (i = 0; i < 15; i++) {
    
      show_string(show_str[i], (my_lcd.Get_Display_Width() - 260) / 2 - 1, (my_lcd.Get_Display_Height() - 150) / 2 + i * 10 - 1, 1, 0xFD20, 0, 1);
    
      my_lcd.Set_Text_colour(0, 255, 0);
    
      my_lcd.Print_Number_Int(buf[i], (my_lcd.Get_Display_Width() - 260) / 2 - 1 + 200, (my_lcd.Get_Display_Height() - 150) / 2 + i * 10 - 1, 0, ' ', 10);
    
     }
    
     delay(2000);
    
     return micros() - time_start;
    
    }
    
    //显示结束和总运行时间(display ending and total running time)
    
    void show_end(unsigned long run_time) {
    
     my_lcd.Fill_Screen(0, 255, 255);
    
     my_lcd.Set_Draw_color(255, 0, 0);
    
     my_lcd.Fill_Round_Rectangle(my_lcd.Get_Display_Width() / 2 - 1 - 120 + 1, my_lcd.Get_Display_Height() / 2 - 1 - 60 + 1, my_lcd.Get_Display_Width() / 2 - 1 + 120 - 1, my_lcd.Get_Display_Height() / 2 - 1 + 60 - 1, 5);
    
     my_lcd.Set_Text_colour(0, 255, 255);
    
     my_lcd.Set_Text_Size(1);
    
     my_lcd.Set_Text_Mode(1);
    
     my_lcd.Print_String("Running over!", CENTER, my_lcd.Get_Display_Height() / 2 - 1 - 40);
    
     my_lcd.Print_String("That's ok!", CENTER, my_lcd.Get_Display_Height() / 2 - 1 - 30);
    
     my_lcd.Print_String("After a few seconds,", CENTER, my_lcd.Get_Display_Height() / 2 - 1 - 20);
    
     my_lcd.Print_String("it will restart.", CENTER, my_lcd.Get_Display_Height() / 2 - 1 - 10);
    
     my_lcd.Print_String("Please wait ...", CENTER, my_lcd.Get_Display_Height() / 2 - 1);
    
     my_lcd.Set_Text_colour(255, 255, 0);
    
     my_lcd.Print_String("Total runtime(us): ", my_lcd.Get_Display_Width() / 2 - 1 - 90, my_lcd.Get_Display_Height() / 2 - 1 + 40);
    
     my_lcd.Set_Text_colour(0, 255, 0);
    
     my_lcd.Print_Number_Int(run_time, my_lcd.Get_Display_Width() / 2 - 1 + 30, my_lcd.Get_Display_Height() / 2 - 1 + 40, 0, ' ', 10);
    
     delay(2000);
    
    }
    
    void setup() {
    
     Serial.begin(9600);
    
     my_lcd.Init_LCD();
    
     Serial.println(my_lcd.Read_ID(), HEX);
    
     my_lcd.Fill_Screen(0x0);
    
     my_lcd.Set_Rotation(1);
    
    }
    
    void loop() {
    
     unsigned long total_time;
    
     my_lcd.Fill_Screen(0x0);
    
     total_time = show_total_time();
    
     show_end(total_time);
    
    }
    

    项目之三:使用图形库随机绘制各种图形,三角函数以及英文字符串

    Arduino实验场景图

    WeChat_20210627082139.gif WeChat_20210627082139 (1).gif WeChat_20210627082139 (2).gif WeChat_20210627082139 (4).gif WeChat_20210627082139 (4).gif WeChat_20210627082139 (5).gif WeChat_20210627082139 (6).gif

    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
    实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板
    项目之三:使用图形库随机绘制各种图形,三角函数以及英文字符串(视频60秒)

    https://v.youku.com/v_show/id_XNTE3NDU2NzEwMA==.html?spm=a2hcb.playlsit.page.1

    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

    实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

    项目之四:KBV图形与字符串显示的综合测试

    实验开源代码:

    /*
    
    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
    
      实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板
    
     项目之四:KBV图形与字符串显示的综合测试
    
     模块直插,引脚用法如下:
    
     LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
    
     Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
    
     LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
    
     Arduino Uno 8 9 2 3 4 5 6 7
    
    */
    
    #define LCD_CS A3 // 片选进入模拟A3
    
    #define LCD_CD A2 // 命令/数据进入模拟A2
    
    #define LCD_WR A1 // LCD 写入到模拟A1
    
    #define LCD_RD A0 // LCD 读取进入模拟A0
    
    #define LCD_RESET A4 //也可以只连接到 Arduino 的复位引脚
    
    #include <SPI.h>
    
    #include "Adafruit_GFX.h"// 导入特定硬件的库
    
    #include <MCUFRIEND_kbv.h>
    
    MCUFRIEND_kbv tft;
    
    //#include <Adafruit_TFTLCD.h>
    
    //Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
    
    // 为一些常见的 16 位颜色值分配人类可读的名称
    
    #define    BLACK  0x0000
    
    #define    BLUE  0x001F
    
    #define    RED   0xF800
    
    #define    GREEN  0x07E0
    
    #define CYAN  0x07FF
    
    #define MAGENTA 0xF81F
    
    #define YELLOW 0xFFE0
    
    #define WHITE  0xFFFF
    
    #ifndef min
    
    #define min(a, b) (((a) < (b)) ? (a) : (b))
    
    #endif
    
    void setup(void);
    
    void loop(void);
    
    unsigned long testFillScreen();
    
    unsigned long testText();
    
    unsigned long testLines(uint16_t color);
    
    unsigned long testFastLines(uint16_t color1, uint16_t color2);
    
    unsigned long testRects(uint16_t color);
    
    unsigned long testFilledRects(uint16_t color1, uint16_t color2);
    
    unsigned long testFilledCircles(uint8_t radius, uint16_t color);
    
    unsigned long testCircles(uint8_t radius, uint16_t color);
    
    unsigned long testTriangles();
    
    unsigned long testFilledTriangles();
    
    unsigned long testRoundRects();
    
    unsigned long testFilledRoundRects();
    
    void progmemPrint(const char *str);
    
    void progmemPrintln(const char *str);
    
    void runtests(void);
    
    uint16_t g_identifier;
    
    extern const uint8_t hanzi[];
    
    void showhanzi(unsigned int x, unsigned int y, unsigned char index)
    
    {
    
     uint8_t i, j, c, first = 1;
    
     uint8_t *temp = (uint8_t*)hanzi;
    
     uint16_t color;
    
     tft.setAddrWindow(x, y, x + 31, y + 31); //设置区域
    
     temp += index * 128;
    
     for (j = 0; j < 128; j++)
    
     {
    
      c = pgm_read_byte(temp);
    
      for (i = 0; i < 8; i++)
    
      {
    
       if ((c & (1 << i)) != 0)
    
       {
    
        color = RED;
    
       }
    
       else
    
       {
    
        color = BLACK;
    
       }
    
       tft.pushColors(&color, 1, first);
    
       first = 0;
    
      }
    
      temp++;
    
     }
    
    }
    
    void setup(void) {
    
     Serial.begin(9600);
    
     uint32_t when = millis();
    
     //  while (!Serial) ;  // 挂起Leonardo,直到您连接串行
    
     if (!Serial) delay(5000);      //给Leonardo一些时间
    
     Serial.println("Serial took " + String((millis() - when)) + "ms to start");
    
     //  tft.reset();         //硬件复位
    
     uint16_t ID = tft.readID();
    
     Serial.print("ID = 0x");
    
     Serial.println(ID, HEX);
    
     Serial.print("Ready ok!");
    
     if (ID == 0xD3D3) ID = 0x9481; //只写扩展
    
     //  ID = 0x9329;               // 强制标识
    
     tft.begin(ID);
    
    }
    
    #if defined(MCUFRIEND_KBV_H_)
    
    uint16_t scrollbuf[320];  // 我最大的屏幕是 320x480
    
    #define READGRAM(x, y, buf, w, h) tft.readGRAM(x, y, buf, w, h)
    
    #else
    
    uint16_t scrollbuf[320];  // Adafruit 只能处理 240x320
    
    // Adafruit 可以一次读取一个像素块
    
    int16_t READGRAM(int16_t x, int16_t y, uint16_t *block, int16_t w, int16_t h)
    
    {
    
     uint16_t *p;
    
     for (int row = 0; row < h; row++) {
    
      p = block + row * w;
    
      for (int col = 0; col < w; col++) {
    
       *p++ = tft.readPixel(x + col, y + row);
    
      }
    
     }
    
    }
    
    #endif
    
    void windowScroll(int16_t x, int16_t y, int16_t wid, int16_t ht, int16_t dx, int16_t dy, uint16_t *buf)
    
    {
    
     if (dx) for (int16_t row = 0; row < ht; row++) {
    
       READGRAM(x, y + row, buf, wid, 1);
    
       tft.setAddrWindow(x, y + row, x + wid - 1, y + row);
    
       tft.pushColors(buf + dx, wid - dx, 1);
    
       tft.pushColors(buf + 0, dx, 0);
    
      }
    
     if (dy) for (int16_t col = 0; col < wid; col++) {
    
       READGRAM(x + col, y, buf, 1, ht);
    
       tft.setAddrWindow(x + col, y, x + col, y + ht - 1);
    
       tft.pushColors(buf + dy, ht - dy, 1);
    
       tft.pushColors(buf + 0, dy, 0);
    
      }
    
    }
    
    void printmsg(int row, const char *msg)
    
    {
    
     tft.setTextColor(YELLOW, BLACK);
    
     tft.setCursor(0, row);
    
     tft.println(msg);
    
    }
    
    void loop(void) {
    
     uint8_t aspect;
    
     uint16_t pixel;
    
     const char *aspectname[] = {
    
      "PORTRAIT", "LANDSCAPE", "PORTRAIT_REV", "LANDSCAPE_REV"
    
     };
    
     const char *colorname[] = { "BLUE", "GREEN", "RED", "GRAY" };
    
     uint16_t colormask[] = { 0x001F, 0x07E0, 0xF800, 0xFFFF };
    
     uint16_t dx, rgb, n, wid, ht, msglin;
    
     tft.setRotation(0);
    
     runtests();
    
     delay(2000);
    
     if (tft.height() > 64) {
    
      for (uint8_t cnt = 0; cnt < 4; cnt++) {
    
       aspect = (cnt + 0) & 3;
    
       tft.setRotation(aspect);
    
       wid = tft.width();
    
       ht = tft.height();
    
       msglin = (ht > 160) ? 200 : 112;
    
       testText();
    
       dx = wid / 32;
    
       for (n = 0; n < 32; n++) {
    
        rgb = n * 8;
    
        rgb = tft.color565(rgb, rgb, rgb);
    
        tft.fillRect(n * dx, 48, dx, 63, rgb & colormask[aspect]);
    
       }
    
       tft.drawRect(0, 48 + 63, wid, 1, WHITE);
    
       tft.setTextSize(2);
    
       tft.setTextColor(colormask[aspect], BLACK);
    
       tft.setCursor(0, 72);
    
       tft.print(colorname[aspect]);
    
       tft.setTextColor(WHITE);
    
       tft.println(" COLOR GRADES");
    
       tft.setTextColor(WHITE, BLACK);
    
       printmsg(184, aspectname[aspect]);
    
       delay(1000);
    
       tft.drawPixel(0, 0, YELLOW);
    
       pixel = tft.readPixel(0, 0);
    
       tft.setTextSize((ht > 160) ? 2 : 1); //对于消息
    
    #if defined(MCUFRIEND_KBV_H_)
    
    #if 1
    
       extern const uint8_t penguin[];
    
       tft.setAddrWindow(wid - 40 - 40, 20 + 0, wid - 1 - 40, 20 + 39);
    
       tft.pushColors(penguin, 1600, 1);
    
    #elif 1
    
       extern const uint8_t wifi_full[];
    
       tft.setAddrWindow(wid - 40 - 40, 20 + 0, wid - 40 - 40 + 31, 20 + 31);
    
       tft.pushColors(wifi_full, 1024, 1, true);
    
    #elif 1
    
       extern const uint8_t icon_40x40[];
    
       tft.setAddrWindow(wid - 40 - 40, 20 + 0, wid - 1 - 40, 20 + 39);
    
       tft.pushColors(icon_40x40, 1600, 1);
    
    #endif
    
       tft.setAddrWindow(0, 0, wid - 1, ht - 1);
    
       if (aspect & 1) tft.drawRect(wid - 1, 0, 1, ht, WHITE);
    
       else tft.drawRect(0, ht - 1, wid, 1, WHITE);
    
       printmsg(msglin, "VERTICAL SCROLL UP");
    
       uint16_t maxscroll;
    
       if (tft.getRotation() & 1) maxscroll = wid;
    
       else maxscroll = ht;
    
       for (uint16_t i = 1; i <= maxscroll; i++) {
    
        tft.vertScroll(0, maxscroll, i);
    
        delay(10);
    
       }
    
       delay(1000);
    
       printmsg(msglin, "VERTICAL SCROLL DN");
    
       for (uint16_t i = 1; i <= maxscroll; i++) {
    
        tft.vertScroll(0, maxscroll, 0 - (int16_t)i);
    
        delay(10);
    
       }
    
       tft.vertScroll(0, maxscroll, 0);
    
       printmsg(msglin, "SCROLL DISABLED  ");
    
       delay(1000);
    
       if ((aspect & 1) == 0) { //Portrait
    
        tft.setTextColor(BLUE, BLACK);
    
        printmsg(msglin, "ONLY THE COLOR BAND");
    
        for (uint16_t i = 1; i <= 64; i++) {
    
         tft.vertScroll(48, 64, i);
    
         delay(20);
    
        }
    
        delay(1000);
    
       }
    
    #endif
    
       tft.setTextColor(YELLOW, BLACK);
    
       if (pixel == YELLOW) {
    
        printmsg(msglin, "SOFTWARE SCROLL  ");
    
    #if 0
    
        // 块的对角线卷轴
    
        for (int16_t i = 45, dx = 2, dy = 1; i > 0; i -= dx) {
    
         windowScroll(24, 8, 90, 40, dx, dy, scrollbuf);
    
        }
    
    #else
    
        // 块的普通水平滚动
    
        n = (wid > 320) ? 320 : wid;
    
        for (int16_t i = n, dx = 4, dy = 0; i > 0; i -= dx) {
    
         windowScroll(0, 200, n, 16, dx, dy, scrollbuf);
    
        }
    
    #endif
    
       }
    
       else if (pixel == CYAN)
    
        tft.println("readPixel() reads as BGR");
    
       else if ((pixel & 0xF8F8) == 0xF8F8)
    
        tft.println("readPixel() should be 24-bit");
    
       else {
    
        tft.print("readPixel() reads 0x");
    
        tft.println(pixel, HEX);
    
       }
    
       delay(2000);
    
      }
    
     }
    
     printmsg(msglin, "INVERT DISPLAY ");
    
     tft.invertDisplay(true);
    
     delay(1000);
    
     tft.invertDisplay(false);
    
    }
    
    typedef struct {
    
     PGM_P msg;
    
     uint32_t ms;
    
    } TEST;
    
    TEST result[12];
    
    #define RUNTEST(n, str, test) { result[n].msg = PSTR(str); result[n].ms = test; delay(500); }
    
    void runtests(void)
    
    {
    
     uint8_t i, len = 24, cnt;
    
     uint32_t total;
    
     RUNTEST(0, "FillScreen        ", testFillScreen());
    
     RUNTEST(1, "Text           ", testText());
    
     RUNTEST(2, "Lines          ", testLines(CYAN));
    
     RUNTEST(3, "Horiz/Vert Lines     ", testFastLines(RED, BLUE));
    
     RUNTEST(4, "Rectangles (outline)   ", testRects(GREEN));
    
     RUNTEST(5, "Rectangles (filled)   ", testFilledRects(YELLOW, MAGENTA));
    
     RUNTEST(6, "Circles (filled)     ", testFilledCircles(10, MAGENTA));
    
     RUNTEST(7, "Circles (outline)    ", testCircles(10, WHITE));
    
     RUNTEST(8, "Triangles (outline)   ", testTriangles());
    
     RUNTEST(9, "Triangles (filled)    ", testFilledTriangles());
    
     RUNTEST(10, "Rounded rects (outline) ", testRoundRects());
    
     RUNTEST(11, "Rounded rects (filled)  ", testFilledRoundRects());
    
     tft.fillScreen(BLACK);
    
     tft.setTextColor(GREEN);
    
     tft.setCursor(0, 0);
    
     uint16_t wid = tft.width();
    
     if (wid > 176) {
    
      tft.setTextSize(2);
    
    #if defined(MCUFRIEND_KBV_H_)
    
      tft.print("MCUFRIEND ");
    
    #if MCUFRIEND_KBV_H_ != 0
    
      tft.print(0.01 * MCUFRIEND_KBV_H_, 2);
    
    #else
    
      tft.print("for");
    
    #endif
    
      tft.println(" UNO");
    
    #else
    
      tft.println("Adafruit-Style Tests");
    
    #endif
    
     } else len = wid / 6 - 8;
    
     tft.setTextSize(1);
    
     total = 0;
    
     for (i = 0; i < 12; i++) {
    
      PGM_P str = result[i].msg;
    
      char c;
    
      if (len > 24) {
    
       if (i < 10) tft.print(" ");
    
       tft.print(i);
    
       tft.print(": ");
    
      }
    
      uint8_t cnt = len;
    
      while ((c = pgm_read_byte(str++)) && cnt--) tft.print(c);
    
      tft.print(" ");
    
      tft.println(result[i].ms);
    
      total += result[i].ms;
    
     }
    
     tft.setTextSize(2);
    
     tft.print("Total:");
    
     tft.print(0.000001 * total);
    
     tft.println("sec");
    
     g_identifier = tft.readID();
    
     tft.print("ID: 0x");
    
     tft.println(tft.readID(), HEX);
    
     //  tft.print("Reg(00):0x");
    
     //  tft.println(tft.readReg(0x00), HEX);
    
     tft.print("F_CPU:");
    
     tft.print(0.000001 * F_CPU);
    
    #if defined(__OPTIMIZE_SIZE__)
    
     tft.println("MHz -Os");
    
    #else
    
     tft.println("MHz");
    
    #endif
    
     delay(1000);
    
    }
    
    //标准 Adafruit 测试。 将调整到屏幕大小
    
    unsigned long testFillScreen() {
    
     unsigned long start = micros();
    
     tft.fillScreen(BLACK);
    
     tft.fillScreen(RED);
    
     tft.fillScreen(GREEN);
    
     tft.fillScreen(BLUE);
    
     tft.fillScreen(BLACK);
    
     return micros() - start;
    
    }
    
    unsigned long testText() {
    
     unsigned long start;
    
     tft.fillScreen(BLACK);
    
     start = micros();
    
     tft.setCursor(0, 0);
    
     tft.setTextColor(WHITE); tft.setTextSize(1);
    
     tft.println("Hello World!");
    
     tft.setTextColor(YELLOW); tft.setTextSize(2);
    
     tft.println(123.45);
    
     tft.setTextColor(RED);  tft.setTextSize(3);
    
     tft.println(0xDEADBEEF, HEX);
    
     tft.println();
    
     tft.setTextColor(GREEN);
    
     tft.setTextSize(5);
    
     tft.println("Groop");
    
     tft.setTextSize(2);
    
     tft.println("I implore thee,");
    
     tft.setTextSize(1);
    
     tft.println("my foonting turlingdromes.");
    
     tft.println("And hooptiously drangle me");
    
     tft.println("with crinkly bindlewurdles,");
    
     tft.println("Or I will rend thee");
    
     tft.println("in the gobberwarts");
    
     tft.println("with my blurglecruncheon,");
    
     tft.println("see if I don't!");
    
     return micros() - start;
    
    }
    
    unsigned long testLines(uint16_t color) {
    
     unsigned long start, t;
    
     int      x1, y1, x2, y2,
    
            w = tft.width(),
    
            h = tft.height();
    
     tft.fillScreen(BLACK);
    
     x1 = y1 = 0;
    
     y2  = h - 1;
    
     start = micros();
    
     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     x2  = w - 1;
    
     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     t   = micros() - start; // fillScreen doesn't count against timing
    
     tft.fillScreen(BLACK);
    
     x1  = w - 1;
    
     y1  = 0;
    
     y2  = h - 1;
    
     start = micros();
    
     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     x2  = 0;
    
     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     t  += micros() - start;
    
     tft.fillScreen(BLACK);
    
     x1  = 0;
    
     y1  = h - 1;
    
     y2  = 0;
    
     start = micros();
    
     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     x2  = w - 1;
    
     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     t  += micros() - start;
    
     tft.fillScreen(BLACK);
    
     x1  = w - 1;
    
     y1  = h - 1;
    
     y2  = 0;
    
     start = micros();
    
     for (x2 = 0; x2 < w; x2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     x2  = 0;
    
     for (y2 = 0; y2 < h; y2 += 6) tft.drawLine(x1, y1, x2, y2, color);
    
     return micros() - start;
    
    }
    
    unsigned long testFastLines(uint16_t color1, uint16_t color2) {
    
     unsigned long start;
    
     int      x, y, w = tft.width(), h = tft.height();
    
     tft.fillScreen(BLACK);
    
     start = micros();
    
     for (y = 0; y < h; y += 5) tft.drawFastHLine(0, y, w, color1);
    
     for (x = 0; x < w; x += 5) tft.drawFastVLine(x, 0, h, color2);
    
     return micros() - start;
    
    }
    
    unsigned long testRects(uint16_t color) {
    
     unsigned long start;
    
     int      n, i, i2,
    
            cx = tft.width() / 2,
    
            cy = tft.height() / 2;
    
     tft.fillScreen(BLACK);
    
     n   = min(tft.width(), tft.height());
    
     start = micros();
    
     for (i = 2; i < n; i += 6) {
    
      i2 = i / 2;
    
      tft.drawRect(cx - i2, cy - i2, i, i, color);
    
     }
    
     return micros() - start;
    
    }
    
    unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
    
     unsigned long start, t = 0;
    
     int      n, i, i2,
    
            cx = tft.width() / 2 - 1,
    
            cy = tft.height() / 2 - 1;
    
     tft.fillScreen(BLACK);
    
     n = min(tft.width(), tft.height());
    
     for (i = n; i > 0; i -= 6) {
    
      i2  = i / 2;
    
      start = micros();
    
      tft.fillRect(cx - i2, cy - i2, i, i, color1);
    
      t  += micros() - start;
    
      // Outlines are not included in timing results
    
      tft.drawRect(cx - i2, cy - i2, i, i, color2);
    
     }
    
     return t;
    
    }
    
    unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
    
     unsigned long start;
    
     int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
    
     tft.fillScreen(BLACK);
    
     start = micros();
    
     for (x = radius; x < w; x += r2) {
    
      for (y = radius; y < h; y += r2) {
    
       tft.fillCircle(x, y, radius, color);
    
      }
    
     }
    
     return micros() - start;
    
    }
    
    unsigned long testCircles(uint8_t radius, uint16_t color) {
    
     unsigned long start;
    
     int      x, y, r2 = radius * 2,
    
               w = tft.width() + radius,
    
               h = tft.height() + radius;
    
     // 这个屏幕没有被清除——这是
    
     // 有意且不影响报告时间。
    
     start = micros();
    
     for (x = 0; x < w; x += r2) {
    
      for (y = 0; y < h; y += r2) {
    
       tft.drawCircle(x, y, radius, color);
    
      }
    
     }
    
     return micros() - start;
    
    }
    
    unsigned long testTriangles() {
    
     unsigned long start;
    
     int      n, i, cx = tft.width() / 2 - 1,
    
               cy = tft.height() / 2 - 1;
    
     tft.fillScreen(BLACK);
    
     n   = min(cx, cy);
    
     start = micros();
    
     for (i = 0; i < n; i += 5) {
    
      tft.drawTriangle(
    
       cx  , cy - i, // 峰值
    
       cx - i, cy + i, // 左下方
    
       cx + i, cy + i, // 右下角
    
       tft.color565(0, 0, i));
    
     }
    
     return micros() - start;
    
    }
    
    unsigned long testFilledTriangles() {
    
     unsigned long start, t = 0;
    
     int      i, cx = tft.width() / 2 - 1,
    
              cy = tft.height() / 2 - 1;
    
     tft.fillScreen(BLACK);
    
     start = micros();
    
     for (i = min(cx, cy); i > 10; i -= 5) {
    
      start = micros();
    
      tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
    
               tft.color565(0, i, i));
    
      t += micros() - start;
    
      tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
    
               tft.color565(i, i, 0));
    
     }
    
     return t;
    
    }
    
    unsigned long testRoundRects() {
    
     unsigned long start;
    
     int      w, i, i2, red, step,
    
            cx = tft.width() / 2 - 1,
    
            cy = tft.height() / 2 - 1;
    
     tft.fillScreen(BLACK);
    
     w   = min(tft.width(), tft.height());
    
     start = micros();
    
     red = 0;
    
     step = (256 * 6) / w;
    
     for (i = 0; i < w; i += 6) {
    
      i2 = i / 2;
    
      red += step;
    
      tft.drawRoundRect(cx - i2, cy - i2, i, i, i / 8, tft.color565(red, 0, 0));
    
     }
    
     return micros() - start;
    
    }
    
    unsigned long testFilledRoundRects() {
    
     unsigned long start;
    
     int      i, i2, green, step,
    
            cx = tft.width() / 2 - 1,
    
            cy = tft.height() / 2 - 1;
    
     tft.fillScreen(BLACK);
    
     start = micros();
    
     green = 256;
    
     step = (256 * 6) / min(tft.width(), tft.height());
    
     for (i = min(tft.width(), tft.height()); i > 20; i -= 6) {
    
      i2 = i / 2;
    
      green -= step;
    
      tft.fillRoundRect(cx - i2, cy - i2, i, i, i / 8, tft.color565(0, green, 0));
    
     }
    
     return micros() - start;
    
    }
    

    实验串口返回情况

    17.jpg

    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

    实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

    项目之四:Kbv 图形与字符串显示的综合测试(实验记录视频1分26秒)

    实验动态图

    WeChat_20210628085659 (1).gif WeChat_20210628085659 (2).gif WeChat_20210628085659 (3).gif

    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

    实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

    项目之四:Kbv 图形与字符串显示的综合测试(实验记录视频1分26秒)

    https://v.youku.com/v_show/id_XNTE3NTAxMDcwNA==.html

    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

    实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板

    项目之五:生成实时时间的电视台测试卡

    实验开源代码:

    /*
    
    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
    
     实验一百五十三:2.4寸TFT液晶触摸屏 彩屏模块 可直插UNO R3 Mega2560开发板
    
     项目之五:生成实时时间的电视台测试卡
    
     模块直插,引脚用法如下:
    
     LCD_CS LCD_CD LCD_WR LCD_RD LCD_RST SD_SS SD_DI SD_DO SD_SCK
    
     Arduino Uno A3 A2 A1 A0 A4 10 11 12 13
    
     LCD_D0 LCD_D1 LCD_D2 LCD_D3 LCD_D4 LCD_D5 LCD_D6 LCD_D7
    
     Arduino Uno 8 9 2 3 4 5 6 7
    
    */
    
    #include <Adafruit_GFX.h>
    
    #if defined(_GFXFONT_H_)      
    
    #include <Fonts/FreeSans9pt7b.h>
    
    #define ADJ_BASELINE 11      //新字体将光标设置为字母底部
    
    #else
    
    #define ADJ_BASELINE 0       //遗留设置光标到字母顶部
    
    #endif
    
    #include <MCUFRIEND_kbv.h>
    
    MCUFRIEND_kbv tft;
    
    #define BLACK  0x0000
    
    #define BLUE  0x001F
    
    #define RED   0xF800
    
    #define GREEN  0x07E0
    
    #define CYAN  0x07FF
    
    #define MAGENTA 0xF81F
    
    #define YELLOW 0xFFE0
    
    #define WHITE  0xFFFF
    
    #define RGB(r, g, b) (((r&0xF8)<<8)|((g&0xFC)<<3)|(b>>3))
    
    #define GREY   RGB(127, 127, 127)
    
    #define DARKGREY RGB(64, 64, 64)
    
    #define TURQUOISE RGB(0, 128, 128)
    
    #define PINK   RGB(255, 128, 192)
    
    #define OLIVE   RGB(128, 128, 0)
    
    #define PURPLE  RGB(128, 0, 128)
    
    #define AZURE   RGB(0, 128, 255)
    
    #define ORANGE  RGB(255,128,64)
    
     
    
    #include <stdio.h>
    
    uint16_t ID;
    
    uint8_t hh, mm, ss; //当前时间的储存容器变量
    
    uint8_t conv2d(const char* p){
    
      uint8_t v = 0;
    
      if ('0' <= *p && *p <= '9') v = *p - '0';
    
      return 10 * v + *++p - '0';
    
    }
    
    void setup(void){
    
      Serial.begin(9600);
    
      tft.reset();
    
      ID = tft.readID();
    
      Serial.print("TFT ID = 0x");
    
      Serial.println(ID, HEX);
    
      //  if (ID == 0xD3D3) ID = 0x9481; // 只写扩展
    
      if (ID == 0xD3D3) ID = 0x9486; // 只写扩展
    
      tft.begin(ID);
    
      tft.setRotation(1);
    
      tft.fillScreen(BLACK);
    
    #if defined(_GFXFONT_H_)
    
      tft.setFont(&FreeSans9pt7b);
    
    #endif
    
      hh = conv2d(__TIME__);
    
      mm = conv2d(__TIME__ + 3);
    
      ss = conv2d(__TIME__ + 6);
    
    }
    
    void loop(void){
    
      int16_t x, y, dx, dy, radius = 108, idx;
    
      uint16_t w, h, len, mask;
    
      uint16_t colors[8] = { BLACK, WHITE, YELLOW, CYAN, GREEN, MAGENTA, RED, BLUE };
    
      uint16_t height, width;
    
      width = tft.width();
    
      height = tft.height();
    
      tft.fillRect(0, 0, 7, 3, WHITE);
    
      tft.fillRect(313, 0, 7, 3, WHITE);
    
      tft.fillRect(0, 237, 7, 3, WHITE);
    
      tft.fillRect(313, 237, 7, 3, WHITE);
    
      for (y = 0, w = 18, h = 3; y < 240; y += 13 * w + h) {
    
        for (x = 25; x < 320 - 18; x += 2 * w) {
    
          tft.fillRect(x, y, w, h, WHITE);
    
        }
    
      }
    
      for (x = 0, w = 7, h = 18; x < 320; x += 17 * h + w) {
    
        for (y = 21; y < 240 - 18; y += 2 * h) {
    
          tft.fillRect(x, y, w, h, WHITE);
    
        }
    
      }
    
      tft.fillRect(7, 3, 17 * 18, 13 * 18, GREY);
    
      for (x = 7, y = 0, w = 1, h = 240; x < 320; x += 18) {
    
        tft.fillRect(x, y, w, h, WHITE);
    
      }
    
      for (x = 0, y = 3, w = 320, h = 1; y < 240; y += 18) {
    
        tft.fillRect(x, y, w, h, WHITE);
    
      }
    
      tft.fillRect(26, 22, 17, 99, TURQUOISE);
    
      tft.fillRect(26, 120, 17, 99, PINK);
    
      tft.fillRect(44, 22, 17, 35, AZURE);
    
      tft.fillRect(44, 184, 17, 35, ORANGE);
    
      tft.fillRect(260, 22, 17, 35, AZURE);
    
      tft.fillRect(260, 184, 17, 35, ORANGE);
    
      tft.fillRect(278, 22, 17, 99, OLIVE);
    
      tft.fillRect(278, 120, 17, 99, PURPLE);
    
      for (dx = radius; dx > -radius; dx--) {
    
        w = sqrt(radius * radius - dx * dx);
    
        y = 120 - dx;
    
        dy = (y - 3) / 18;
    
        mask = 7;
    
        colors[0] = (dy == 3) ? DARKGREY : BLACK;
    
        switch (dy) {
    
          case 0:
    
          case 1: idx = 1; len = 0; break;
    
          case 2: idx = 0; len = 0; break;
    
          case 3: idx = 0; len = 13; mask = 1; break;
    
          case 4:
    
          case 5: idx = 2; len = 38; break;
    
          case 6:
    
          case 7:
    
          case 8: idx = 0; len = 0; break;
    
          case 9: for (idx = 2; idx < 8; idx++) {
    
              //dy = 0xFF >> (7 - idx);
    
              dy = (idx - 2) * 51;
    
              colors[idx] = tft.color565(dy, dy, dy);
    
            }
    
            idx = 2; len = 38; break;
    
          case 10: idx = 1; len = 0; break;
    
          case 11:
    
          case 12: colors[2] = YELLOW; idx = 2; len = 0; break;
    
        }
    
        if (len == 0)
    
          tft.fillRect(160 - w, y, w * 2, 1, colors[idx]);
    
        else {
    
          if (mask == 1) idx = 1 + (w) / len;
    
          dy = w % len;
    
          for (x = 160 - w; x < 160 + w; idx++) {
    
            tft.fillRect(x, y, dy, 1, colors[idx & mask]);
    
            x += dy;
    
            if (x + len > 160 + w) dy = w % len;
    
            else dy = len;
    
          }
    
        }
    
      }
    
      for (x = 72, y = 129, dx = 5, dy = 0; dx > 0; x += 2 * dx) {
    
        tft.fillRect(x, y, dx, 36, WHITE);
    
        dy += dx * 2;
    
        if (dy >= 36) {
    
          dy = 0;
    
          dx--;
    
        }
    
      }
    
      tft.fillRect(160 - 8, 5 * 18 + 3, 17, 3 * 18, BLACK);
    
      for (x = 3 * 18 + 7, y = 6 * 18 + 3, w = 1, h = 18; x < 160 + 108; x += 18) {
    
        tft.fillRect(x, y, w, h, WHITE);
    
      }
    
      tft.fillRect(160 - 108, 120, 108 * 2, 1, WHITE);
    
      tft.fillRect(160, 5 * 18 + 3, 1, 3 * 18, WHITE);
    
      tft.fillRect(108, 2 * 18 + 3, 6 * 18, 18, WHITE);
    
      //  tft.fillRect(108, 10 * 18 + 3, 6 * 18, 18, BLACK);
    
      tft.fillRect(160 - 8, 11 * 18 + 3, 17, radius - 18*9/2, RED);
    
      tft.setCursor(160 - 36, 24 + ADJ_BASELINE);
    
      tft.setTextColor(BLACK);
    
      tft.setTextSize(1);
    
      tft.print("320x240");
    
      tft.setCursor(109, 43 + ADJ_BASELINE);
    
      tft.setTextColor(BLACK);
    
      tft.setTextSize(1);
    
      tft.print("ID=0x");
    
      tft.print(tft.readID(), HEX);
    
      tft.setTextColor(WHITE, BLACK);
    
      //  tft.setFont(NULL);
    
      //  tft.setTextSize(2);
    
       
    
      while (1) {
    
        if (++ss > 59) {
    
          ss = 0;
    
          mm++;
    
          if (mm > 59) {
    
            mm = 0;
    
            hh++;
    
            if (hh > 23) hh = 0;
    
          }
    
        }
    
        char buf[20];
    
        sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
    
        tft.fillRect(108, 10 * 18 + 3, 6 * 18, 18, BLACK);
    
        tft.setCursor(128, 187 + ADJ_BASELINE);
    
        tft.print(buf);
    
        delay(1000);
    
      }
    
    }
    

    Arduino实验场景图

    18.jpg

    相关文章

      网友评论

          本文标题:【雕爷学编程】Arduino动手做(153)---2.4寸TFT

          本文链接:https://www.haomeiwen.com/subject/jtykudtx.html