TWI I2C while (!(TWCR & (1<<TWINT))); never become 1 "ATmega328p"

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ATmega328p TWI

The problem is the program stuck in the while loop some of reason

I added a green LED to the project to found the problem

The green LED turns on and stays on forever at "while (!(TWCR & (1<<TWINT)));" this point

Does anyone have any idea why the TWINT newer become 1?

//ATmega328p
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
	TWBR = 2;			//Bitrate 2 at 1MHz gives 50KHz
	DDRD = 0b00001100;
	PORTD = 0b00000000;

	while (1)
	{
		_delay_ms(1000);

		//wake the sensor
		PORTD |= 1<<PIND2;				// Progress green LED on
		TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);	// Send Start
		while (!(TWCR & (1<<TWINT)));			// Wait for TWINT set to 1
		PORTD &= (~1<<PIND2);				// Progress green LED off
		if ((TWSR & 0xF8) != 0x08)	PORTD |= 1<<PIND3;	// Turn on red LED if there's an error

		TWDR = 0xB8;
		TWCR = (1<<TWINT) | (1<<TWEN);	//start transmission
		while (!(TWCR & (1<<TWINT)));
		if ((TWSR & 0xF8) != 0x18)	PORTD |= 1<<PIND3;

		TWDR = 0x00;
		TWCR = (1<<TWINT) | (1<<TWEN);	//start transmission
		while (!(TWCR & (1<<TWINT)));
		if ((TWSR & 0xF8) != 0x28)	PORTD |= 1<<PIND3;

		TWCR = (1<<TWINT)|(1<<TWEN) |(1<<TWSTO); // Stop

		_delay_ms(1000);
		PORTD = 0b00000000;
	}
}

 

Last Edited: Sun. Jul 29, 2018 - 05:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

Topic locked