Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2018 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b1bec3b298f72056d6d0d0e90585b8a5 to your computer and use it in GitHub Desktop.
Save anonymous/b1bec3b298f72056d6d0d0e90585b8a5 to your computer and use it in GitHub Desktop.
#include "Wire.h"
#define I2C_SLAVE_ADDR 0x10
#define RESET_PIN 2
void setup() {
Wire.begin();
pinMode(RESET_PIN,OUTPUT);
// reset the slave
digitalWrite(RESET_PIN, LOW);
delay(10);
digitalWrite(RESET_PIN, HIGH);
// wait for slave to finish any init sequence
delay(2000);
}
void loop() {
uint8_t i;
uint8_t inByte = 0;
uint8_t outByte;
uint8_t reqBytes;
// generate, save, and send N random byte values
Wire.beginTransmission(I2C_SLAVE_ADDR);
outByte = random(10);
Wire.write(outByte);
Wire.endTransmission();
//delay (500); // optional delay if required by slave (like sample ADC)
// read N bytes from slave
reqBytes = Wire.requestFrom(I2C_SLAVE_ADDR, (int)1); // Request N bytes from slave
if(Wire.available()){
Serial.print("Sent out byte: ");
Serial.print(outByte);
Serial.println("");
Serial.print("returned # bytes: ");
Serial.print(reqBytes);
Serial.println("");
for(i = 0;i<reqBytes;i++){
Serial.print("Got Bytes back: ");
inByte = Wire.read();
Serial.print(inByte);
Serial.println("");
}
}
else{
Serial.println("Wire request info empty");
}
// delay 1 second so user can watch results
delay(5000);
}
#include <TinyWireS.h>
#define I2C_SLAVE_ADDR 0x10
//HERE IS THE PIN OUTS OF THE ATTINY85-20PU:
//https://arduinotech.dk/wp-content/uploads/2013/11/ATTiny85-pin.jpg
//Best labelling i could find.
int led = 3;
int but = 4;
const int DEBOUNCE_TIME = 5;
// the setup routine runs once when you press reset:
// The default buffer size, Can't recall the scope of defines right now
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
uint8_t master_data[16];
// global variable to number of bytes sent from the master.
uint8_t master_bytes;
uint8_t master_byte;
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(but,INPUT);
TinyWireS.begin(I2C_SLAVE_ADDR);
TinyWireS.onRequest(requestEvent);
TinyWireS.onReceive(receiveEvent);
//LED turns on to indicate program is running and waiting.
digitalWrite(led,HIGH);
}
/*
* This is called for each read request we receive, never put more than one byte of data (with TinyWireS.send) to the
* send-buffer when using this callback
*/
void requestEvent()
{
uint8_t blinkCount;
//Depending on the amount of bytes, lets do a blink code.
digitalWrite(led,HIGH);
// for(blinkCount = 0;blinkCount < 3;blinkCount++){
//
// digitalWrite(led,HIGH);
// delay(400);
// digitalWrite(led,LOW);
// delay(400);
// }
TinyWireS.send(master_byte);
}
/**
* The I2C data received -handler
*
* This needs to complete before the next incoming transaction (start, data, restart/stop) on the bus does
* so be quick, set flags for long running tasks to be called from the mainloop instead of running them directly,
*/
void receiveEvent(uint8_t numBytes)
{
digitalWrite(led,LOW);
master_byte = 0;
//digitalWrite(led,HIGH);
if (numBytes < 1)
{
// Sanity-check
return;
}
if (numBytes > TWI_RX_BUFFER_SIZE)
{
// Also insane number
return;
}
//digitalWrite(led,LOW);
master_byte = TinyWireS.receive();
// uint8_t blinkCount;
// for(blinkCount = 0;blinkCount < master_byte;blinkCount++){
//
// digitalWrite(led,HIGH);
// delay(200);
// digitalWrite(led,LOW);
// delay(200);
// }
}
int butState = 0;
// the loop routine runs over and over again forever:
void loop() {
/**
* This is the only way we can detect stop condition (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=984716&sid=82e9dc7299a8243b86cf7969dd41b5b5#984716)
* it needs to be called in a very tight loop in order not to miss any (REMINDER: Do *not* use delay() anywhere, use tws_delay() instead).
* It will call the function registered via TinyWireS.onReceive(); if there is data in the buffer on stop.
*/
TinyWireS_stop_check();
//The Megahertz you assign to this, will need to divide the regular delay.
//If you are on 1 mhz, you don't divide, 300 milliseconds, is 300.
//If you are on 8 mhz, you need to divide, 300 / 8 = 38 to get the same frequency.
// butState = digitalRead(but);
// if(butState == HIGH){
// digitalWrite(led,HIGH);
// }
// else{
// digitalWrite(led,LOW);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment