2015年7月24日 星期五

reads the Temperature from the Texas Instruments TMP75 sensor using I2Ccommunication.

源自於
http://playground.arduino.cc/Main/TMP75I2CThermometerCode



This Sketch reads the Temperature from the Texas Instruments TMP75 sensor using I2Ccommunication. The temperature is read, sent to the serial port and to a 2 line by 16 Character TM162AD Hitachi based LCD.
Help from http://bildr.org/2011/01/tmp102-arduino/ for the bitwise Arithmetic, thanks.
No promise or claims of originality , quality or code correctness is made or implied. YMMV. Enjoy :).

Code

// Arduino Sketch to read the Texas Instruments 
// TMP75 Temperature Sensor
//  It uses the I2C in the Wire Library
//  Written by J.Park jim(_dot_)buzz(_at_)gmail(_dot_)com
//  23 April 2012 reference 

// Define includes
#include <LiquidCrystal.h>
#include <Wire.h>        // Wire header file for I2C and 2 wire

// I used a Sensor PCB from a scrapped White Intel based iMac
// Note there are two sensor boards both with a TMP75 sensor in the iMac.

// TMP75 Address is 0x48 and from its Datasheet = A0,A1,A2 are all grounded.
int TMP75_Address = 0x48; // Apple iMac Temp Sensor Circular PCB
//int TMP75_Address = 0x49; // Apple iMac Temp Sensor Square PCB

// LCD pinouts used (rs, enable, d4, d5, d6, d7) 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Declare some nice variables
int decPlaces = 1;
int numOfBytes = 2;
int baudRate = 9600;
int columns = 16;   // LCD is 16 Column 2 Row Hitachi LCD
int rows = 2;
byte configReg = 0x01;  // Address of Configuration Register
byte bitConv = B01100000;   // Set to 12 bit conversion
byte rdWr = 0x01;       // Set to read write
byte rdOnly = 0x00;    // Set to Read
String temperature, SerialDegC, lcdDegC;

// Setup the Arduino LCD (Columns, Rows)
void setup(){
  lcd.begin(columns, rows);          // set up the LCD's number of rows and columns:
  Serial.begin(baudRate);            // Set Serial Port speed
  Wire.begin();                      // Join the I2C bus as a master
  SerialDegC += char(176);           // Setup a Degrees C Serial symbol
  SerialDegC += "C ";
  lcdDegC += char(223);              // Setup a Degrees C LCD Symbol
  lcdDegC += "C ";
  Wire.beginTransmission(TMP75_Address);       // Address the TMP75 sensor
  Wire.write(configReg);                       // Address the Configuration register 
  Wire.write(bitConv);                         // Set the temperature resolution 
  Wire.endTransmission();                      // Stop transmitting
  Wire.beginTransmission(TMP75_Address);       // Address the TMP75 sensor
  Wire.write(rdOnly);                          // Address the Temperature register 
  Wire.endTransmission();                      // Stop transmitting 
}

// Main Program Infinite loop 
void loop(){
  float temp = readTemp();             // Read the temperature now
  Serial.print(temp,decPlaces);        // Temperature value to 1 Decimal place no newline to serial
  Serial.println(SerialDegC);          // Degree symbol after the temperature value with newline  
  lcd.setCursor(2,0);                  // Set Cursor position (Column,Row)
  lcd.print("Temperature  *");         // Print string to LCD
  lcd.setCursor(5,1);                  // Set Cursor position (Column,Row)
  lcd.print(temp,decPlaces);           // Print temperature value to 1 Decimal place no newline
  lcd.print(lcdDegC);
  delay(500);                          // Slow the reads down so we can follow the output
  lcd.setCursor(2,0);
  lcd.print("Temperature   ");
  delay(500);
}

// Begin the reading the TMP75 Sensor 
float readTemp(){
  // Now take a Temerature Reading
  Wire.requestFrom(TMP75_Address,numOfBytes);  // Address the TMP75 and set number of bytes to receive
  byte MostSigByte = Wire.read();              // Read the first byte this is the MSB
  byte LeastSigByte = Wire.read();             // Now Read the second byte this is the LSB

  // Being a 12 bit integer use 2's compliment for negative temperature values
  int TempSum = (((MostSigByte << 8) | LeastSigByte) >> 4); 
  // From Datasheet the TMP75 has a quantisation value of 0.0625 degreesC per bit
  float temp = (TempSum*0.0625);
  Serial.println(MostSigByte, BIN);   // Uncomment for debug of binary data from Sensor
  Serial.println(LeastSigByte, BIN);  // Uncomment for debug  of Binary data from Sensor
  return temp;                           // Return the temperature value
}

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...