3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

I2C Bus for ATtiny and ATmega

Step 5Let's Code and Test!

Let\
It's time to build the drivers and the example programs. We'll start with the ATtiny2313 and the PCA8574A breadboard that we just built. Download the file I2C.zip into your I2C work directory and unzip it. You'll have a new folder called I2C. In it you'll find USI I2C (for ATtiny2313) and TWI I2C (for ATmega168). In USI I2C, you'll find the I_O Port folder. That folder contains the code for our first example program, and the USI I2C drivers.

Using WinAVR, compile and load the code into the ATtiny2313. Take a deep breath and turn on the power. Here's what to expect:
At power on, LED 1 on port PD6 of the ATtiny2313 blinks twice.
Nothing else will happen until you push the button (S1). Each time the button is pressed, the switches are read and their setting will be displayed on the LEDs connected to the PCA8574A. Change the value of the switches, press the button, and the LEDs should change. Keep doing this until you get over the thrill of seeing it work. If (God forbid!) things don't work as expected, carefully check over your wiring. I2C errors will be signaled by blinks on LED3 (PD4) and probably mean you need to check that SDA and SCL are connected to the correct pins and are pulled up correctly. If things still don't work, read the rest of this section to learn about debugging.

Now go back and let's have a look at the code. Open the file USI_I2C_Port.c. This is the code for the example program. (USI_TWI_Master.c and USI_TWI_Master.h contain the drivers - you can ignore them unless you're curious.) Use the example to guide your own I2C applications.

Mostly, the program shows you how to initialize and use the I2C drivers, including setting up the slave address and the rest of the message buffer, and getting the data out of it. You'll also see how I debounce the button and set up the while loop. There are a few details of the program worth mentioning. Note that the data from the switches is inverted before it is written to the LEDs on the Port Expander. Also note that the input ports on the Port Expander must be written as High to make them work properly. Those details are described in the PCA8574A data sheet. Always read the data sheets carefully!

Of more interest is the use of conditional debugging. Near the start of the program file is the statement //#define DEBUG and sprinkled throughout the code are #ifdef DEBUG statements. As long as DEBUG is not defined (the two slashes make the line a comment and keep it from being defined), the code within the #ifdef to #endif statements will not be compiled. But if things don't work as you expect, recompile and reload the code with #define DEBUG uncommented. You'll get a lot more blinks on the LEDs which you can decode to follow the execution of your program and aid you in finding exactly where things go wrong. In fact, I recommend you try this just to see what happens.

What you'll see is that LED 2 (on PD5) will blink as execution progress through the program. The value read from the switches will be blinked on LED 1 (PD6) before it is displayed on the Port Expander LEDs. You should be able to track the program as it runs by using these LEDs.

We'll work with the ATmega168 next; skip this section if you're only interested in the ATtiny2313. Still with me? Good. Move to the TWI_I2C folder, change your working directory to IO_Port, and compile and load TWI_I2C_Port.c into the ATmega168. Disconnect the SDA and SCL lines from the ATtiny2313 and connect them to ATmega168. Hook up power and ground, and power up. The operation should be the same! Play until the thrill subsides, then let's look at the code.

Open TWI_I2C_Port.c. The code is nearly identical except for error handling and accommodating interrupt driven drivers. Here are the differences:
Note that the clock must be set to 4MHz for the I2C bus to work properly.
The sei(); statement turns on interrupts after initialization of the I2C drivers.
To check for errors, a specific status bit is tested.
During a read, the TWI_Read_Data_From_Buffer function must be called to transfer the data read into the message buffer.
During a write, while ( TWI_Transceiver_Busy() ) must be used to be sure the transfer is complete before checking for errors.
These last two functions are described above in the description of the drivers. Other than that, the code's pretty much the same as for the ATtiny2313. DEBUG works the same also if you want to experiment with that.
I2C.zip47 KB
« Previous StepDownload PDFView All StepsNext Step »
2 comments
Jul 12, 2009. 6:23 AMfabelizer says:
Great experiment, if I can only get it to compile in AVR Studio4! Does anyone have any tips on opening this project in AVR Studio 4? Even though Studio4 allows me to manually open the USI_I2C_Port.c file, when made into a project file, it complains: >Coordinator: None of the available object file readers can read the specified >object file. Please check the format of the object file. >Error loading object file I:\My Documents\AVR\Programs\I2C\USI I2C\I_O >Port\USI_I2C_Port.c and stops. Suggestions? Thanks, -fab
Jul 12, 2009. 11:32 AMfabelizer says:
Ok, I got it compiled. Apparently the USI_TWI_Master.c has to be put into the source files area in the project, then there were a few minor version related changes, but the code is now in the tn2313! -fab

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
41
Followers
8
Author:doctek