搜索
热搜: ROHM 模拟 车载
查看: 4003|回复: 3

[分享] 霍尔传感器实例讲解

  [复制链接]

该用户从未签到

65

主题

171

帖子

0

精华

高级会员

最后登录
2023-8-24
发表于 2018-7-20 11:11:13 | 显示全部楼层 |阅读模式
本期我们将一边解读程序内容一边学习TFT显示器的操作!

目 录
  • 用Arduino在TFT液晶显示器上显示
  • 尝试利用加速度传感器的数值绘制图表
  • 总结
1.用Arduino在TFT液晶显示器上显示本期使用的TFT显示器是sainsmart公司的ST7735R显示器。此款显示器是一种小型显示器,除了Arduino外,Raspberry Pi等也可使用。上面贴装有microSD卡槽,可进行数据的读写。本期只尝试TFT显示器的显示功能。
首先,连接Arduino和TFT显示器。
4.jpg
照片1 TFT显示器


5.jpg
照片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,因此需要更改部分库文件。
SainSmart 1.8 ST7735R TFT LCD Module with MicroSD LED Backlight For Arduino Raspberry Pi
下载库(ST7735R V0.2)
打开上面的URL,在网页最下面有下载链接,点击写有"Download Link"的链接地址,即可将库、样本代码、文件等整套下载下来。下载完毕,解压压缩文件后,则可改写必要的文件。
文本用可编辑"ST7735.h"的编辑器打开后,更改第4行的如图所示部分。这样一来,即使Arduino也可以使用。
6.png

