ATmega168 TWI (2-wire) TWSR status codes

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ducttapepro6
 
Posts: 4
Joined: Fri Aug 08, 2008 6:42 pm

ATmega168 TWI (2-wire) TWSR status codes

Post by ducttapepro6 »

I have:
(1) ATmega168
(1) TC74 - Digital Temperature sensor.

Using the 2-wire module within the ATMega168 here is my code, straight out of the datasheet:
*************

Code: Select all

// talk to temp sensor. prep to send the temp back.
      TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);            // send a start condition
      while (!(TWCR & (1<<TWINT)));                          // Wait for "start sent"
      TWDR = (sensor_address <<1);                           // Sensor Address, Write
      TWCR = (1<<TWINT) | (1<<TWEN);                        // Start transfer. Send TWDR out.
      while (!(TWCR & (1<<TWINT)));                          // Wait for "TWDR sent"
      printf("TWSR should be 0x18 - %x \n", (TWSR &  0xF8));  // THIS CHECKS OUT OK !!!
      TWDR = 0x00;                                            // Address "temperature reading" byte
      TWCR = (1<<TWINT) | (1<<TWEN);                       // Start transfer. Send 0x00.
      while (!(TWCR & (1<<TWINT)));                         // wait for "Data sent"
      printf("TWSR should be 0x28 - %x \n", (TWSR &  0xF8));  // THIS PRINTS '40' every time !!!!
****************

I have found that I can't manage to get the correct TWSR code (0x28) after sending the "0x00" byte - I get the code 0x40; meaning " Slave+R sent, ACK received". I get the correct codes for all other 2-wire exchanges with the temperature sensor.

In the end, the TWSR code for the exchange in question doesn't seem to matter - I am still able to read the temperature byte correctly!

The reason for my desire to get the TWSR code correct is that my next venture is to try a wii nunchuck that I have... It is not going so well & I get the same TWSR errors at the exact same point in the conversation.

Thanks,

User avatar
ran talbott
 
Posts: 57
Joined: Thu Feb 19, 2009 1:32 pm

Re: ATmega168 TWI (2-wire) TWSR status codes

Post by ran talbott »

I don't have anything specific for you, because I'm still new to I2C on the Arduino, but I've seen a few posts by people who've gotten Wii nunchuks working over on the forums at arduino.cc, so there might be some answers for you there.

Ran

User avatar
ducttapepro6
 
Posts: 4
Joined: Fri Aug 08, 2008 6:42 pm

Re: ATmega168 TWI (2-wire) TWSR status codes

Post by ducttapepro6 »

I'm still most interested in getting the correct TWSR codes.

Right now (while working with both digital temperature sensor or the nunchuck) I'm getting a Master Transmission (MT) code after sending the slave address+ write (a correct result) followed immediately by a Master Reciever (MR) code for sending the slave a register to look in (not what I'm expecting to get).

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”