View previous topic :: View next topic |
Author |
Message |
AIVA
Joined: 15 Nov 2008 Posts: 18
|
Posted: Mon Jan 05, 2009 3:50 pm Post subject: TWI slave problems |
|
|
Hello
I am try to use TWI slave library with 2 MEGA128 and have found some problem
1) I send from Master device to Slave 23 byte that arrived right on Slave but if
insert instruction to read some pins (a1=pintb.2 and a2=pinnb.3) while the I2C transmit,
I receive the 23 byte but random appear strange value
2) when I try to read some byte from slave device I receive the byte but the cycle on Slave have a fault.
In the cycle Case -- End select, the Variable of Case don't recognize the right position
3) I use graphic display 128*64 and if receive TWI data and display some message at same time the dislay
show bad character
Now I have fix the problem insert same flag but is not a right solution
Someone has found the same problems ?
Thanks |
|
Back to top |
|
|
Matrixx
Joined: 30 Nov 2005 Posts: 158
|
Posted: Tue Jan 06, 2009 4:03 pm Post subject: |
|
|
It will be good to show some part of you code to better understand the fault, maybe the problem is on the sequence of the operations.
I'm using M128 as master and a M32 as slave but in the design process I had to deal with timing to allow the slave do the stuff without affect the I2C operation. Basically you have to do the I/O and other operations outside the TWI interrupt subs. When in the TWI interrupt, only read the vars.
It works very fine for me. |
|
Back to top |
|
|
AIVA
Joined: 15 Nov 2008 Posts: 18
|
Posted: Tue Jan 06, 2009 6:31 pm Post subject: |
|
|
Hello Matrixx
Thanks for your answer
program:
do
"RX data from external device with RS485"
if F1=0 and F2=0 then "show data on graphic display"
"when Master call with TWI, read data from SLAVE"
loop
Now I look to:
Twi_addressed_gowrite:
f1=1
Twi_addressed_goread:
f2=1
but with flag to stop the operation on slave some time i have a error,
as if i use lcd and at same time arrive twi data
I am in progress to insert a pin between Master and Slave to stop operation on slave device before use TWI
On Master device, before call twi subroutine i insert a timer to leave time at slave to stop the operation
Do you have a idea about minimum value of this timer ?
(I don't know the time that display need to show a character)
I think to insert 10 msec |
|
Back to top |
|
|
Matrixx
Joined: 30 Nov 2005 Posts: 158
|
Posted: Tue Jan 06, 2009 8:20 pm Post subject: |
|
|
As I understand your main process is:
Code: |
Do
Read RS485
Maybe do some process before....
Update LCD
Loop
|
Of course, all this while listening to the TWI.
Twi_addressed_goread and Twi_addressed_gowrite are handled in "background" for you. Actually I have no code inside this subs. I think you must concentrate on the Twi_gotdata sub.
My recommendation is that you elaborate a set of commands in the master, then send it to the slave. The slave is doing the main loop but also listen for TWI, it receives the command and depending on the request do the pertinent action. If desired, return a value to the master.
Your main slave loop will looks like:
Code: |
Dim RxFlag as bit '0 = no data, 1 = TWI received a command
Do
If RxFlag = 1 then
Decode the comand
Choose what to do
Maybe send back data
RxFlag=0 'reset flag
End if
'continue with the usual stuff...
Read RS485
Maybe do some process before....
Update LCD
Loop
|
And the sub at the same salve will looks like:
Code: |
Twi_gotdata:
'here the master send data, Twi_btw is a var which count the number of bytes
'received
'TWI is the var which stock the actual byte
'We need to make an array to collect data. We are here each time we receive a byte
Rx(twi_btw) = Twi
If Twi_btw = 7 Then
Rxflag = 1
End If
Return
|
In this exmple of my actually working code, I designed the slave to receive 7 bytes from the master. At the 7th byte received I set the RxFlag so the main loop can decode the command.
by example, my commands are to program the salve to read a set of 4 tpms sensors via RS232 so I have to instruct the salve what ID's need to read and ignore the rest, and what pressure to watch. So I send a command of 7 bytes to the slave where the byte 1 is the command itself, the 4 next bytes are the hex Id number, the 6th byte is the pressure to watch and the 7th byte is the checksum. I expect the slave to respond with 11 bytes.
The slave in this example is an M32 configured as:
Code: |
'-------I2C config ------------------------------------------------------
'$lib "i2c_twi.lbx"
Config Sda = Portc.1 ' I2C Data.
Config Scl = Portc.0 ' I2C Clock.
Config Twislave = &H70 , Btr = 7 , Bitrate = 400000
I2cinit
'this array holds the bytes that form the command:
Dim Rx(7) As Byte
|
|
|
Back to top |
|
|
AIVA
Joined: 15 Nov 2008 Posts: 18
|
Posted: Wed Jan 07, 2009 10:34 pm Post subject: |
|
|
Hello Matrixx
In have fix the problem
I have connect 2 pins on master and slave
before send twi data the master set the pin on slave and the slave stop
the job and wait twi data
After that slave receive data restart his job
Thanks for your help |
|
Back to top |
|
|
Matrixx
Joined: 30 Nov 2005 Posts: 158
|
Posted: Wed Jan 07, 2009 11:34 pm Post subject: |
|
|
Yes, that is another way to do it. I have also a pin shared in other application, not to stop slave but for indicate master that slave is ready to be polled (because slave refresh his data every 200mS).
I think to have a shared pin to stop the slave job would not be neccesary, but while it works, it's ok |
|
Back to top |
|
|
charlesk
Joined: 11 Nov 2005 Posts: 3
|
Posted: Sat Jan 24, 2009 1:26 pm Post subject: TWI Communication |
|
|
I have a project where a Mega644 TWI master communicates with five Mega168 TWI slaves. What I have found is that the communication is dependent on the compiler version. With Bascom 1.11.8.9 it works, fails when I compile with 1.11.9.0 and is OK with 1.11.9.1. But it fails again using 1.11.9.2 and 1.11.9.3. Sending data from master to slaves works OK in all versions but to get data from slaves I poll the slave '168s from the '644 and read byte(s) back. When I send these bytes out to an RS232 debug port, I get garbage using the compilers that don't work. This is absolutely repeatable on a number of different PCs.
Has anyone else come across this problem? |
|
Back to top |
|
|
|