Settings meaning for TWI device

Go To Last Post
67 posts / 0 new

Pages

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

GermanFranz wrote:

Moe123 wrote:

 How can he use that code ????!! that code does a completly different task.

 

 

Sure? It's same TWI Mr.Moe123 wink

 

Diefferent "task", its not just about how to initialize TWI and thats it....

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

Moe123 wrote:
Diefferent "task", its not just about how to initialize TWI and thats it....

 

Well Mr. Moe123,

first, this helpful instructive code does not just show the initialization and second, it would be nice if you could offer specific help on the subject here.

Your objections, however, do not make much hope , or?

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

Herr Franz,

 

Please read again what I wrote.

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

Nahhh... It's not working.

 

I0ve changed a few things but I can't see on scope what I was expecting.

 

I haven't seen that code yet. But to go through it I need also to take a look at that chip.

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

PsySc0rpi0n wrote:
I haven't seen that code yet. But to go through it I need also to take a look at that chip.

Once again: TWI its the same! Completely. If this C-Code is working on ATTiny402 and an ATTiny416 you can use same techniques, same code for read/write access using Mega4808.

Moe123 wrote:
Please read again what I wrote

Please read again what is "task" of this thread.

Last Edited: Sat. Apr 20, 2019 - 03:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GermanFranz wrote:
PsySc0rpi0n wrote:
I haven't seen that code yet. But to go through it I need also to take a look at that chip.
Once again: TWI its the same! Completely. If this C-Code is working on ATTiny402 and an ATTiny416 you can use same techniques, same code for read/write access using Mega4808.

 

Ok, I guess I'll have to give it a try!

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

Hello.

 

Finally back to my TWI thing.

 

I've been looking to the code in those 2 files.

I have some questions because I'm not sure if things are the way I'm thinking they are!

 

I'll go through each code regarding each question.

 

In the following code:

char TWI_Start(char addr){
	if ((TWI0.MSTATUS & TWI_BUSSTATE_gm) != TWI_BUSSTATE_BUSY_gc){
		TWI0.MCTRLB &= ~(1<<TWI_ACKACT_bp);
		TWI0.MADDR = addr;

		if (addr & 1){
			while (!(TWI0_MSTATUS & TWI_RIF_bm))
				/*wait*/;
		}
		else{
			while (!(TWI0_MSTATUS & TWI_WIF_bm))
				/*wait*/;
		}
		return TWI0.MSTATUS;
	}
	else
		return TWI0.MSTATUS;
}

First question is why there is this TWI_Start() function and then one other for the Read operation and another for the write operation? I had created only the TWI_Init() and then the TWI_Read() functions. Is this "Start" function mandatory?

Then, I don't understand why there is an ACK signal being sent if the BUSSTATE is different from BUSY... Can you please explain me why?

Then, I don't understand why there is this IF with "addr & 1 ". What is the purpose of this? If this is to set a READ/WRITE operation, then, why are there specific functions for the same Read/Write operations?

Also, RIF and WIF, according to the User Manual, says that these bits are set when a Read or Write operations are successfully completed. But this function has no code to read or write anything from/for the BUS. So why is this code inside this function? Or am I getting all wrong?

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

PsySc0rpi0n wrote:
First question is why there is this TWI_Start() function and then one other for the Read operation and another for the write operation? I had created only the TWI_Init() and then the TWI_Read() functions. Is this "Start" function mandatory?
I may have suggested this before but have you looked at Fleury? Not necessarily the nitty gritty details of the implementation but simply the highlevel API he provides - he has a complete/minimal implementation of everything a normal use of TWI will require:

 

http://homepage.hispeed.ch/peterfleury/doxygen/avr-gcc-libraries/group__pfleury__ic2master.html#func-members

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

clawson wrote:

PsySc0rpi0n wrote:
First question is why there is this TWI_Start() function and then one other for the Read operation and another for the write operation? I had created only the TWI_Init() and then the TWI_Read() functions. Is this "Start" function mandatory?
I may have suggested this before but have you looked at Fleury? Not necessarily the nitty gritty details of the implementation but simply the highlevel API he provides - he has a complete/minimal implementation of everything a normal use of TWI will require:

 

http://homepage.hispeed.ch/peterfleury/doxygen/avr-gcc-libraries/group__pfleury__ic2master.html#func-members

 

Yes, you have already suggested to study Fleury's code and try to start from there, but at this point, I want to try the other suggested code! I was just trying to understand how that code works so that I can use it properly!

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

I kind of took some parts of that code and I can now see e few more things on my scope but I still think it's not correct what scope is showing!

 

My actual code:

https://ideone.com/4GVVf4

 

This is what my scope shows now:

 

Looks like the only correct thing is the address which is 0x18. In this code I was trying to read 5 times and average the value. But I'm not even sure if what I see here represents 5 reads and even if it does (I think there are 6 reads instead), why the sensor is telling 01, FF, FF, FF, FF? This doesn't looks correct at all. The 0x1D could be eventually correct but not all the others!

Last Edited: Fri. Apr 26, 2019 - 10:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seems to me you have too many unknowns.

 

You probably want to start out with well known, totally understood I2C device - perhaps an RTC from the DS13xx range? Maybe an EEPROM? Use that to get your I2C stack to the point where it's fully implemented, tested and understood and does what you want. Then when you connect to this new "unknown device" any problems you hit must be with that device, not with the I2C implementation.

 

Otherwise you just don't know.

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

PsySc0rpi0n wrote:
why the sensor is telling 01, FF, FF, FF, FF? This doesn't looks correct at all

What should the sensor provide with these data?

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

GermanFranz wrote:
PsySc0rpi0n wrote:
why the sensor is telling 01, FF, FF, FF, FF? This doesn't looks correct at all
What should the sensor provide with these data?

 

Just temperature values. The first byte that is read, should not be 0 either!

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

Where do you set the address to read from? For that matter, what is the slave device? There is no mention of it in your code.

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

Kartman wrote:
Where do you set the address to read from? For that matter, what is the slave device? There is no mention of it in your code.

 

Line 108 of the code. It's in TWI_Read() function! Address is 0x18!

The Slave device is a temperature sensor MCP9808.

Last Edited: Mon. Apr 29, 2019 - 08:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I made this work with the other guy code and a couple of changes!

Pages