NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » TWI hang up

October 10, 2011
by esoderberg
esoderberg's Avatar


I have a program using a version of your TWI code communicating at 400kHz between two 328p (one as dedicated Master) as well as with a L3G4200D gyro slave device on the same bus. All is well and good, runs very reliably. I also have a 328p communicating with an AM4096 magnetic encoder slave device. All is well there too. However, when I put all of them together on the same bus and try to communicate with them within the same program using the same supporting functions, the code hangs almost immediately. Tried reducing bit rate and tweaking pull up resistors to no avail. All the SCL/SDA lines are relatively short. Some times it seems to hang on the

 while(TWCR & (1<<TWSTO));

line in the TWI interrupt function, but even if this line is removed the code still hangs with all three slaves(and still runs fine until I add the AM4096 into the mix with the 328 and gyro).
Any pointers on what I might want to troubleshoot next to get all three slaves to play nicely together in the same program and same bus?


October 12, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi esoderberg,

Sounds like you covered most of the possibilities here, so I'm guessing there is some subtle bug in the integration of the code. I believe the TWI interface is an addressable interface, could it be that the addresses for your devices are colliding, and causing all the devices to answer at the same time?

There could be a weird interaction happening with the interrupt handler code. Are you trying to print stuff out inside your interrupt handlers? This will often cause the handlers to run too slow for the devices. Did you by any chance make your interrupts interrupt-able? This could cause you to run out of memory fairly quick if you keep interrupting interrupts.


October 31, 2011
by esoderberg
esoderberg's Avatar


I guess I'm conditioned by the myriad of mistakes I make along the way for most of my little projects to assume that any IC I'm working with is OK and if something is not working it must be a wiring/circuit mistake or a programming error. However, in this case, after much troubleshooting and gnashing of teeth, it turns out the sample IC I got was the wrong one. After describing the problem to RLS, they sent me another sample and everything is now running exactly as it's supposed to with all components on the TWI bus together.


November 01, 2011
by Ralphxyz
Ralphxyz's Avatar

Thanks for posting that mishap Eric, that is a really important lesson!


Post a Reply

Please log in to post a reply.

Did you know that a thermometer can be made "faster" by using a bit of math? Learn more...