Advertisement
martin2250

attiny 85 I²C

May 9th, 2014
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #define ADDRESS 0x10
  2.  
  3. byte pointer = 0;
  4.  
  5. long temp = 0;
  6. long pres = 0;
  7. long bat = 0;
  8.  
  9. long samples = 0;
  10.  
  11. byte i2cdata[10];
  12.  
  13. void setup()
  14. {
  15.   TinyWireS.begin(ADDRESS);
  16.   TinyWireS.onRequest(onRequest);
  17.   TinyWireS.onReceive(onReceive);
  18.   ADMUX |= _BV(REFS2) | _BV(REFS1);
  19.   ADMUX &= ~_BV(REFS0);
  20. }
  21.  
  22. void loop()
  23. {
  24.   bat += analogRead(A3);
  25.   TinyWireS_stop_check();
  26.   pres += analogRead(A2);
  27.   TinyWireS_stop_check();  
  28.   samples++;
  29.   tws_delay(100);
  30. }
  31.  
  32. void onRequest()
  33. {
  34.   TinyWireS.send(i2cdata[pointer]);
  35.   pointer++;
  36. }
  37.  
  38. void onReceive(byte c)
  39. {
  40.   byte x = TinyWireS.receive();
  41.   if(x == 255)
  42.   {
  43.       temp = 0;
  44.       pres = 0;
  45.       bat = 0;
  46.  
  47.       samples = 0;
  48.   }
  49.   else if(x == 254)
  50.   {
  51.     temp /= samples;
  52.     pres /= samples;
  53.     i2cdata[0] = *(&temp);
  54.     i2cdata[1] = *(&temp + 1);
  55.     i2cdata[2] = *(&temp + 2);
  56.     i2cdata[3] = *(&temp + 3);
  57.     pointer = 0;
  58.   }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement