Guest User

Slave_Example

a guest
May 17th, 2017
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 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  0x26            // i2c slave address (38)
  19. #define LED1_PIN         4              // ATtiny Pin 3
  20. #define LED2_PIN         1              // ATtiny Pin 6
  21.  
  22. void setup(){
  23.   pinMode(LED1_PIN,OUTPUT);             // for general DEBUG use
  24.   pinMode(LED2_PIN,OUTPUT);             // for verification
  25.   Blink(LED1_PIN,2);                    // show it's alive
  26.   TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode
  27. }
  28.  
  29.  
  30. void loop(){
  31.   byte byteRcvd = 0;
  32.   if (TinyWireS.available()){           // got I2C input!
  33.     byteRcvd = TinyWireS.receive();     // get the byte from master
  34.     Blink(LED1_PIN,byteRcvd);           // master must wait for this to finish before calling Wire.requestFrom
  35.     byteRcvd += 10;                     // add 10 to what's received
  36.     TinyWireS.send(byteRcvd);           // send it back to master
  37.  
  38.     Blink(LED2_PIN,1);                  // show we transmitted
  39.   }
  40. }
  41.  
  42.  
  43. void Blink(byte led, byte times){ // poor man's display
  44.   for (byte i=0; i< times; i++){
  45.     digitalWrite(led,HIGH);
  46.     delay (250);
  47.     digitalWrite(led,LOW);
  48.     delay (175);
  49.   }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment