I2C b/w arduino uno (atmega328p) and LIS3DH without Wire.h

Hi.

I have to work with attiny20 and LIS3DH. Since attiny20 doesn't support Wire.h, I am trying to set up the communication without Wire.h in atmel studio 7.0.

I came up with following code and have modified it according to my purpose but it doesn't seem to work.

0x18 - device address
0x38 - register address
0x15 - value to be written

------------------------------------------- i2c.h --------------------------------------------------------

/* Header File for I2C */

#include <avr/io.h> //--- Reg file of ATmega16

#define F_CPU 8000000UL //--- CPU Clock Frequency

//--- I2C Initialize ---//

void i2c_init()
{
TWBR = 0x20; //--- Baud rate is set by calculating the of TWBR see Details for Reference
TWCR = (1<<TWEN); //--- Enable I2C
TWSR = 0x00; //--- Prescaler set to 1
}

//--- I2C Start Condition ---//

void i2c_start()
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA); //--- Start Conditon
while (!(TWCR & (1<<TWINT))); //--- Check for Start condition successful
}

//--- I2C Stop Condition ---//

void i2c_stop()
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); //--- Stop Conditon
while (!(TWCR & (1<<TWINT))); //--- Check for Start condition successful
}

void i2c_write(char x)
{
TWDR = x; //--- Move value to I2C reg
TWCR = (1<<TWINT) | (1<<TWEN); //--- Enable I2C and Clear Interrupt
while (!(TWCR & (1<<TWINT))); //--- Write Successful with TWDR Empty
}

//--- I2C Read Condition ---//

uint8_t i2c_read()
{
TWCR = (1<<TWEN) | (1<<TWINT); //--- Enable I2C and Clear Interrupt
while (!(TWCR & (1<<TWINT))); //--- Read successful with all data received in TWDR
return TWDR;
}

//--- End of Header ---//

------------------------------------- main.c --------------------------------------------------------------

/*

  • i2c_ioexpander.c
  • Created: 12/11/2017 2:10:55 PM
  • Author : Ayush
    */

#include <avr/io.h> //--- Reg file of Atmega16
#include "i2c.h" //--- Header file for i2c which we created
#include <util/delay.h> //--- Delay Header
#include <stdio.h>

int main(void)
{
DDRB = 0xFF; //--- PORTB as OUTPUT
PORTB = 0xFF;
i2c_init(); //--- Initiate I2C comm
i2c_start(); //--- Start Condition to Slave device
i2c_write(0x18); //--- Write address of Slave into I2C to select slave
while(1)
{
i2c_write(0x38); //--- Write data to IO Expander
_delay_ms(1000); //--- 1sec Delay
i2c_write(0x15); //--- Write data to IO Expander
_delay_ms(1000); //--- 1sec Delay
}
}

Ayush21298:
Hi.

I have to work with attiny20 and LIS3DH. Since attiny20 doesn't support Wire.h, I am trying to set up the communication without Wire.h in atmel studio 7.0.

I came up with following code and have modified it according to my purpose but it doesn't seem to work.

Please use the code tags when posting code.

Why do you state that "you have to work with attiny20 and LIS30H"?
the attiny20 is TWI/I2C Slave only. the attiny20 cannot initiate a transfer.

the LIS30H is also a TWI/I2C Slave.
http://www.st.com/content/ccc/resource/technical/document/datasheet/3c/ae/50/85/d6/b1/46/fe/CD00274221.pdf/files/CD00274221.pdf/jcr:content/translations/en.CD00274221.pdf

http://www.atmel.com/images/atmel-2565-using-the-twi-module-as-i2c-slave_applicationnote_avr311.pdf

So what are you using for the TWI/I2C Master?

What is it you are attempting to do?
What is the purpose of your project?

I would like to read your Topic Title as follows:

I2C Bus with arduino uno (atmega328p) and LIS3DH without Wire.h.

If so, I would like to understand that you want to connect the following slave devices with the TWI Bus of Arduino UNO:

Attiny20 MCU
LIS3DH (3-axis accelerometer)

Then you can very well use the following TWI Bus Commands of Arduino (after including Wire.h) to communicate with the above slaves devices:

Wire.begin();
Wire.beginTransmission();
Wire.write();
Wire.endTransmission()

Wire.requestFrom();
Wire.available();
Wire.read();

You may read/practice these documents ([doc1](http://www.microcontrollerolympiadbd.com/UNO-EEPROM (2).pdf) and [doc2](http://www.microcontrollerolympiad.com/UNO-BMP180 (2).pdf)) to have hands-on experience on the interfacing of TWI Bus slaves using Wire.h.

GolamMostafa:
I would like to read your Topic Title as follows:

I2C Bus with arduino uno (atmega328p) and LIS3DH without Wire.h.

If so, I would like to understand that you want to connect the following slave devices with the TWI Bus of Arduino UNO:

Attiny20 MCU
LIS3DH (3-axis accelerometer)

Then you can very well use the following TWI Bus Commands of Arduino (after including Wire.h) to communicate with the above slaves devices:

Wire.begin();

Wire.beginTransmission();
Wire.write();
Wire.endTransmission()

Wire.requestFrom();
Wire.available();
Wire.read();




You may read this documents ([doc1](http://www.microcontrollerolympiadbd.com/UNO-EEPROM (2).pdf) and [doc2](http://www.microcontrollerolympiad.com/UNO-BMP180 (2).pdf)) to have hands-on experience on the interfacing of TWI Bus slaves using Wire.h.

he is not using Arduino IDE, he is using amtel 7, which I have not idea about.
I do not run windows.

If he continues to request help he needs to switch to using the Arduino IDE and he needs to use some device tp be the TWI/I2C master. If he were to use an Arduino UNO he has no reason to use a attiny20.

The OP has been talking about Arduino UNO, ATmega328P, TWI Bus, and TWI Bus Register of ATmega328P. He has also been talking about Atmel Studio 7, Attiniy20, and LIS3DH.

Atmel Studio 7 is an IDE which supports both Assembly and C/C++; I think (I tried but failed) that it does not support Arduino UNO which means that hex file generated by Atmel Studio 7 can not be uploaded into Arduino UNO using Atmel Studio 7 IDE.

Therefore, the OP must use Arduino IDE to program his Arduino UNO.

I have paraphrased his (OP) positing a little bit to accommodate Attiny20 as a slave TWI device.

Atmel7 can upload .hex files, but one needs a Programmer to do it.
I've only used Atmel AVR ISP MKii, may need some fooling around to get other types to work.

Out of the whole discussion, I got to know very important thing that attiny20 can't be used as TWI master.

Although my question was not clear. Sorry about it.

The basic question was "How can I set up a TWI/I2C communication b/w atmega328p and LIS3DH without Wire.h ?".

If you don't want to use the high level commands of the Wire.h, you may very well finish data exchange with TWI slaves using register level instructions that involve TWCR, TWSR, and TWDR registers of the Master ATmega328P. You have shown some of the register level instructions in your OP which appear to be very criptic. Some of these register level instructions of simpler format can be found in the linked documents of Post#2.

My recommendation would be to use the Wire.h library first and then to use the register level instructions.