| Author |
Message |
|
|
Posted: Jun 30, 2007 - 12:16 AM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Hi,
This post is intended for Don Blake, but we thought others might be able to contribute/benefit from it as well.
I am trying to get my ATtiny45 Slave device using the USI-TWI interface to send data back to an ATmega88 Master device using standard TWI.
I've had plenty of success sending commands to the ATtiny, but getting it to return anything has proven difficult to say the least.
I was using the IAR code from AVR312 in a demo version of IAR, but I found Don's conversion of that code for Atmels GCC and would much rather use that since I'm familiar with AVRstudio and the STK500.
My question is, has anyone had any success getting a slave to return data over the USI-TWI interface and if so, would you care to share that code or at least the experience with a novice experimenter?
Also, for Don, can I use the AVR312 main.c with your converted code?
Thanks, Dan Gates |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Jun 30, 2007 - 01:28 AM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
|
topazx2 wrote:
My question is, has anyone had any success getting a slave to return data over the USI-TWI interface and if so, would you care to share that code or at least the experience with a novice experimenter?
Dan, I only have one application that uses the USI-TWI interface on an ATtiny2313. It does not read from the slave device. I've never even tried reading from the slave. When I get a chance, I'll look over the code and see if there's anything obvious.
I don't think I ever would have gotten this code working without my oscilloscope. Have you used an oscilloscope to see what's going on during a slave read?
Are you calling usiTwiTransmitByte to place data in the transmission buffer before the master tries to read from the slave? What do you see happening on the master side during a slave read?
BTW - the code that Dan is refereeing to is an attachment in my post of 16 April 2007 in this thread.
topazx2 wrote:
Also, for Don, can I use the AVR312 main.c with your converted code?
I've never even looked at main.c before just a little bit ago. Off hand, I don't see why not as long as you change the function names to match my implementation.
Don |
|
|
| |
|
|
|
|
|
Posted: Jul 01, 2007 - 01:05 AM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
Dan, another question: what speed are you running the ATtiny45 at? Also, are you trying to run the I2C bus at 100 KHz or 400 KHz?
I'm not familiar with the ATtiny45 - the ATtiny2313 defaults to 1 MHz (8 MHz internal RC oscillator with divide clock by 8). I can't find the reference, but IIRC, 1 MHz is not fast enough for a 100 KHz I2C bus. I'm running my ATtiny2313 at 4 MHz.
Don |
|
|
| |
|
|
|
|
|
Posted: Jul 03, 2007 - 06:40 PM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
I have a debug routine that shows where any errors occur during communication, plus I have a logic capture device that we created to be able to see exactly what's going on during communication.
I can see that during a master write, everything goes as expected without a hitch. However, in a master read routine, everything does what I expect accept that the returned data is all zeros rather than the expected value of 0x0F that I've placed in the USIDR.
I have not yet tried using your version of the Slave driver, so I will attempt that today and get back to you.
BTW, I've got the tiny45 set at 20Mhz using the internal PLL clock. I do understand that the slave device has to be running at a minimum of 10 times the I2C bus, but even at the 20Mhz I still have not been able to get speeds of 100k on the bus. The best I can do and still get reliable results is ~75k. Good enough for now. |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Jul 03, 2007 - 10:34 PM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Ok, here's the status.
I've attempted communications using Don's code and still no success. I am no longer getting a start condition ack as I was with my old code.
So now I'm curious as to why I get a response and am able to at least send data using the old slave code (same master code), but not able to even get a start condition when using the new slave code.
I suppose there's a chance that something in the master code is wacky, is there any chance of seeing a sample of the master code you use to communicate with your slave device?
-Dan |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Jul 03, 2007 - 11:08 PM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
|
topazx2 wrote:
I am no longer getting a start condition ack as I was with my old code.
Are you using the AVR312 main.c? Are you including <avr/io.h> and <avr/interrupt.h> and did you change __enable_interrupt() to sei( );?
Also, if you're using the AVR312 main.c, I'd suggest removing the code that writes to DDRB and PORTB.
topazx2 wrote:
I suppose there's a chance that something in the master code is wacky, is there any chance of seeing a sample of the master code you use to communicate with your slave device?
Sure. I've added my master utility code here. I didn't include a main.c because it's somewhat involved. Basically, main needs to enable global interrupts via sei( ), call twiInit and call twiWrite.
I've started to put together a simple test case where the master does a slave write followed by a slave read. If you're still stuck, I'll continue working on it and see if I can figure out what's going on.
Don |
|
|
| |
|
|
|
|
|
Posted: Jul 04, 2007 - 12:46 AM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Don,
I was able to get my master code to send data to your slave code. I'm now working on getting a response which eludes me in both versions of the slave code.
I would certainly appreciate any time you're willing to devote to this, you may see something that I'm just not seeing.
BTW, Yes on all above cautions. I've attached a copy of the main.c code I've been working with (receives but doesn't reply)
-Dan |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Jul 04, 2007 - 04:08 AM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
|
topazx2 wrote:
I would certainly appreciate any time you're willing to devote to this, you may see something that I'm just not seeing.
I have the sample program working - using an ATmega88 as the master and an ATtiny2313 as a slave. I'm able to transmit data to the slave and read back the echoed data. But . . .
topazx2 wrote:
I've attached a copy of the main.c code I've been working with (receives but doesn't reply)
I see the following problem in main.c:
Code:
if(usiTwiDataInReceiveBuffer())
{
temp = usiTwiReceiveByte();
switch (temp)
{
case 1: led(LED1, 1); break;
case 2: led(LED2, 1); break;
case 3: led(LED3, 1); break;
usiTwiTransmitByte(distance);
}
// usiTwiTransmitByte(distance);
}
usiTwiTransmitByte will never be called. Your switch statement has 3-cases which all end in break and no default: case. The usiTwiTransmitByte within the switch statement is dead code; it'll never be executed. As a matter of fact, the switch statement does nothing.
Edit: my statement about the switch statement doing nothing is incorrect - the led( ) function is being called.
I've attached my working Slave main.c which echoes all received data (that is, if the master reads back as many bytes as are written within the limits of the Rx and Tx buffers).
Don |
|
|
| |
|
|
|
|
|
Posted: Jul 04, 2007 - 05:00 PM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Don,
Actually the code I sent was only depicting the working receive code, I don't use the LED switch commands in the code I'm trying to get to return data.
Sorry for the confusion I should have commented the usiTwiTransmitByte out before sending.
I am going to take a look at what you've posted right now and get back to you in a few. Thanks for taking the time and happy 4th!
-Dan |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Jul 04, 2007 - 11:39 PM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
|
topazx2 wrote:
I am going to take a look at what you've posted right now and get back to you in a few. Thanks for taking the time and happy 4th!
Not at all. Having struggled with I2C on numerous occasions, I'm always anxious to help.
Happy 4th to you too. Hope the weather is better there. We haven't had rain in what seems like forever and it's been raining all day with a high temperature of 62 degrees.
Don |
|
|
| |
|
|
|
|
|
Posted: Jul 11, 2007 - 09:48 PM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Well, thanks to Don Blake's efforts in translating ATMELs IAR version of the USI TWI code to a more useful AVR GCC version, I am happy to report that I can successfully transmit and receive from an ATmega88 to an ATtiny45 using I2C protocol. I plan to post a working version of the code once I clean it up a bit and document it all a little better.
Don, I never did get a chance to look at that last bit of code you sent since I got it all working right after writing you the last time. I have downloaded it for future reference though, thanks!
-Dan |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Jul 11, 2007 - 10:37 PM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Don,
I noticed that your location shows Apalachin, NY. That reminded me of something I saw recently.
Due to the new code of conduct dictated by politically correctness, those who live in the hills of Kentucky and Tennessee shall no longer be referred to as Hill Billy's, but shall now be known as Apalachin Americans.
-Dan |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Jul 12, 2007 - 03:46 AM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
|
topazx2 wrote:
Well, thanks to Don Blake's efforts in translating ATMELs IAR version of the USI TWI code to a more useful AVR GCC version, I am happy to report that I can successfully transmit and receive from an ATmega88 to an ATtiny45 using I2C protocol.
Dan, that's good news!
topazx2 wrote:
Due to the new code of conduct dictated by politically correctness, those who live in the hills of Kentucky and Tennessee shall no longer be referred to as Hill Billy's, but shall now be known as Apalachin Americans.
Cute. Actually, Apalachin (small town in Central New York) is pronounced differently than Appalachian (as in mountains of Eastern North America). However, having said that, I probably am a hillbilly.
Don |
|
|
| |
|
|
|
|
|
Posted: Aug 01, 2007 - 03:37 PM |
|

Joined: Nov 18, 2005
Posts: 89
|
|
Hi all,
I am trying to do the same thing as you, and the slavs code seems straight foreward, but how do i interface the slave code on the master side?
I2c master protocols typically start by sending an adress byte and then writing a data byte, starting and stopping etc.
Can someone share their master code that works with this slave code?
regards
Magnus |
|
|
| |
|
|
|
|
|
Posted: Aug 02, 2007 - 04:31 AM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
|
skogsvargen wrote:
Can someone share their master code that works with this slave code?
I posted my master utility code in this thread back on 03 July.
Don |
|
|
| |
|
|
|
|
|
Posted: Aug 02, 2007 - 07:41 AM |
|

Joined: Nov 18, 2005
Posts: 89
|
|
Don,
I found the master I2C library, but I really need some instructions how to use them together with the slave example that echoes bytes sent from the slave.
/Magnus |
|
|
| |
|
|
|
|
|
Posted: Aug 03, 2007 - 04:43 AM |
|


Joined: Jan 03, 2004
Posts: 1637
Location: Apalachin, NY, USA
|
|
|
skogsvargen wrote:
I found the master I2C library, but I really need some instructions how to use them together with the slave example that echoes bytes sent from the slave.
Ah, okay, here's an example master source file I've used. It writes the echoed data to an LCD if NOLCD is not defined.
You'll probably need to change the #undef NOLCD to #define NOLCD.
Don |
|
|
| |
|
|
|
|
|
Posted: Sep 25, 2007 - 06:02 PM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Ok, here's some sample code for using the ATtiny45 as a slave device. The slave code assumes you have an analog sensor of some sort connected to pin3 (PB4) of the tiny45.
The tiny45 is configured for analog reference of 2.5v and uses the USI as a TWI (I2C) interface.
The master device is an ATmega88 and assumes that you have a 16x2 LCD connected in four bit mode. The LCD provides error feedback if selected or prints the analog data if no errors occur.
The current slave address is 0x26. This can be changed, but requires changing in both the slave and the master.
The master code is pretty vague since my focus was on the slave side, but everything works.
-Dan Gates |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Sep 25, 2007 - 06:04 PM |
|

Joined: Sep 05, 2004
Posts: 61
Location: Oregon
|
|
Some of you may not be able to use an RAR compressed file, so here's the zipped version.
-Dan Gates |
_________________ There are 10 types of people in this world,
those who understand binary and those who do not.
|
| |
|
|
|
|
|
Posted: Dec 13, 2007 - 06:30 PM |
|

Joined: Dec 13, 2007
Posts: 10
|
|
Hi,
I was just going through your posts and need your suggestions and help for two wire interface using ATTiny24 and At24C01B.
In my case Attiny24 is Master and EEPROM(24C01) is slave.
I am using internal clock of 1MHz and generating the clock for TWI by software storbe.
I can generate the clock and also can send data if I avoid genrating ststrt conditon. I hv observed the clk and data on oscilloscope but I am not able to generate start condition. Also I dont get any acknowlegement from Slave. If I am on the verge to generate start condition my clock stops.
I am attaching the code here.
Please help.
Thanks,
Eshani
quote="donblake"]
skogsvargen wrote:
Can someone share their master code that works with this slave code?
I posted my master utility code in this thread back on 03 July.
Don[/quote] |
|
|
| |
|
|
|
|
|