尝试制作使用加速度传感器和tft液晶面板的游戏!arduino+传感器的开发更加轻松!罗姆传感器评估套件的试用 |从零开始,电子工程超入门|罗姆传感器评估套件|罗姆传感器|罗姆半导体集团(rohm semiconductor)-尊龙凯时ag客户端

转载自 device plus,获得了相关许可,特此声明

上期我们把加速度传感器的检测结果显示在tft液晶面板上,本期我们将一边解读程序内容一边学习tft显示器的操作!

此次的电子工程配方

完成为止的大致时间标准:60分钟
必要零件

※罗姆传感器评估套件可通过以下网站购买!

目 录

  1. 用arduino在tft液晶显示器上显示
  2. 尝试利用加速度传感器的数值绘制图表
  3. 总结

1.用arduino在tft液晶显示器上显示

本期使用的tft显示器是。此款显示器是一种小型显示器,除了arduino外,raspberry pi等也可使用。上面贴装有microsd卡槽,可进行数据的读写。本期只尝试tft显示器的显示功能。

首先,连接arduino和tft显示器。

照片1 tft显示器

 

照片2 tft显示器的背面

顺便说一句,写在电路板上的引脚名称及作用大致如下:

  • vcc – 电源输入(correction voltage)
  • gnd – 接地(ground)
  • scl – 串行时钟线(serial clock line)
  • sda – 串行数据线(serial data line)
  • rs/dc – 指令/数据选取(command/data selection)
  • res – 复位(lcd controller reset)
  • cs – 芯片选择(chipselect for tf card)

arduino和tft显示器连接完毕后,就可以试着运行样本程序。

 

使tft显示器的库适用于arduino

虽然该tft显示器利用库st7735r可以在arduino上显示,但st7735r无法直接用于arduino,因此需要更改部分库文件。

打开上面的url,在网页最下面有下载链接,点击写有"download link"的链接地址,即可将库、样本代码、文件等整套下载下来。下载完毕,解压压缩文件后,则可改写必要的文件。

文本用可编辑"st7735.h"的编辑器打开后,更改第4行的如图所示部分。这样一来,即使arduino也可以使用。

 

文件更改完毕后,请将解压的"tft18"目录再次压缩为zip等格式,在arduino(或arduino create)的add library作为库添加,或者设置在位于arduino安装目录的"libraries"目录下来读取库。

读取完毕后,从sketch的样本尝试运行"tft18"-"graphictest"。

查看样本程序,可以确认显示非常顺畅。
样本程序 – graphictest

//引脚的设定
#define sclk 4
#define mosi 5
#define cs 6
#define dc 7
#define rst 8
//使用的颜色编号
#define	black           0x0000
#define	blue            0x001f
#define	red             0xf800
#define	green           0x07e0
#define cyan            0x07ff
#define magenta         0xf81f
#define yellow          0xffe0
#define white           0xffff
#include 
#include 

st7735 tft = st7735(cs, dc, mosi, sclk, rst);

