2013-12-18 14:14:51 [显示全部楼层]
5128浏览
查看: 5128|回复: 0

[转载] [Arduino模块]TPA81 8Pixel热传感器

[复制链接]
[Arduino模块]
TPA81 8Pixel热传感器

电路示意:



代码示例:
  1. /********************************************
  2. *    arduino example for TPA81 and LCD03    *
  3. *         TPA81 controlled by I2C           *
  4. *        LCD03 controlled by serial         *
  5. *                                           *
  6. *         By James Henderson 2012           *
  7. ********************************************/
  8. #include <Wire.h>
  9. #include <SoftwareSerial.h>
  10. #define ADDRESS             0x68                                   // Address of TPA81
  11. #define SOFTREG             0x00                                   // Byte for software version
  12. #define AMBIANT             0x01                                   // Byte for ambiant temperature
  13. #define LCD_RX              0x02                                   // Pin for rx
  14. #define LCD_TX              0x03                                   // Pin for tx
  15. #define LCD03_HIDE_CUR      0x04
  16. #define LCD03_CLEAR         0x0C
  17. #define LCD03_SET_CUR       0x02
  18. #define LCD03_RETURN        0x0D
  19. SoftwareSerial lcd_03 = SoftwareSerial(LCD_RX, LCD_TX);  // Defines serial port for LCD03
  20. int temperature[] = {0,0,0,0,0,0,0,0};                   // Array to hold temperature data
  21. void setup(){
  22.   lcd_03.begin(9600);                                    // Starts software serial port for LCD03
  23.   Wire.begin();
  24.   delay(100);                                            // Wait to make sure everything is powerd up
  25.   
  26.   lcd_03.write(LCD03_CLEAR);   
  27.   lcd_03.write(LCD03_HIDE_CUR);
  28.   byte software = getData(SOFTREG);                       // Get software version
  29.   lcd_03.print("TPA81 Example  V:");
  30.   lcd_03.print(software);                                // Print software version to the screen
  31. }
  32.   
  33. void loop(){
  34.   for(int i = 0; i < 8; i++){                            // Loops and stores temperature data in array
  35.   temperature[i] = getData(i+2);
  36.   }
  37.   
  38.   lcd_03.write(LCD03_SET_CUR);                           
  39.   lcd_03.write(21);                                      // Moves cursor to space 21
  40.   for(int x = 0; x < 8; x++){                            // Loop prints each member of temperature to LCD03 followed by a space
  41.     if(x==4)                                             // If x is 4 perform a carriage return to format the results on the LCD03 for easier reading
  42.       lcd_03.write(LCD03_RETURN);
  43.     lcd_03.print(temperature[x]);
  44.     lcd_03.print(" ");
  45.     delay(50);                                           // Wait befor printing next value to give time for everything to be sent
  46.   }
  47.   
  48.   lcd_03.write(LCD03_RETURN);                          
  49.   int ambiantTemp = getData(AMBIANT);                    // Get reading of ambiant temperature and print to LCD03 screen
  50.   lcd_03.print("Ambiant: ");
  51.   lcd_03.print(ambiantTemp);
  52. }
  53. byte getData(byte reg){                                   // Function to receive one byte of data from TPA81
  54.   Wire.beginTransmission(ADDRESS);                        // Begin communication with TPA81
  55.     Wire.write(reg);                                      // Send reg to TPA81
  56.   Wire.endTransmission();
  57.   Wire.requestFrom(ADDRESS, 1);                           // Request 1 byte
  58.   while(Wire.available() < 1);                            // Wait for byte to arrive
  59.   byte data = Wire.read();                                // Get byte
  60.   return(data);                                           // return byte
  61. }
复制代码




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail