0
\$\begingroup\$

I have thoroughly read the TWI part of ATmega data sheets, but I am still confused about a few things.

  1. If I write TWCR = (1 << TWINT);, will that write a zero to TWIE (and thereby disable TWI interrupts)? If I write TWCR = (1 << TWINT) | (1 << TWIE);, will that write a one to TWIE (and thereby reenable TWI interrupts)?

  2. In the most recent data sheet for the ATmega1284p for example, the SCL frequency is calculated as follows:

    \$ SCL = \frac{F\_CPU}{16\ +\ (2\ *\ TWBR\ *\ PreScaler)} \$

    However, on this document: http://www.atmel.com/Images/Atmel-2564-Using-the-TWI-Module-as-I2C-Master_ApplicationNote_AVR315.pdf the SCL frequency is calculated as follows:

    \$ SCL = \frac{F\_CPU}{16\ +\ (2\ *\ TWBR)\ -\ (4^{TWBR})} \$

    and I have even seen other versions on other websites. Has the formula changed? Which is the correct one?

  3. Is it safe to assume that the TWI hardware is the same for all microcontrollers in the ATmega series?
\$\endgroup\$
0
\$\begingroup\$
  1. No need to set the enable bit once set, unless for some reason you cleared it.

  2. The 2nd formula looks wrong. I would go with the data sheet.

  3. It seems atmel licenced NXP i2c module - the code examples are identical to those in lpc210x. But do check the data sheet to be sure.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the response. I am concerned about clearing the TWIE bit without explicitly setting it because you don't OR when you write to TWCR, you set it equal to something because TWINT won't be cleared otherwise. In other words, you don't write TWCR |= (1 << TWINT), you write TWCR = (1 << TWINT) and I think that might clear the TWIE bit as well. \$\endgroup\$ – HaLailah HaZeh Mar 6 '17 at 4:32
  • \$\begingroup\$ > because you don't OR when you write to TWCR... you don't OR or you cann't OR? TWINT is the flag bit. so you write '1' to it to clear it. TWIE is the interrupt bit. you write '1' to it to enable it. I guess I'm a little bit lost as to exactly what you are trying to ask. \$\endgroup\$ – dannyf Mar 6 '17 at 11:53
  • \$\begingroup\$ See here atmel.com/webdoc/AVRLibcReferenceManual/FAQ_1faq_intbits.html In short, you can't OR TWCR since it won't clear TWINT. My question is if explicitly clearing TWINT with an = will also clear TWIE. \$\endgroup\$ – HaLailah HaZeh Mar 7 '17 at 1:19
  • \$\begingroup\$ @HaLailahHaZeh You should go study bit masking. \$\endgroup\$ – Matt Young Mar 7 '17 at 2:18
0
\$\begingroup\$
  1. TWCR can be a confusing register, especially considering that TWINT is cleared by writing a one (not a zero) to it and that ORing (1 << TWINT) to TWCR will not clear TWINT. However, TWIE behaves like a normal bit and thus as expected, TWCR = (1 << TWINT); will clear both TWIE and TWINT and TWCR = (1 << TWINT) | (1 << TWIE); will clear TWINT and set TWIE.

  2. Given that all the recent data sheets have the first formula, I suspect that is the correct one.

  3. As dannyf pointed out, the TWI hardware is probably the same for all chips in the ATmega series.

\$\endgroup\$

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.