I have been trying to get this working all night, but for some reason the I2C scanner written by Nick Gammon wont detect it.. (not his fault..)
Right now i am just trying to add I2C to my current code, so I have commented out anything that was not needed for the I2C scanner to pick it up..
Can anyone tell me what i have missed
Any help is greatly appreciated. ![]()
I2C Lib from GitHub - rambo/TinyWire: My modifications to TinyWire Arduino libs
My code
/*
rst -Â - VCC
3 -Â - 2
4 -Â - 1
GND -Â - 0
*/
//Include libaries\\
//----------------\\
#include <util/delay.h>
#include <avr/io.h>
#include "TinyWireS.h"
#define F_CPU 8000000
#define I2C_SLAVE_ADDRESS 0x26
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
//#define Interupt 1
//#define ledpin 3
//#define PWMPin 4
int fanRPS = 0; //Fans revalutions per second
char charToSend = ' ';
int fanDC = 85; //Fans duty cycle
// typedef struct{Â Â Â Â Â Â Â Â Â //Defines the structure for multiple fans and their dividers
// char fantype;
// unsigned int fandiv;
// }fanspec;
// fanspec fanspace[3]={{0,1},{1,2},{2,8}};
// char fan = 2;
//----------------------------------------------//
void setup(){
/*
//PWM setup\\
//---------\\
//Control Register A for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
//TCCR0A is 8 bits: [COM0A1:COM0A0:COM0B1:COM0B0:unused:unused:WGM01:WGM00]
//2<<COM0A0: sets bits COM0A0 and COM0A1, which (in Fast PWM mode) clears OC0A on compare-match, and sets OC0A at BOTTOM
//2<<COM0B0: sets bits COM0B0 and COM0B1, which (in Fast PWM mode) clears OC0B on compare-match, and sets OC0B at BOTTOM
//3<<WGM00: sets bits WGM00 and WGM01, which (when combined with WGM02 from TCCR0B below) enables Fast PWM mode
TCCR0A = 2<<COM0A0 | 2<<COM0B0 | 3<<WGM00;
//Control Register B for Timer/Counter-0 (Timer/Counter-0 is configured using two registers: A and B)
//TCCR0B is 8 bits: [FOC0A:FOC0B:unused:unused:WGM02:CS02:CS01:CS00]
//0<<WGM02: bit WGM02 remains clear, which (when combined with WGM00 and WGM01 from TCCR0A above) enables Fast PWM mode
//1<<CS00: sets bits CS01 (leaving CS01 and CS02 clear), which tells Timer/Counter-0 to not use a prescalar
TCCR0B = 0<<WGM02 | 1<<CS00;
//Control Register for Timer/Counter-1 (Timer/Counter-1 is configured with just one register: this one)
//TCCR1 is 8 bits: [CTC1:PWM1A:COM1A1:COM1A0:CS13:CS12:CS11:CS10]
//0<<PWM1A: bit PWM1A remains clear, which prevents Timer/Counter-1 from using pin OC1A (which is shared with OC0B)
//0<<COM1A0: bits COM1A0 and COM1A1 remain clear, which also prevents Timer/Counter-1 from using pin OC1A (see PWM1A above)
//1<<CS10: sets bit CS11 which tells Timer/Counter-1Â to not use a prescalar
TCCR1 = 0<<PWM1A | 0<<COM1A0 | 2<<CS10;
//General Control Register for Timer/Counter-1 (this is for Timer/Counter-1 and is a poorly named register)
//GTCCR is 8 bits: [TSM:PWM1B:COM1B1:COM1B0:FOC1B:FOC1A:PSR1:PSR0]
//1<<PWM1B: sets bit PWM1B which enables the use of OC1B (since we disabled using OC1A in TCCR1)
//2<<COM1B0: sets bit COM1B1 and leaves COM1B0 clear, which (when in PWM mode) clears OC1B on compare-match, and sets at BOTTOM
GTCCR = 1<<PWM1B | 2<<COM1B0;
OCR1C = 170;
*/
//Interupt setup\\
//--------------\\
// GIMSK = 1<<PCIE;
// PCMSK = 1<<PCINT1;
//Pin setup\\
//---------\\
// pinMode(Interupt, INPUT);
// pinMode(ledpin, OUTPUT);
// pinMode(PWMPin, OUTPUT);
// digitalWrite(ledpin, LOW);
//I2C setup\\
//---------\\
TinyWireS.begin(I2C_SLAVE_ADDRESS);
// TinyWireS.onReceive(receiveEvent);
// TinyWireS.onRequest(requestEvent);
// if(getRPM() < 500){
// fan = 1;
// }
}
//----------------------------------------------//
void loop(){
//digitalWrite(ledpin, LOW);
fanRPS = fanDC * 5 / 10;
charToSend = char(fanRPS);
TinyWireS_stop_check();
}
//----------------------------------------------//
void requestEvent(){
TinyWireS.send(charToSend);
}
void receiveEvent(uint8_t howMany){
if(TinyWireS.available()){Â
// if(howMany < 1){
// return;
// }
// if(howMany > TWI_RX_BUFFER_SIZE){
// return;
// }
// howMany--;
// if(!howMany){
// return;
// }
// while(howMany--){
fanDC = TinyWireS.receive() - '0';
//}
}
}
//----------------------------------------------//
// ISR(PCINT0_vect){ //This is the function that the interupt calls
// if(digitalRead(Interupt) == HIGH){
// fanRPS++;
// }
// }
//----------------------------------------------//
//int getRPM(){
// int Calc = 0;
// fanRPS = 0; //Set fanRPS to 0 ready for calculations
// PCMSK = 1<<PCINT1; //Enables interrupts
// _delay_ms(1000); //Wait 1 second
// PCMSK = 0<<PCINT1; //Disable interrupts
// Calc = ((fanRPS * 60)/fanspace[fan].fandiv); //Times fanRPS (which is apprioxiamately the fequency the fan is spinning at) by 60 seconds before dividing by the fan's divider
// return Calc;
//}
This is some example code from http://letsmakerobots.com/node/39130 that works and is detected
// the 7-bit address (remember to change this when adapting this example)
#define I2C_SLAVE_ADDRESS 0x26
// Get this from https://github.com/rambo/TinyWire
#include <util/delay.h>
#include <avr/io.h>
#include <TinyWireS.h>
// 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
//Character variable used to echo data back.
char chrSendData;
//Variables used in getting and parsing data.
char rxChrData; //Receives the data.
char rxString[12];Â //Varbiable for holding one string of data.
int rxIndex = 0; //Used to index rxString.
//Integer for holding the pwm value received from master.
int pwmValA;
void requestEvent(){Â
 TinyWireS.send(chrSendData);
}
//Handles receiving i2c data.
void receiveEvent(uint8_t howMany)
{
  if (TinyWireS.available()){Â
   if (howMany < 1)
   { // Sanity-check
     return;
   }
   if (howMany > TWI_RX_BUFFER_SIZE)
   { // Also insane number
     return;
   }
Â
   howMany--;
   if (!howMany)
   { // This write was only to set the buffer for next read
     return;
   }
   while(howMany--)
   { //Gets i2c data.
     rxChrData = TinyWireS.receive();
     //Places the characters in an array one at a time.
     rxString[rxIndex] = char(rxChrData);
     //Increment the data array.
     rxIndex++;
     //If a stop character is read, parse the char array and convert it to a single integer.Â
     if (rxChrData == ':'){
       //This is a low memory form of parsing the char array into an intger
       pwmValA = int(100*rxString[2]+10*rxString[3]+rxString[4]);
       //Prints the parsed value.
       Serial.println(pwmValA);
       //Writes the parsed value to pin 1 (PB1).
       digitalWrite(3, HIGH);
       //Resets the char array index.
       rxIndex = 0;Â
    }
   }
  }
}
void setup()
{
  Serial.begin(9600);
  pinMode(1, OUTPUT); // OC1A, also The only HW-PWM -pin supported by the tiny core analogWrite
  TinyWireS.begin(I2C_SLAVE_ADDRESS);
  //Sets up the onReceive function (what we do if we get stuff).
  TinyWireS.onReceive(receiveEvent);
  //Sets up the onRequest function (what we do if asked to send something).
  TinyWireS.onRequest(requestEvent);
}
void loop()
{
//Detects a stop sending command.
TinyWireS_stop_check();
//Puts the data we got into a variable to send back for error checking.
chrSendData = char(rxChrData);
}