Problem with ATTiny85 + ADXL345 + TinyWireM

Hi
I am having problems with my setup:

  • An ATTiny85 running at 8 Mhz
  • An ADXL345 connected to the ATTiny85 (pins 5 & 7) with pullups
  • TinyWireM set for 8Mhz

For some un explained reason, I can connect tot the ADXL345. But as soon as I change a register, it loses the connection.
my test code is the following:

#include "ADXL345.h"
#include "TinyWireM.h"
#include "TinyDebugserial.h"

TinyDebugSerial serial;

ADXL345 adxl(0x53); //variable adxl is an instance of the ADXL345 library

void setup(){
	serial.begin(9600);
        serial.println("First check connected");
	if(adxl.EnsureConnected()){
		serial.println("ADXL Connected");
	} else {
		serial.println("ADXL Not Connected");
	}
	adxl.SetRange(2, true);
	adxl.EnableMeasurements();;
}

void loop(){
	//
	// read the accelerometer
	//
	AccelerometerRaw raw = adxl.ReadRawAxis();
	//
	// Output x,y,z values
	//
	serial.print(raw.XAxis);
	serial.print(" - ");
	serial.print(raw.YAxis);
	serial.print(" - ");
	serial.println(raw.ZAxis);
	if(adxl.EnsureConnected()){
		serial.println("ADXL Connected");
	} else {
		serial.println("ADXL Not Connected");
	}
	//
	// delay 1 second
	//
	unsigned long x = millis() + 1000L;
	while (millis() < x) {};
}

the ADXL345 code I am using is:

/*
ADXL345.cpp - Header file for the ADXL345 Triple Axis Accelerometer Arduino Library.
Copyright (C) 2011 Love Electronics (loveelectronics.co.uk)

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

 Datasheet for ADXL345:
 http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf

*/
#include <Arduino.h>
#include "ADXL345.h"		// ADXL345 class definitions
#include <TinyWireM.h>			// I2C Master lib for Arduino UNO

ADXL345::ADXL345()
{
  m_Address = DefaultADXL345_Address;
  TinyWireM.begin();
}

ADXL345::ADXL345(uint8_t customAddress)
{
	m_Address = customAddress;
	TinyWireM.begin();
}

AccelerometerRaw ADXL345::ReadRawAxis()
{
	uint8_t* buffer = Read(Register_DataX, 6);
	AccelerometerRaw raw = AccelerometerRaw();
	raw.XAxis = (buffer[1] << 8) | buffer[0];
	raw.YAxis = (buffer[3] << 8) | buffer[2];
	raw.ZAxis = (buffer[5] << 8) | buffer[4];
	return raw;
}

AccelerometerScaled ADXL345::ReadScaledAxis()
{
	AccelerometerRaw raw = ReadRawAxis();
	AccelerometerScaled scaled = AccelerometerScaled();
	scaled.XAxis = raw.XAxis * m_Scale;
	scaled.YAxis = raw.YAxis * m_Scale;
	scaled.ZAxis = raw.ZAxis * m_Scale;
	return scaled;
}

void ADXL345::EnableMeasurements()
{
	Write(Register_PowerControl, 0x08);
}

void ADXL345::SetRange(int range, bool fullResolution)
{

	// Get current data from this register.
	uint8_t data = Read(Register_DataFormat, 1)[0];

	// We AND with 0xF4 to clear the bits are going to set.
	// Clearing ----X-XX
	data &= 0xF4;

	// By default (range 2) or FullResolution = true, scale is 2G.
	m_Scale = ScaleFor2G;
	
	// Set the range bits.
	switch(range)
	{
		case 2:
			break;
		case 4:
			data |= 0x01;
			if(!fullResolution) { m_Scale = ScaleFor4G; }
			break;
		case 8:
			data |= 0x02;
			if(!fullResolution) { m_Scale = ScaleFor8G; }
			break;
		case 16:
			data |= 0x03;
			if(!fullResolution) { m_Scale = ScaleFor16G; }
			break;
		default:
			break;
	}

	// Set the full resolution bit.
	if(fullResolution)
		data |= 0x08;

	Write(Register_DataFormat, data);
}

uint8_t ADXL345::EnsureConnected()
{
	uint8_t data = Read(0x00, 1)[0];
	
	if(data == 0xE5)
		IsConnected = true;
	else
		IsConnected = false;

	return IsConnected;
}

void ADXL345::Write(int address, int data)
{
	TinyWireM.beginTransmission(m_Address);
	TinyWireM.send(address);
	TinyWireM.send(data);
	TinyWireM.endTransmission();
}

uint8_t* ADXL345::Read(int address, int length)
{
	TinyWireM.beginTransmission(m_Address);
	TinyWireM.send(address);
	TinyWireM.endTransmission();
  
	TinyWireM.beginTransmission(m_Address);
	TinyWireM.requestFrom(m_Address, length);

	if(TinyWireM.available() == length)
	{
		for(uint8_t i = 0; i < length; i++)
		{
			buffer[i] = TinyWireM.receive();
		}
	}
	TinyWireM.endTransmission();

	return buffer;
}

I have compiled with Eclipse + Arduino 1.5.2, Arduino 1.0.5, programmed through Arduino ISP using AVRDUDESS and using the Arduino 1.0.5 IDE. All give the same results.
The code did work in the past, but that was tested withoug the serial option.
Is there something I am missing?

The problem wasn't the software. it was the $%^&*(&^%$ chip. Either the clock is way off at 8Mhz causing all kinds of timing problems or it is simply defect. I used a brand new one to start with. Picked another new one and this one is fine. Could be the clock itself so I will have a look at it later on.
Also switched from TinyWireM to DigitialIO and modified it for the tiny. Worked fine.