1

I found this useful website explaining for to configure and implement the TWI for mega328p, but what I found not so obvious is that in the website they set TWEN in every function, while in the datasheet C code example, they only set it in START and STOP functions.

I also find another style in Arduino Wire library, that they implement more easier functions for starting and ending the module.

This is the website, could anyone take a look and tell me if setting TWEN in each function is necessary?

Also, is it necessary to check for TWINT after sending STOP command?

http://www.embedds.com/programming-avr-i2c-interface/

Regards,

1

The big difference is interrupt driven or busy wait implementation of the TWI hardware module protocol. Enabling interrupts is necessary in an interrupt driven implementation while busy wait should not enable interrupts.

A busy wait implementation is much simpler but it cannot be asynchronous. An interrupt driven implementation can be asynchronous and even power down the rest of the MCU during the TWI communication. It should be noted that the Arduino Wire class is interrupt driven but synchronous.

Cheers!

  • OK so the interrupt driven is when I enable TWIE with the GIE. And busy wait is with disabling TWIE. So my question now is when TWINT is 0? I see it 1 all the time in the 328p datasheet. And it's mentioned that you have to wait until the bit is 0. And if you want to clear it then write 1 to this bit. So what's the value of TWINT when it's set or cleared? Regards, – R1S8K May 1 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.