// filename: Attiny85_spi
//Sketch uses 976 bytes (16%) of program storage space. Maximum is 6012 bytes.
//Global variables use 20 bytes of dynamic memory.
void setup() {
// put your setup code here, to run once:
//Sample code for configuring ATtiny85 as slave and receive data:
#include<avr/io.h>
#include <I2Cdev.h>
#include <TinyWireM.h>
#include<TinyDebug.h>
String received_data;
//PB2 - SCL
//PB0 - SDA
short int fmax;
short int i=0;
initialize();
}
//======================================================
void loop() {
// put your main code here, to run repeatedly main();
main();
}
// =====================================================
void initialize()
{
USICR=(1<<USIWM1)|(1<<USICS1)|(1<<USICLK); //TWI mode
DDRB=0x00; //SDA & SCL direction as input
PORTB=0x00; //SDA & SCL default state
USISR=0x00; //Counter value
}
void i2c_address()
{
char usi_data;
while(USISIF==0); //Wait till address bit is received
{
usi_data=USIDR;
if(usi_data==0x00) //Verifying the address
{
i2c_ack(); //Send acknowledge bit
int fmax=1;
}
}
}
void i2c_ack()
{
DDRB|=(1<<PB0); //Set the direction to output
USISR|=(1<<USIOIF); //clear overflow flags
PORTB|=(1<<PB0);
while((USISR&0x01)==0); //Wait until counter goes 1
PORTB&=~(1<<PB0); //Ack bit end
USISR&=0xF0; //Clear counter bits
}
void receive_data()
{
short int i=0;
if(( fmax==1)&&(USIOIF==1)) //Checked address and counter overflow bits
{
received_data[i]=USIDR; //Received data stored in string
i++;
USISR|=(1<<USIOIF); //Clear overflow flag
}
}
int main()
{
initialize(); //i2c configuration
i2c_address(); //check address
i2c_ack(); //acknowledge bit
while(1)
{
receive_data(); //receive data and storing data
}
}