[TUT] I2C Bus for ATtiny and ATmega

Go To Last Post
36 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've created a fairly extensive Instructable on the I2C Bus. It offers a complete set of drivers for the ATtiny2313 and the ATmega168 - probably pretty easy to adapt to others. There're schematics showing an I/O Port Expander, an SRAM, and an EEPROM with complete example programs.

The Instructable is here: http://www.instructables.com/id/I2C_Bus_for_ATtiny_and_ATmega/

Hope you find it useful!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried using the program TWI_I2C_EEPROM.c, and followed the associated schematic diagram, and I got this result:
"Warning: return type of 'main' is not 'int'"
error:"'CLKPR' undeclared (first use in this function)"

What did I do wrong?

I hope I can get this program working, as it is one that I have been trying to find, and will be very useful.

Thank you
Patrick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sounds like you are not including in your application, or you've selected the incorrect AVR model in your makefile. Make sure that your source files have this line at the top somewhere:

#include 

Either in the .c file, or in a .h file that the .c file includes.

As for the makefile, ensure that the "MCU = " line has your correct AVR model listed.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Answer and discussion are posted in Instructable.

Basically, Patrick was using a ATmega8, so there is no CLKPR register - have to set fuses to make the clock go 8MHz.

Warning about Main can be ignored - I can't get it to go away.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your main functions must use the protype:

int main (void)

As the int return type (error code back to the calling OS) is mandated in the C specification. Of course, this makes no sense on the AVR which has no OS, but it is still required -- just place a "return 0" at the end of main and it will make the compiler happy. As your AVR programs should never return anyway, the optimiser will see the return value as dead code and remove it from the compiled output.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i want to build communication i2c between micro to micro (atmega32 to atmega8) not to IC eeprom.
anyone have this tutorial?
thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

confused with address atmega8 as a slave,
should i make my own address(with programming)?
Help me please!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

should i make my own address(with programming)?

Yup, that's what TWAR is for.

EDIT: Devinur, Oh great - I see now that you cross posted the same question in the GCC forum where it had already attracted two very useful answers. Please do not cross post in future as it just wastes people's time (as it did for me when I posted this)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Requires $ for download, Can any one post somewhere else??

Nandhu

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to comment, since i have tried this tutorial (or in the process of working it through), that it is compatible with both ATmega8 and ATmega328p (but for the former, you need to redefine the pins in a header file, i got mine off of nerdkits, but since it was a member exclusive, i cannot post it here). Very good tutorial/instructable. Just one note on the eeprom for m168: is it supposed to blink with all the lights and then go off again, or is this a compatibility issue possibly and i should use my m168 instead of the m328p?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bump.

Could anyone post the .pdf file or copy paste it in here? You have to become a member and get your VISA card abused all over the internet to get access to that tutorial.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey rg2720, I dont think anyone can post the pdf file here because it is the copyright of instructable (at least I think so), however, if you want a copy, just give me your email and i send it to you...

cs

I'm happy ytd, today, and tmr :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hmmm, i don't think the instructables get copyrighted when they are posted, you just have to have permission from the author to make into a private .pdf, so it would be better to ask doctek through a PM for permission for this, since i can probably make it into a pdf easily enough, with commented pictures

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Could anyone post the .pdf file or copy paste it in here? You have to become a member and get your VISA card abused all over the internet to get access to that tutorial.

I accessed it with a free registry, easy and quite non-commitive to do. (only required an email address, doesnt even have to be your own)

The CLKPR register however, doesn't seem to exist for my ATmega32. I'm assuming I can set the clock rate in the main header as usual and make this project go 'round just fine, but I'm wondering how it's done run-time?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You cannot change the CPU speed at runtime in a mega32 but what use is he making of CLKPR in the mega168 (I cannot access the code)? Surely he's just over-riding the setting of the CKDIV8 fuse anyway? Can you quote the few lines where CLKPR is being accesed? On a mega32 the chances are it's just a case of making a one time adjustment to the CLKSEL fuses.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
CLKPR = 0x80;		// Initiate write cycle for clock setting
CLKPR = 0x01;		// 4 MHz clock

Is written in main as part of the initialisation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK then you just need to fuse the mega32 for the 4MHz internal oscillator option to achieve the same.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I2c has 3 modes of 0peration with different speeds
how does it happen ?
as in how can the same Micro controller with the same frame format send different baud rates ??? what is the exact reason for that ??
like I2c can transmit with 400kb/s 100kb/s and also 3.4Mb/s
how does this basically happen ??? like i udnerstand how it happens in UART you increas the Baud rate and all.. but howdoes it happen here ??? the clock frequency also remains teh same in this case..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The I2C Clock signal is not really a *clock* in the conventional sense, but rather a latch signal. Data bits are latched on the clock edge. So the clock (latch) pulses pace the transfer. Think of the speed ratings as the upper limit for the transfer rate - you can't go any faster, but you can go slower. This makes slower rated parts compatible with faster parts - the I2C speed just must match the speed of the slowest device on the bus.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It would be most appropriate that mr doctek would not ask us to pay and register in order to see his i2c code. I think that's what forums are for. He;p each other. not make money, am I right Mr doctek?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Stergio,

