Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #define I2C_SLAVE_ADDRESS 0x4 // the 7-bit address
  4.  
  5. #define MEASURE_TEMPERATURE 0xA0
  6. #define SEND_TEMPERATURE 0xB0
  7.  
  8. #define TEMP_CONST 10 // (1 Celcius == 10 mV)
  9. #define TEMP_RESOLUTION 1024 // 2^10 (10 bits)
  10. #define UNIT_CHANGE 1000 // (1V = 1000 mV)
  11. #if defined(ARDUINO_ARCH_ESP8266) || defined(__CC3200R1M1RGC__)
  12. #define SUPPLY_VOLTAGE 3.3 // VCC votage for ESP8266
  13. #else
  14. #define SUPPLY_VOLTAGE 5.0 // VCC votage for ATmega328p
  15. #endif
  16.  
  17.  
  18. float temp;
  19. const float temp_magic_no = (((SUPPLY_VOLTAGE * UNIT_CHANGE) / TEMP_RESOLUTION) / TEMP_CONST);
  20. int temperature;
  21. volatile uint8_t buffer[2];
  22.  
  23.  
  24. void setup()
  25. {
  26. Wire.begin(); // join i2c bus (address optional for master)
  27. Serial.begin(115200);
  28. Serial.println("START...");
  29. }
  30.  
  31.  
  32. void loop()
  33. {
  34. Wire.beginTransmission(I2C_SLAVE_ADDRESS); // transmit to device #4
  35. Wire.write(MEASURE_TEMPERATURE); // sends five bytes
  36. Wire.endTransmission(); // stop transmitting
  37. delay(100);
  38.  
  39. Wire.beginTransmission(I2C_SLAVE_ADDRESS); // transmit to device #4
  40. Wire.write(SEND_TEMPERATURE);
  41. Wire.endTransmission();
  42. Wire.requestFrom(I2C_SLAVE_ADDRESS, 2);
  43. buffer[0] = Wire.read();
  44. buffer[1] = Wire.read();
  45. temperature = (buffer[0] << 8) | buffer[1];
  46.  
  47. Serial.print("raw adc value = ");
  48. Serial.println(temperature);
  49.  
  50. temp = temperature * temp_magic_no;
  51. Serial.print("current temperature = ");
  52. Serial.println(temp);
  53.  
  54. delay(2000);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement