View previous topic :: View next topic |
Author |
Message |
Juergen
Joined: 13 Nov 2004 Posts: 85 Location: AZ, USA
|
Posted: Fri Jan 17, 2014 9:54 pm Post subject: Question to i2c_TWI-slave-acknack.LBX/i2c_TWI-slave.LBX |
|
|
I purchased the libraries as I want to design an I2C slave connection for an existing firmware which already runs perfect.
Unfortunately the manual which comes with the libraries is pretty sparse and the sample codes didn't get me any further either.
I have to write (master to my new slave) 1 or 2 or 4 bytes and provide back 1, 2, 4 bytes which the master has to read.
There are two variables TWI_btw (for received bytes) and TWI_btr (for send bytes) which look like they can be used to increment the data which I have in an array.
When (if at all) do these get reset to 0 (or 1)? Is it when the I2C receives a correct address? Does the channel stay open or only for the amount of bytes specified in the config BTR=x?
If I specify BTR=4 (4 bytes) in the config, is there a way to get this working if I only transfer 1 or 2 bytes or do I have to invent an “end transmission” signal?
Thanks a lot for any help you may be able to provide!
'address 70 , read 1 byte
Config Twislave = &H70 , BTR = 1 , BITRATE = 100000, GENCALL=1, USERACK=ack
'=======I2C slave sub-routines ================================
'the following labels are called from the library
Twi_stop_rstart_received:
'nothing to do here
Return
Twi_addressed_goread:
'nothing to do here
Return
Twi_addressed_gowrite:
'nothing to do here
Return
'this label is called when the master sends data and the slave has received the byte
'the variable TWI holds the received value
Twi_gotdata:
I2C-data(TWI_btw) = TWI
Return
'this label is called when the master receives data and needs a byte
'the variable twi_btr is a byte variable that holds the index of the needed byte
'so when sending multiple bytes from an array, twi_btr can be used for the index
Twi_master_needs_byte:
TWI = SensData(TWI_btr)
Return
'when the mast has all bytes received this label will be called
Twi_master_need_nomore_byte:
TWI_btr=0 '????????
Return
(BASCOM-AVR version : 2.0.7.6 ) _________________ Juergen
-- In a world without fences and walls, who needs Windows and Gates? -- |
|
Back to top |
|
|
MAK3
Joined: 24 Sep 2010 Posts: 449 Location: Germany
|
Posted: Sat Jan 18, 2014 10:41 pm Post subject: |
|
|
Hi Juergen,
see Bascom-Help under CONFIG TWISLAVE
http://avrhelp.mcselec.com/index.html?configtwislave.htm
USERACK
Default is OFF. When you use ON, an alternative library will be used.
This library will create a variable named TWI_ACK.
Each time your code is called this variable is filled with the value 255. If you do not alter the value, the slave will send an ACK as it is supposed to. If you reset the value to 0, the slave will send a NACK. You can use this to send data with variable length to the slave. In this case, BTR only serves as an index. You must make sure to reset TWI_ACK when you have send the last byte to the master.
MAK3 |
|
Back to top |
|
|
Juergen
Joined: 13 Nov 2004 Posts: 85 Location: AZ, USA
|
Posted: Sun Jan 19, 2014 12:20 am Post subject: |
|
|
Hi MAK3,
thank you and yes, I have seen this and made some progress with it. However, it still doesn't tell me how to handle the transmit (data slave to master) side. Guess the master simply has to know what length of data to read as it has to provide the clock for it. I just made a small program to just receive data and then read the same back via I2C. Hope this works. The open question is still whether teh TWI_btw and TWI_btr are indexed starting at 0 or 1 which has impact on teh array dimensioning.
Juergen _________________ Juergen
-- In a world without fences and walls, who needs Windows and Gates? -- |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Jan 20, 2014 10:06 pm Post subject: |
|
|
TWI_BTW and TWI_BTR are reset to 0 when the slave is addressed. They are increased by 1 for each byte to send/receive. The base is always 1. _________________ Mark |
|
Back to top |
|
|
|