Step 5Let's Code and Test!
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 Step | Download PDFView All Steps | Next Step » |
2
comments
|
Add Comment
|
![]() |
Add Comment
|












