文件更改完毕后,请将解压的"TFT18"目录再次压缩为zip等格式,在Arduino(或Arduino Create)的Add Library作为库添加,或者设置在位于Arduino安装目录的"libraries"目录下来读取库。
读取完毕后,从sketch的样本尝试运行"TFT18"-"graphictest"。
查看样本程序,可以确认显示非常顺畅。
样本程序 – graphictest
  1. //引脚的设定

  2. #define sclk 4

  3. #define mosi 5

  4. #define cs 6

  5. #define dc 7

  6. #define rst 8



  7. //使用的颜色编号

  8. #define BLACK           0x0000

  9. #define BLUE            0x001F

  10. #define RED             0xF800

  11. #define GREEN           0x07E0

  12. #define CYAN            0x07FF

  13. #define MAGENTA         0xF81F

  14. #define YELLOW          0xFFE0

  15. #define WHITE           0xFFFF



  16. #include <ST7735.h>

  17. #include <SPI.h>




  18. ST7735 tft = ST7735(cs, dc, mosi, sclk, rst);





  19. void fillpixelbypixel(uint16_t color) {

  20. for (uint8_t x=0; x < tft.width; x++) {

  21. for (uint8_t y=0; y < tft.height; y++) {

  22. tft.drawPixel(x, y, color);

  23. }

  24. }

  25. delay(100);

  26. }



  27. void setup(void) {

  28. Serial.begin(9600);

  29. Serial.print("hello!");

  30. tft.initR();               // initialize a ST7735R chip




  31. Serial.println("init");

  32. tft.writecommand(ST7735_DISPON);



  33. uint16_t time = millis();

  34. tft.fillScreen(BLACK);

  35. time = millis() - time;



  36. Serial.println(time, DEC);

  37. delay(500);



  38. //

  39. tft.fillScreen(BLACK);

  40. testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  41. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa,

  42. fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut

  43. ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu

  44. hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet

  45. posuere. ", WHITE);

  46. delay(1000);



  47. //a single pixel

  48. tft.drawPixel(tft.width/2, tft.height/2, GREEN);

  49. delay(500);



  50. // line draw test

  51. testlines(YELLOW);

  52. delay(500);



  53. // optimized lines

  54. testfastlines(RED, BLUE);

  55. delay(500);



  56. testdrawrects(GREEN);

  57. delay(500);



  58. testfillrects(YELLOW, MAGENTA);

  59. delay(500);



  60. tft.fillScreen(BLACK);

  61. testfillcircles(10, BLUE);

  62. testdrawcircles(10, WHITE);



  63. Serial.println("done");

  64. delay(1000);

  65. }



  66. void loop() {

  67. tft.writecommand(ST7735_INVON);

  68. delay(500);

  69. tft.writecommand(ST7735_INVOFF);

  70. delay(500);

  71. }



  72. void testlines(uint16_t color) {

  73. tft.fillScreen(BLACK);

  74. for (uint16_t x=0; x < tft.width; x+=6) {

  75. tft.drawLine(0, 0, x, tft.height-1, color);

  76. }

  77. for (uint16_t y=0; y < tft.height; y+=6) {

  78. tft.drawLine(0, 0, tft.width-1, y, color);

  79. }



  80. tft.fillScreen(BLACK);

  81. for (uint16_t x=0; x < tft.width; x+=6) {

  82. tft.drawLine(tft.width-1, 0, x, tft.height-1, color);

  83. }

  84. for (uint16_t y=0; y < tft.height; y+=6) {

  85. tft.drawLine(tft.width-1, 0, 0, y, color);

  86. }



  87. tft.fillScreen(BLACK);

  88. for (uint16_t x=0; x < tft.width; x+=6) {

  89. tft.drawLine(0, tft.height-1, x, 0, color);

  90. }

  91. for (uint16_t y=0; y < tft.height; y+=6) {

  92. tft.drawLine(0, tft.height-1, tft.width-1, y, color);

  93. }



  94. tft.fillScreen(BLACK);

  95. for (uint16_t x=0; x < tft.width; x+=6) {

  96. tft.drawLine(tft.width-1, tft.height-1, x, 0, color);

  97. }

  98. for (uint16_t y=0; y < tft.height; y+=6) {

  99. tft.drawLine(tft.width-1, tft.height-1, 0, y, color);

  100. }



  101. }



  102. void testdrawtext(char *text, uint16_t color) {

  103. tft.drawString(0, 0, text, color);

  104. }



  105. void testfastlines(uint16_t color1, uint16_t color2) {

  106. tft.fillScreen(BLACK);

  107. for (uint16_t y=0; y < tft.height; y+=5) {

  108. tft.drawHorizontalLine(0, y, tft.width, color1);

  109. }

  110. for (uint16_t x=0; x < tft.width; x+=5) {

  111. tft.drawVerticalLine(x, 0, tft.height, color2);

  112. }

  113. }



  114. void testdrawrects(uint16_t color) {

  115. tft.fillScreen(BLACK);

  116. for (uint16_t x=0; x < tft.width; x+=6) { tft.drawRect(tft.width/2 -x/2,

  117. tft.height/2 -x/2 , x, x, color); } } void testfillrects(uint16_t color1,

  118. uint16_t color2) { tft.fillScreen(BLACK); for (uint16_t x=tft.width-1; x > 6;

  119. x-=6) {

  120. tft.fillRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color1);

  121. tft.drawRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color2);

  122. }

  123. }



  124. void testfillcircles(uint8_t radius, uint16_t color) {

  125. for (uint8_t x=radius; x < tft.width; x+=radius*2) {

  126. for (uint8_t y=radius; y < tft.height; y+=radius*2) {

  127. tft.fillCircle(x, y, radius, color);

  128. }

  129. }

  130. }



  131. void testdrawcircles(uint8_t radius, uint16_t color) {

  132. for (uint8_t x=0; x < tft.width+radius; x+=radius*2) {

  133. for (uint8_t y=0; y < tft.height+radius; y+=radius*2) {

  134. tft.drawCircle(x, y, radius, color);

  135. }

  136. }

  137. }
复制代码



在上面的程序中,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显示器侧的配线了。
7.jpg
照片3 加速度传感器和TFT显示器


