CONFIG TWISLAVE
(Created page with "= <span class="f_Header">Action</span> = Configure the TWI Slave address and bit rate <span style="font-family: Arial;"> </span> <span style="font-family: Arial;">&nbs...")
Newer edit →
Revision as of 13:54, 8 February 2013
Contents |
Action
Configure the TWI Slave address and bit rate
Syntax
CONFIG TWISLAVE = address , BTR = value , BITRATE = value , SAVE=option [,GENCALL=value] [,USERACK=ack]
(I2C TWI Slave is part of the I2C-Slave library. This is an add-on library that is not included in Bascom-AVR by default. It is a commercial add on library. It is available from MCS Electronics )
See also: I2C TWI Slave, USING I2C Protocol, Using USI, CONFIG I2CSLAVE
Remarks
Address |
The slave address that is assigned to the slave chip. This must be an Even number. Bit 0 of the address is used to activate the general call address. The GENCAL option will set this bit automatic.
I2C uses a 7 bit address from bit 1 to bit 7. Bit 0 is used to specify a read/write operation. In BASCOM the byte transmission address is used for I2C. This means that an I2C 7-bit address of 1 becomes &B10 = 2. And we say the address is 2. This is done so you can copy the address from the data sheets which are in the same format in most cases. So if you work with 7 bit address, you need to multiply the address by 2. |
BTR |
Bytes to receive. With this constant you specify how many bytes will be expected when the master reads data from the slave. And thus how many bytes will be sent to the master. |
Bit rate |
This is the I2C/TWI clock frequency. Most chips support 400 KHz (400000) but all I2C chips support 100000. |
SAVE |
SAVE = NOSAVE : this can be used when you do not change a lot of registers in the interrupt. SAVE = SAVE : this is best to be used when you do not use ASM in the TWI interrupt. See the explanation below. When you do not specify SAVE, the default will be SAVE=SAVE. |
GENCALL |
General call address activated or not. When you specify 1 or YES, the General call address will be activated which mean that the slave will respond not only to it's own address, but also to the general call address 0. When you omit the option or specify 0 or NO, the general call address will not be honored. |
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. |
The variables Twi , Twi_btr and Twi_btw are created by the compiler. These are all bytes
The TWI interrupt is enabled but you need to enabled the global interrupt
The TWI Slave code is running as an interrupt process. Each time there is a TWI interrupt some slave code is executed. Your BASIC code is called from the low level slave code under a number of events. You must include all these labels in your Slave application. You do not need to write code in all these sub routines.
Label |
Event |
Twi_stop_rstart_received |
The Master sent a stop(i2CSTOP) or repeated start. Typical you do not need to do anything here. |
Twi_addressed_goread |
The master has addressed the slave and will now continue to send data to the slave. You do not need to take action here. |
Twi_addressed_gowrite |
The master has addressed the slave and will now continue to receive data from the slave. You do not need to take action here. |
Twi_gotdata |
The master has sent data. The variable TWI holds the received value. The byte TWI_BTW is an index that holds the value of the number of received bytes. The first received byte will have an index value of 1. |
Twi_master_needs_byte |
The master reads from the slave and needs a value. The variable TWI_BTR can be inspected to see which index byte was needed. With the CONFIG BTR, you specify how many bytes the master will read. |
In most cases your main application is just an empty DO LOOP. But when you write a slave that performs other tasks on the background these other tasks are interrupted by the TWI traffic.
Take in mind that the interrupt with the lowest address has the highest priority.
So do NOT write blocking code inside an interrupt. While servicing another interrupt, the TWI interrupt can not be serviced.
The TWI Slave code will save all used registers.
But since it will call your BASIC application when the TWI interrupt occurs, your BASIC code could be in the middle of say a PRINT statement.
When you then execute another PRINT statement , you will destroy registers.
So keep the code in the sub routines to a minimum, and use SAVE option to save all registers. This is the default.
While two printing commands will give odd results (print 12345 and 456 in the middle of the first print will give 1234545) at least no register is destroyed.
A typical configuration is shown below.
PIC
To test the above hardware, use the samples : twi-master.bas and twi-slave.bas
Optional you can use i2cscan.bas to test the general call address.
When you want to change the address of the slave at run time you need to write to the TWAR register.
The TWAR register contains the slave address. Bit 0 which is used to indicate a read or write transaction should be cleared. When you set it, the slave will also recognize the general call address. The GENCALL option just sets bit 0 of the slave.
See also
CONFIG TWI , CONFIG SCL , CONFIG SDA , I2C TWI Slave
ASM
NONE