I Have an ATmega16 mc which is master on the i2c and a ATMega8 mc which is slave on the i2c.

I have connected the two mcs' sda and scl ports to each other alongside a pullup resistor.

Now I want to read a register from the ATMega8 using the ATMega16.

The problem is that I don't want to assign all the variables manually. Is there any libs or headers that will do this thing for me?

  • $\begingroup$ Belongs on EE SE $\endgroup$ Oct 25 '12 at 14:42
  • $\begingroup$ If you were to rephrase the question as "I've set up my registers as follows {...} but still can't get it to work..." we might be able to help a bit more $\endgroup$
    – Andrew
    Oct 25 '12 at 15:44
  • 1
    $\begingroup$ I wouldn't be too strict on the border to other fields. If you strip robotics of Electrical, Mechanical and Software Engineering, there isn't anything left. $\endgroup$
    – Jakob
    Oct 26 '12 at 7:40
  • $\begingroup$ Remember that just because a question is closed, doesn't mean that it has to stay closed. If this can be made more relevant to future robotics visitors, it could still be reopened. $\endgroup$
    – Mark Booth
    Oct 26 '12 at 20:52

I've not used the ATMega family, but have lots of experience of configuring many other devices - setting up the control registers for I2C is usually pretty straight forward, with appropriate reference to the datasheet/user-manual and will help you understand what's going on.

For such tasks, a library is (a) inefficient (b) overkill and (c) doesn't help you learn.

I don't know if there is one, but wouldn't recommend you use it, if there was...

  • $\begingroup$ I don't think this is very good advice. If there are good firmware libraries available for a Microcontroller I would always recommend to use them. They usually make for more structured code that is much easier to read and takes less time to write. So (a) inefficient is irrelevant at setup time, (b) it helps you learn as you see how its properly done when you browse the library code. $\endgroup$
    – Jakob
    Oct 26 '12 at 7:31
  • $\begingroup$ Hi Jakob... only giving my opinion :) And how many people bother to look at the library code? Few even manage to RTFM! For some aspects (eg USB, ethernet) there are standard protocols to follow... for I2C/SPI (and even RS232) there are not so a general purpose library cannot be efficient. $\endgroup$
    – Andrew
    Oct 26 '12 at 8:50
  • $\begingroup$ I fail to see how RTFM is a very good answer on stack exchange and then to add in some unrelated bias about libraries gets you a 'not useful' vote from me I'm afraid. Sorry. $\endgroup$
    – Mark Booth
    Oct 26 '12 at 20:57

It's been a while since I've used the AVR, but there where two libraries which would do what you are looking for. Unfortunately ATMEL doesn't provide proper firmware libraries themselves (as e.g. STM does).

  • Procyon AVRlib is quite outdated, but also very lightweight. You can just use the parts that you are interested in.

  • AVR Libc is another library which is slightly more recent and in my experience quite comprehensive. I think it is actually bundled with a lot of toolchains.

  • $\begingroup$ Thanks for the links... notwithstanding my answer, I'll have a look see :) $\endgroup$
    – Andrew
    Oct 26 '12 at 9:34

AVR publishes a set of guides for every microcontroller they make. See http://www.atmel.com/devices/atmega16.aspx?tab=documents, and obtain the ATmega16(L) Complete document.

You will find a chapter dedicated to I2C (it is named 2-wire Serial Interface (TWI)). While there may be libraries, the general practice is to set the appropriate registers manually.

Each register is given a name (all caps, like a constant) - an 8-bit integer you can set in C. Specific bits will be used to set one microcontroller as a master or slave. For example, the following registers are relevant to the two-wire interface.

TWI Register Description

  • TWI Bit Rate Register – TWBR
    • Bits 7...0 – TWI Bit Rate Register
  • TWI Control Register – TWCR
    • Bit 7 – TWINT: TWI Interrupt Flag
    • Bit 6 – TWEA: TWI Enable Acknowledge Bit
    • Bit 5 – TWSTA: TWI START Condition Bit
    • Bit 4 – TWSTO: TWI STOP Condition Bit
    • Bit 3 – TWWC: TWI Write Collision Flag
    • Bit 2 – TWEN: TWI Enable Bit
    • Bit 1 – Res: Reserved Bit
    • Bit 0 – TWIE: TWI Interrupt Enable
  • TWI Status Register – TWSR
  • TWI Data Register – TWDR
  • TWI (Slave) Address Register – TWAR

Note that you set certain bits to specify bit rate, the data to transfer, the address a slave responds to, and when to start/stop transfers (the control register). You can also obtain the status of transmissions from the status register.

The document will also walk you through the steps to use it:

enter image description here Obtained from the AVR "ATmega16(L) Complete" document


Not the answer you're looking for? Browse other questions tagged or ask your own question.