显示加速度传感器数值的程序
  1. #include <Wire.h>

  2. #include <KX022.h>

  3. #include <ST7735.h>

  4. #include <SPI.h>





  5. // You can use any (4 or) 5 pins

  6. #define sclk 4

  7. #define mosi 5

  8. #define cs 6

  9. #define dc 7

  10. #define rst 8  // you can also connect this to the Arduino reset



  11. // Color definitions

  12. #define BLACK           0x0000

  13. #define BLUE            0x001F

  14. #define RED             0xF800

  15. #define GREEN           0x07E0

  16. #define CYAN            0x07FF

  17. #define MAGENTA         0xF81F

  18. #define YELLOW          0xFFE0

  19. #define WHITE           0xFFFF



  20. ST7735 tft = ST7735(cs, dc, mosi, sclk, rst);



  21. KX022 kx022(KX022_DEVICE_ADDRESS_1E);



  22. int _cnt = 0;

  23. //图表初始位置

  24. int _xc = 120;

  25. int _yc = 130;

  26. int _zc = 140;




  27. void fillpixelbypixel(uint16_t color) {

  28. for (uint8_t x=0; x < tft.width; x++) {

  29. for (uint8_t y=0; y < tft.height; y++) {

  30. tft.drawPixel(x, y, color);

  31. }

  32. }

  33. delay(100);

  34. }



  35. void setup(void) {

  36. byte rc;



  37. Serial.begin(9600);

  38. while (!Serial);

  39. Wire.begin();

  40. tft.initR(); // initialize a ST7735R chip

  41. rc = kx022.init();

  42. tft.fillScreen(BLACK);




  43. 1.显示文字DEVICE PLUS

  44. testdrawtext("DEVICE PLUS!!", WHITE,25,50);

  45. delay(1000);

  46. tft.fillScreen(BLACK);

  47. }



  48. void loop() {

  49. //KX022

  50. byte rc;

  51. float acc[3];



  52. //2.获取加速度传感器的值

  53. rc = kx022.get_val(acc);

  54. if (rc == 0) {

  55. Serial.write("KX022 (X) = ");

  56. Serial.print(acc[0]);

  57. Serial.println(" [g]");

  58. Serial.write("KX022 (Y) = ");

  59. Serial.print(acc[1]);

  60. Serial.println(" [g]");

  61. Serial.write("KX022 (Z) = ");

  62. Serial.print(acc[2]);

  63. Serial.println(" [g]");

  64. Serial.println();



  65. //将float型转换为char型

  66. char xVal[10];

  67. dtostrf(acc[0], 5, 2, xVal);

  68. char yVal[10];

  69. dtostrf(acc[1], 5, 2, yVal);

  70. char zVal[10];

  71. dtostrf(acc[2], 5, 2, zVal);



  72. //转换为TFT液晶

  73. //tft.fillScreen(BLACK);

  74. tft.fillRect(0,0, 120, 60, BLACK);

  75. testdrawtext("X:", RED, 5, 15);

  76. testdrawtext(xVal, WHITE, 30, 15);

  77. testdrawtext("Y:", BLUE, 5, 30);

  78. testdrawtext(yVal, WHITE, 30, 30);

  79. testdrawtext("Z:", GREEN, 5, 45);

  80. testdrawtext(zVal, WHITE, 30, 45);



  81. //3.绘制图表

  82. int x = int(acc[0]*100)+120;

  83. int y = int(acc[1]*100)+130;

  84. int z = int(acc[2]*100)+40;

  85. tft.drawLine(_cnt-1, _xc, _cnt, x, RED);

  86. tft.drawLine(_cnt-1, _yc, _cnt, y, BLUE);

  87. tft.drawLine(_cnt-1, _zc, _cnt, z, GREEN);



  88. _cnt++;

  89. //到达画面边缘时复位

  90. if(_cnt > 120){

  91. _cnt = 0;

  92. tft.fillScreen(BLACK);

  93. }



  94. _xc = x;

  95. _yc = y;

  96. _zc = z;



  97. delay(10);

  98. }

  99. delay(10);

  100. }



  101. void testlines(uint16_t color) {

  102. tft.fillScreen(BLACK);

  103. for (uint16_t x=0; x < tft.width; x+=6) {

  104. tft.drawLine(0, 0, x, tft.height-1, color);

  105. }

  106. for (uint16_t y=0; y < tft.height; y+=6) {

  107. tft.drawLine(0, 0, tft.width-1, y, color);

  108. }



  109. tft.fillScreen(BLACK);

  110. for (uint16_t x=0; x < tft.width; x+=6) {

  111. tft.drawLine(tft.width-1, 0, x, tft.height-1, color);

  112. }

  113. for (uint16_t y=0; y < tft.height; y+=6) {

  114. tft.drawLine(tft.width-1, 0, 0, y, color);

  115. }



  116. tft.fillScreen(BLACK);

  117. for (uint16_t x=0; x < tft.width; x+=6) {

  118. tft.drawLine(0, tft.height-1, x, 0, color);

  119. }

  120. for (uint16_t y=0; y < tft.height; y+=6) {

  121. tft.drawLine(0, tft.height-1, tft.width-1, y, color);

  122. }



  123. tft.fillScreen(BLACK);

  124. for (uint16_t x=0; x < tft.width; x+=6) {

  125. tft.drawLine(tft.width-1, tft.height-1, x, 0, color);

  126. }

  127. for (uint16_t y=0; y < tft.height; y+=6) {

  128. tft.drawLine(tft.width-1, tft.height-1, 0, y, color);

  129. }

  130. }



  131. void testdrawtext(char *text, uint16_t color,int x,int y) {

  132. tft.drawString(x, y, text, color);

  133. }



  134. void testfastlines(uint16_t color1, uint16_t color2) {

  135. tft.fillScreen(BLACK);

  136. for (uint16_t y=0; y < tft.height; y+=5) {

  137. tft.drawHorizontalLine(0, y, tft.width, color1);

  138. }

  139. for (uint16_t x=0; x < tft.width; x+=5) {

  140. tft.drawVerticalLine(x, 0, tft.height, color2);

  141. }

  142. }



  143. void testdrawrects(uint16_t color) {

  144. tft.fillScreen(BLACK);

  145. for (uint16_t x=0; x < tft.width; x+=6) { tft.drawRect(tft.width/2 -x/2,

  146. tft.height/2 -x/2 , x, x, color); } } void testfillrects(uint16_t color1,

  147. uint16_t color2) { tft.fillScreen(BLACK); for (uint16_t x=tft.width-1; x > 6;

  148. x-=6) {

  149. tft.fillRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color1);

  150. tft.drawRect(tft.width/2 -x/2, tft.height/2 -x/2 , x, x, color2);

  151. }

  152. }



  153. void testfillcircles(uint8_t radius, uint16_t color) {

  154. for (uint8_t x=radius; x < tft.width; x+=radius*2) {

  155. for (uint8_t y=radius; y < tft.height; y+=radius*2) {

  156. tft.fillCircle(x, y, radius, color);

  157. }

  158. }

  159. }



  160. void testdrawcircles(uint8_t radius, uint16_t color) {

  161. for (uint8_t x=0; x < tft.width+radius; x+=radius*2) {

  162. for (uint8_t y=0; y < tft.height+radius; y+=radius*2) {

  163. tft.drawCircle(x, y, radius, color);

  164. }

  165. }

  166. }
复制代码



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

程序的流程如下:
  • 在setup内显示"DEVICE PLUS!!"文字
  • 获取加速度传感器的值并取整
  • 根据数值显示图表和文本
本期是每帧都在x轴上加1,从左向右绘制图表。到达边上的120px时,将用drawrect清除图表。另外,上部数字也是一样,每帧都用drawrect进行更新。

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

3.jpg
回复

使用道具 举报

该用户从未签到

1153

主题

5959

帖子

0

精华

论坛元老

最后登录
2021-2-19
发表于 2018-7-20 16:02:50 | 显示全部楼层
代码和实验不并存么
回复 支持 反对

使用道具 举报

该用户从未签到

2248

主题

1万

帖子

1

精华

论坛元老

最后登录
2024-4-24
发表于 2018-7-24 08:42:40 | 显示全部楼层
文章写的很不错
回复 支持 反对

使用道具 举报

该用户从未签到

14

主题

304

帖子

0

精华

高级会员

最后登录
2020-6-24
发表于 2018-7-24 09:47:07 | 显示全部楼层
代码很长
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|罗姆半导体技术社区

GMT+8, 2024-4-24 10:56 , Processed in 0.119609 second(s), 18 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表