What are you talking about? - you only have to sign up for "pro membership" if you want to see the entire tutorial as either a PDF or on a single web page. Otherwise you click the page icons to the right of those two options to see each page in turn - but the entire article is freely accessible this way.

BTW it's not doctek who would charging anyway - it's instructables.com who have to pay for the web hosting facilities some way. One way is to encourage "Pro" membership which allows PDF/full-page access with ess adverts or the other is to split articles over multiple pages so that each can show embedded adverts - but this is simply the price a user such as doctek "pays" for hosting the article on a commercial site.

Moderator.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

It would be most appropriate that mr doctek would not ask us to pay and register in order to see his i2c code. I think that's what forums are for. He;p each other. not make money, am I right Mr doctek?

What are you talking about? The linked Instructables tutorial is entirely free to read, I just re-checked it.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe you can put it somewhere else than on instructables? Most people don't have an instructables account (you have to pay for it) and cannot see the required details. I would like to check out how you did certain things but I cannot.

abcminiuser wrote:

What are you talking about? The linked Instructables tutorial is entirely free to read, I just re-checked it.
- Dean :twisted:

Try opening the drawings with the schematics for example.

I consider this a very unusable tutorial in this way... hope you fix it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All code, etc. should be available from Instructables without paying any money. I'm pretty sure you do have to register, but that's no different than this site. Last time I checked, there was no charge to download the code. If that's changed, I'll gladly post to this site.

You should know two things, stergio33:
First, I've never gotten a dime from any of my postings. That's not why I do this. My goal is to share and help others as I've been helped.
Second, If you have any trouble getting the code, send me an email and I'll make sure you get the code.
The reason I point people to the Instructables site is so all the comments can be collected there. It's also much easier to post a longer instructable with code and pictures to Instructables than it is to this site.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I really enjoyed your tutorial doctek. I've read instructables for over a year and never signed up until yours.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are there any others tutorials about i2c/twi in the atmegas?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have just downloaded the code. looks nice.

There is one small thng though that I do not fully understand. Inside the ISR you declare TWI_bufPtr.

If I got it right, then this means that every time the ISR is called there is a risk that this variable has gotten a random value.
Should it not be declared outside the ISR ?? and also be volatile as it has to be changed inside the ISR?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can't access the code as it appears you need to sign in to instructables to access it (which rather makes a comedy of this being a tutorial on AVR Freaks but ho hum - presumably other readers are willing to sign up?). However are you SURE it's not simply that the variable is 'static'?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes it is declared static inside the function......
you asking this made me do a search......

I was under the assumption that declaring a variable static only has effect when declaring it outside a function in a file so that it was only visible there.

Did a search and a new world opened. This is a better way to have variables that have to be kept after a function exit, but have to keep their value.

thanks for the hint....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
What are you talking about? - you only have to sign up for "pro membership" if you want to see the entire tutorial as either a PDF or on a single web page. Otherwise you click the page icons to the right of those two options to see each page in turn - but the entire article is freely accessible this way.

Oh! that is how it works. I hope you forgive me, but it was not very clear. Since it asked me to be a pro member I did not search for free way.
Thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I had a quick look at the text of the tutorial. Sorry, Is there somewhere the code? I can't find it.
Mr Doctec, I apologise, I was just asked to be a pro member, and I stopped searching. This is missleading, since I was looking in may places for such a tutorial. I will read it, thank you for taking the trouble, to answer.
One thing that keeps me from sending money through the web is that I have to give credit card details.
Thank you and please forgive me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a page that says now were going to code.....

on that page there is a link to i2C.zip

you need to sign on to get that code.

when you go to signing on there is the possibility of doing a free login.
it is something like 'i'm happy to go with the free account for now' it is in a green bar on the bottom of the subscription window.

after that I could download the package.

having this discussion again perhaps doctek is better of also posting his zip file here.

regards

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear meslomp,
Thanks a lot, I just downloaded the zips.
I can't understand the idea, of the site of "instructables" of providing information for free but having you to solve puzles, if you don't pay.
Yes I copied and pasted in the word, why giving me a hard time?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wishing to be as helpful as possible, I've posted all the files to the AVR Projects section. You can now get the schematics, all the code and even a pdf of the Instructable there. There is one limitation that I have been planning to address for a while. The code will not address larger EEPROMs. It's an easy extension to fix this, and I've actually done it and given the code to a person who asked. But I haven't had time to test it myself, so don't really want to release it into the wild yet.

So if there's some genuine interest and need, please post and I'll try to respond on xmas break.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I want to know how to set the address for the slave/master device for using twi in atmega MCU.

http://www.engineersgarage.com/e...

In this tutorial, the address is taken as 0x20, how this has been defined ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What device are you planning to use? The device has certain addresses or range of addresses to which it responds. That information in usually in the data sheet for the device.

You might also inquire on the site from which you got the link. My guess is that they have two AVR devices talking to each other. In that case, since the slave is fully programmable, they use whatever I2C address they like - it's arbitrary.

HTH.