Guest User

Working_Slave

a guest
May 17th, 2017
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. /* ATtiny85 as an I2C Slave            BroHogan                           1/12/11
  2.  * Example of ATtiny I2C slave receiving and sending data to an Arduino master.
  3.  * Gets data from master, adds 10 to it and sends it back.
  4.  * SETUP:
  5.  * ATtiny Pin 1 = (RESET) N/U                      ATtiny Pin 2 = (D3) N/U
  6.  * ATtiny Pin 3 = (D4) to LED1                     ATtiny Pin 4 = GND
  7.  * ATtiny Pin 5 = I2C SDA on DS1621  & GPIO        ATtiny Pin 6 = (D1) to LED2
  8.  * ATtiny Pin 7 = I2C SCK on DS1621  & GPIO        ATtiny Pin 8 = VCC (2.7-5.5V)
  9.  * NOTE! - It's very important to use pullups on the SDA & SCL lines!
  10.  * Current Rx & Tx buffers set at 32 bytes - see usiTwiSlave.h
  11.  * Credit and thanks to Don Blake for his usiTwiSlave code.
  12.  * More on TinyWireS usage - see TinyWireS.h
  13.  */
  14.  
  15.  
  16. #include <TinyWireS.h>                  // wrapper class for I2C slave routines
  17.  
  18. #define I2C_SLAVE_ADDR  8            // i2c slave address (8) - defined in master code
  19. #define LED_PIN         8              // ATtiny PB2- Pin8
  20.  
  21.  
  22. void setup(){
  23.   pinMode(LED_PIN,OUTPUT);             // for general DEBUG use
  24.    digitalWrite(LED_PIN,HIGH);
  25.     delay (250);
  26.     digitalWrite(LED_PIN,LOW);
  27.     delay (175);                     // show it's alive
  28.   TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode
  29. }
  30.  
  31.  
  32. void loop(){
  33.   byte byteRcvd = 0;
  34.   if (TinyWireS.available()){           // TinyWireS.available() returns the number of bytes in the received buffer
  35.     byteRcvd = TinyWireS.receive();     // TinyWireS.receive()returns the next byte in the received buffer??
  36.     digitalWrite(LED_PIN,HIGH);
  37.     delay (250);
  38.     digitalWrite(LED_PIN,LOW);
  39.     delay (175);          // master must wait for this to finish before calling Wire.requestFrom
  40.     byteRcvd += 10;                     // add 10 to what's received
  41.     TinyWireS.send(byteRcvd);           // send it back to master
  42.   }
  43. }
  44.  
  45.  
  46. /*void Blink(byte led, byte times){ // poor man's display
  47.   for (byte i=0; i< times; i++){
  48.     digitalWrite(led,HIGH);
  49.     delay (250);
  50.     digitalWrite(led,LOW);
  51.     delay (175);
  52.   }
  53. }
  54.  
  55. */
Advertisement
Add Comment
Please, Sign In to add comment