twint twi

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

Hi guys,
i'm a little bit lost in a datasheet. By

(1 << TWINT)

do i clear or set the flag?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You clear the flag by WRITING a 1 to it. What you are showing is out of context so we don't really know what that bit does.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you write, it clears the flag.
If you read, it returns the value of the flag.

In other words, it works the same as most other interrupt flag bits. However you seldom notice with the INTF0 bit because the ISR() clears it automatically. (you seldom use INTF0 without an ISR())

The TWINT bit always needs clearing manually.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

morning

so if i take that setting bit (1 << TWINT) clears the TWINT flag then why (from the datasheet)

while (!(TWCR & (1<<TWINT)));

means waiting for TWINT flag set? Shouldn't it be waiting for TWINT cleared?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, you typically have:

    TWCR = (1<<TWINT)|(1<<TWSTA);   // clear flag, issue start command.
    ...
    while (!(TWCR & (1<<TWINT)));   // wait for interrupt flag
    ... 

You could equally well do this in stages.

    TWCR = (1<<TWINT);   // clear interrupt flag 
    ...
    TWCR = (1<<TWSTA);   // issue start command.
    ...
    // wait for START command to complete:
    while (!(TWCR & (1<<TWINT)));   // wait for interrupt flag to set
    TWCR = (1<<TWINT);   // clear interrupt flag 
    ... 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David

I think i do understand the way it works, you clear the flag and wait the process to finish and the hardware to set the flag, just can't get the logic of line

 while (!(TWCR & (1<<TWINT)));

The cycle goes on while TWCR bit 7 is 0 (flag set), but it should bit 1 by now (we cleared it at the start of the operation). By that line it looks like hardware sets the flag by setting bit to 1(???).
And by the way david, did you do that intentionally

    TWCR = (1<<TWINT);   // clear interrupt flag
    ...
    TWCR = (1<<TWSTA);   // issue start command.

not the

    TWCR = (1<<TWINT);   // clear interrupt flag
    ...
    TWCR |= (1<<TWSTA);   // issue start command.

?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are going to want to make it:

TWCR |= (1<<TWINT);
..
TWCR |= (1<<TWSTA);

or

TWCR |= (1<<TWINT);
..
TWCR = (1<<TWSTA) | (1 << TWEN);

or some variation of that, other wise you clear TWEN and shut off the TWI interface.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks martin, i was just looking for clues everywhere, thought maybe david intentionally wrote it this way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry. I just typed instead of pasting from existing code.

Yes, the 'command' will have TWEN set.
No, I avoid using |= or &= unless it is strictly necessary. e.g.

unsigned char i2c_write(unsigned char ch)
{
	volatile unsigned char twst;
	TWDR = ch;
	TWCR = (1<<TWINT) | (1<<TWEN);
	while ((TWCR & (1<<TWINT)) == 0) ;
	switch (twst = TW_STATUS) {
		case TW_MT_SLA_ACK:
		case TW_MT_DATA_ACK:
		case TW_MR_SLA_ACK:
		case TW_MR_DATA_ACK:
			return twst;
	}
	return 0;
}

I agree that the 'write 1 to clear interrupt flag' seems strange.
OTOH, I find the 8051 approach of 'write 0 to clear flag' strange too.

Incidentally, the INTFn flags are conveniently in a 'flags-only' SFR. When you get TWINT and TWEN in the same SFR it is awkward. Likewise with the USI in a Tiny.
Other peripherals like SPI clear their SPIF bit by reading SPSR rather than writing. But the WCOL bit needs reading SPDR.

Yes. Other makers' MCUs seem to have anomalies too!
It is just something that you have to get used to.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Whenever the TWINT flag is set to 1 by the hardware itself(upon completion of some pre-defined processes),an interrupt is SET.

Whenever the TWINT flag is set to 1 by the application software(as in ,us,the programmers),the already set interrupt(if any) is cleared.

It's strange that 1 is used for both setting and clearing the interrupt flag.The microcontroller uses an uncanny way of distinguishing between the hardware setting and the software setting of the flag bits.
Yes,we have to get used to it! :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you resurrecting an almost 2-year-old thread?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your problem being?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AdiAVRookie wrote:
Your problem being?

There is no problem with resurrecting an old thread.
However, please do not write incorrect explanations. i.e. please read the data sheet for yourself.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey David! I'm sorry for forgetting to mention that explanation was based on an assumption. Can you point out where I have gone wrong with my explanation?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey I am writing 

TWCR = (1<<TWINT) | (1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0) ;

but still TWINT is not clearing. i.e. my program hangs at  the while condition written above.

 please help!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Download a respected library and use it.

 

If you are determined to re-invent the wheel,   you will be able to compare the performance of your square wheel with the round one (e.g. Fleury "i2cmaster.h" library)

i.e. you will see where you have gone wrong.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nisargmehta wrote:
i.e. my program hangs at  the while condition written above.
This expression:

((TWCR & (1<<TWINT)) == 0)

... is true only if TWINT is clear.

 

Your program is 'hanging' at the while because TWINT has been cleared.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Mon. Jan 5, 2015 - 08:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But TWINT=1 means Cleared & TWINT = 0 means Set

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nisargmehta wrote:

But TWINT=1 means Cleared & TWINT = 0 means Set

 

Oh no,  it doesn't.

 

Read your data sheet.  Search for "TWINT"

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nisargmehta wrote:
But TWINT=1 means Cleared & TWINT = 0 means Set
No it does not.

 

You are confusing the means of clearing the flag with the state of the flag.

 

It is true that you must write '1' to that bit to clear it.  However if the flag bit reads as '0', the flag is clear.  If the flag bit reads as '1', the flag is set.

 

What does the datasheet say about TWINT?:

 

 

This has been discussed many times in the past:

https://www.google.ca/search?q=why+write+1+to+clear+flag+site%3Aavrfreaks.net

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Mon. Jan 5, 2015 - 05:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can any one explain statement written below with proper example...

while (!(TWCR & (1<<TWINT)));

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Congratulations on finding the appropriate thread.
I suspect that you have the same question every year in your University.
.
Believe me. The answers are all here. (and in the data sheet).
You have to read the replies several times. It is NOT obvious.
.
With any microcontroller you:
1. start a peripheral operation.
2. When completed, an interrupt flag is set.
3. You service the interrupt. And clear the flag ready for next operation.
4. You do not have to use an ISR(). You can just poll the flag manually.
.
David.

Last Edited: Wed. Jun 15, 2016 - 04:14 PM