Posted by GermanFranz: Thu. Apr 18, 2019 - 10:29 AM
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?
Posted by GermanFranz: Sat. Apr 20, 2019 - 02:02 PM
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.
Posted by PsySc0rpi0n: Sat. Apr 20, 2019 - 03:16 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.
Posted by PsySc0rpi0n: Tue. Apr 23, 2019 - 06:46 PM
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?
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:
Posted by PsySc0rpi0n: Wed. Apr 24, 2019 - 06:22 PM
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:
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!
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!
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.
Diefferent "task", its not just about how to initialize TWI and thats it....
- Log in or register to post comments
TopWell 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?
- Log in or register to post comments
TopHerr Franz,
Please read again what I wrote.
- Log in or register to post comments
TopNahhh... 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.
- Log in or register to post comments
TopOnce 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.
Please read again what is "task" of this thread.
- Log in or register to post comments
TopOk, I guess I'll have to give it a try!
- Log in or register to post comments
TopHello.
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:
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?
- Log in or register to post comments
Tophttp://homepage.hispeed.ch/peterfleury/doxygen/avr-gcc-libraries/group__pfleury__ic2master.html#func-members
- Log in or register to post comments
TopYes, 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!
- Log in or register to post comments
TopI 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!
- Log in or register to post comments
TopSeems 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.
- Log in or register to post comments
TopWhat should the sensor provide with these data?
- Log in or register to post comments
TopJust temperature values. The first byte that is read, should not be 0 either!
- Log in or register to post comments
TopWhere 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.
- Log in or register to post comments
TopLine 108 of the code. It's in TWI_Read() function! Address is 0x18!
The Slave device is a temperature sensor MCP9808.
- Log in or register to post comments
TopI made this work with the other guy code and a couple of changes!
- Log in or register to post comments
TopHi, would you be able to post the working code? Thanks
Pete
- Log in or register to post comments
TopPages