void fillpixelbypixel(uint16_t color) {
  for (uint8_t x=0; x < tft.width; x  ) {
    for (uint8_t y=0; y < tft.height; y  ) {
      tft.drawpixel(x, y, color);
    }
  }
  delay(100);
}
void setup(void) {
  serial.begin(9600);
  serial.print("hello!");
  tft.initr();               // initialize a st7735r chip

  serial.println("init");
  tft.writecommand(st7735_dispon);
  uint16_t time = millis();
  tft.fillscreen(black);
  time = millis() - time;
  serial.println(time, dec);
  delay(500);
  //
  tft.fillscreen(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. ", white);
  delay(1000);
  //a single pixel
  tft.drawpixel(tft.width/2, tft.height/2, green);
  delay(500);
  // line draw test
  testlines(yellow);
  delay(500);
  // optimized lines
  testfastlines(red, blue);
  delay(500);
  testdrawrects(green);
  delay(500);
  testfillrects(yellow, magenta);
  delay(500);
  tft.fillscreen(black);
  testfillcircles(10, blue);
  testdrawcircles(10, white);
  serial.println("done");
  delay(1000);
}
void loop() {
  tft.writecommand(st7735_invon);
  delay(500);
  tft.writecommand(st7735_invoff);
  delay(500);
}
void testlines(uint16_t color) {
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(0, 0, x, tft.height-1, color);
   }
   for (uint16_t y=0; y < tft.height; y =6) {
     tft.drawline(0, 0, tft.width-1, y, color);
   }
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(tft.width-1, 0, x, tft.height-1, color);
   }
   for (uint16_t y=0; y < tft.height; y =6) {
     tft.drawline(tft.width-1, 0, 0, y, color);
   }
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(0, tft.height-1, x, 0, color);
   }
   for (uint16_t y=0; y < tft.height; y =6) {
     tft.drawline(0, tft.height-1, tft.width-1, y, color);
   }
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(tft.width-1, tft.height-1, x, 0, color);
   }
   for (uint16_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.drawstring(0, 0, text, color);
}
void testfastlines(uint16_t color1, uint16_t color2) {
   tft.fillscreen(black);
   for (uint16_t y=0; y < tft.height; y =5) {
     tft.drawhorizontalline(0, y, tft.width, color1);
   }
   for (uint16_t x=0; x < tft.width; x =5) {
     tft.drawverticalline(x, 0, tft.height, color2);
   }
}
void testdrawrects(uint16_t color) {
 tft.fillscreen(black);
 for (uint16_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(black); for (uint16_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 (uint8_t x=radius; x < tft.width; x =radius*2) {
    for (uint8_t y=radius; y < tft.height; y =radius*2) {
      tft.fillcircle(x, y, radius, color);
    }
  }
}
void testdrawcircles(uint8_t radius, uint16_t color) {
  for (uint8_t x=0; x < tft.width radius; x =radius*2) {
    for (uint8_t y=0; y < tft.height radius; y =radius*2) {
      tft.drawcircle(x, y, radius, color);
    }
  }
}

在上面的程序中,tft操作的中心函数如下:

  • tft.drawpixel(x,y,color); - 在指定位置(x,y)显示指定颜色(color)的点。
  • tft.drawcircle(x, y, radius, color); - 在指定位置(x,y)绘制指定半径(radius)的圆。
  • tft.fillrect(x1,y1, x2, y2, color); - 以指定位置1(x1,y1)到位置2(x2,y2)之间的宽度和高度涂一个长方形。
  • tft.drawstring(x, y, text, color); - 在指定位置(x,y)用指定颜色(color)显示文本。
  • tft.fillscreen(0x0000); - 整个显示器屏幕按指定颜色显示

此外还有几个函数,但基本上用这些就可以实现丰富的表现。

 

2.尝试利用加速度传感器的数值绘制图表

确认完tft显示器的工作后,接下来我们试着在tft显示器上显示加速度传感器的值。使用传感器评估套件时,只要将加速度传感器安装在套件上,基本上就不用更改tft显示器侧的配线了。

照片3 加速度传感器和tft显示器

 

显示加速度传感器数值的程序

#include 
#include 
#include 
#include 

// you can use any (4 or) 5 pins
#define sclk 4
#define mosi 5
#define cs 6
#define dc 7
#define rst 8  // you can also connect this to the arduino reset
// color definitions
#define	black           0x0000
#define	blue            0x001f
#define	red             0xf800
#define	green           0x07e0
#define cyan            0x07ff
#define magenta         0xf81f
#define yellow          0xffe0
#define white           0xffff
st7735 tft = st7735(cs, dc, mosi, sclk, rst);
kx022 kx022(kx022_device_address_1e);
int _cnt = 0;
//图表初始位置
int _xc = 120;
int _yc = 130;
int _zc = 140;

void fillpixelbypixel(uint16_t color) {
 for (uint8_t x=0; x < tft.width; x  ) {
 for (uint8_t y=0; y < tft.height; y  ) {
 tft.drawpixel(x, y, color);
 }
 }
 delay(100);
}
void setup(void) {
 byte rc;
 serial.begin(9600);
 while (!serial);
 wire.begin();
 tft.initr(); // initialize a st7735r chip
 rc = kx022.init();
 tft.fillscreen(black);

 1.显示文字device plus
 testdrawtext("device plus!!", white,25,50);
 delay(1000);
 tft.fillscreen(black);
}
void loop() {
 //kx022
 byte rc;
 float acc[3];
//2.获取加速度传感器的值
 rc = kx022.get_val(acc);
 if (rc == 0) {
 serial.write("kx022 (x) = ");
 serial.print(acc[0]);
 serial.println(" [g]");
 serial.write("kx022 (y) = ");
 serial.print(acc[1]);
 serial.println(" [g]");
 serial.write("kx022 (z) = ");
 serial.print(acc[2]);
 serial.println(" [g]");
 serial.println();
 //将float型转换为char型
 char xval[10];
 dtostrf(acc[0], 5, 2, xval);
 char yval[10];
 dtostrf(acc[1], 5, 2, yval);
 char zval[10];
 dtostrf(acc[2], 5, 2, zval);
 //转换为tft液晶
 //tft.fillscreen(black);
 tft.fillrect(0,0, 120, 60, black);
 testdrawtext("x:", red, 5, 15);
 testdrawtext(xval, white, 30, 15);
 testdrawtext("y:", blue, 5, 30);
 testdrawtext(yval, white, 30, 30);
 testdrawtext("z:", green, 5, 45);
 testdrawtext(zval, white, 30, 45);
 //3.绘制图表
 int x = int(acc[0]*100) 120;
 int y = int(acc[1]*100) 130;
 int z = int(acc[2]*100) 40;
 tft.drawline(_cnt-1, _xc, _cnt, x, red);
 tft.drawline(_cnt-1, _yc, _cnt, y, blue);
 tft.drawline(_cnt-1, _zc, _cnt, z, green);
 _cnt  ;
 //到达画面边缘时复位
 if(_cnt > 120){
 _cnt = 0;
 tft.fillscreen(black);
 }
 _xc = x;
 _yc = y;
 _zc = z;
 delay(10);
 }
 delay(10);
}
void testlines(uint16_t color) {
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(0, 0, x, tft.height-1, color);
   }
   for (uint16_t y=0; y < tft.height; y =6) {
     tft.drawline(0, 0, tft.width-1, y, color);
   }
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(tft.width-1, 0, x, tft.height-1, color);
   }
   for (uint16_t y=0; y < tft.height; y =6) {
     tft.drawline(tft.width-1, 0, 0, y, color);
   }
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(0, tft.height-1, x, 0, color);
   }
   for (uint16_t y=0; y < tft.height; y =6) {
     tft.drawline(0, tft.height-1, tft.width-1, y, color);
   }
	
   tft.fillscreen(black);
   for (uint16_t x=0; x < tft.width; x =6) {
     tft.drawline(tft.width-1, tft.height-1, x, 0, color);
   }
   for (uint16_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,int x,int y) {
  tft.drawstring(x, y, text, color);
}
void testfastlines(uint16_t color1, uint16_t color2) {
   tft.fillscreen(black);
   for (uint16_t y=0; y < tft.height; y =5) {
     tft.drawhorizontalline(0, y, tft.width, color1);
   }
   for (uint16_t x=0; x < tft.width; x =5) {
     tft.drawverticalline(x, 0, tft.height, color2);
   }
}
void testdrawrects(uint16_t color) {
 tft.fillscreen(black);
 for (uint16_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(black); for (uint16_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 (uint8_t x=radius; x < tft.width; x =radius*2) {
    for (uint8_t y=radius; y < tft.height; y =radius*2) {
      tft.fillcircle(x, y, radius, color);
    }
  }
}
void testdrawcircles(uint8_t radius, uint16_t color) {
  for (uint8_t x=0; x < tft.width radius; x =radius*2) {
    for (uint8_t y=0; y < tft.height radius; y =radius*2) {
      tft.drawcircle(x, y, radius, color);
    }
  }
}

启动上面的程序后,就会显示上期介绍的加速度传感器数值的图表。

 

程序的流程如下:

  1. 在setup内显示"device plus!!"文字
  2. 获取加速度传感器的值并取整
  3. 根据数值显示图表和文本

本期是每帧都在x轴上加1,从左向右绘制图表。到达边上的120px时,将用drawrect清除图表。另外,上部数字也是一样,每帧都用drawrect进行更新。

 

总结

到现在为止,我们使用传感器评估套件测试了很多的传感器及部件。本期的小型tft显示器也是一样,通过轻松操控arduino或raspberry pi等小型计算机,可以根据我们的想法实现平时用普通电脑无法办到的事。因为普通的电脑较贵,无法任意随便使用,而arduino等与普通电脑相比价格相对便宜,所以例如将本期用到的tft显示器和arduino pro mini等组合起来,既可制作一个时钟或者小型游戏,还可以加上传感器评估套件的传感器做成一个数据记录仪。