Sunteți pe pagina 1din 26

Using I2C/TWI with BASCOM-AVR

Tested with BASCOM-AVR Version 2.0.2.0 December 2010 Version 0.5

1. 2. 3. 4. 5. 6. 7. 8. 9.

What you need to get started I2c/TWI Addressing Hardware connection Why 2 different Slave Libraries ? Config I2CISLAVE Config TWISLAVE I2C/TWI MASTER You have different Voltages like 5V and 3.3V on Slave Side You have long cable distance and several Slaves 10. I2c with ATXMEGA Family

What you need to get started


The commercial I2CSLAVE Library from MCS http://www.mcselec.com BASCOM-AVR FULL VERSION already installed Copy following files from the I2CSLAVE ZIP-File to Bascom-AVR LIB folder (e.g. C:\.....\BASCOMAVR\LIB):
i2c_TWI-slave-acknack.LBX (is the latest available Library) and is used when under CONFIG TWISLAVE the parameter USERACK = On is used i2c_TWI-slave.LBX and is used when under CONFIG TWISLAVE the parameter USERACK = OFF is used (This is also the DEFAULT VALUE) i2cslave.LBX (is the latest available Library)

I2c/TWI Addressing
7-Bit Addressing

A7 | A6 | A5 | A4 | A3 | A2 | A1 | R/W
Example (WRITE Address = &B1011000 = &HB0) MASTER = TRASMITTER and SLAVE = RECEIVER

1 |0 |1 | 1 | 0 |0| 0|0
Example (READ Address = &B1011001 = &HB1) MASTER = RECEIVER and SLAVE = TRANSMITTER

1 |0 |1 | 1 | 0 |0| 0|1

I2c/TWI Addressing
General Call Address = &B00000000 = &H00 Most of the Slaves have Address Inputs (for example A1, A2 and A3) which can be set by jumper or hardwired. So there are 112 Addresses possible (without the reserved addresses like General Call Address)

Hardware connection Master Slave including Pullup Resistors (4.7K or 10K)

Also connect GND of Master and Slave

MASTER

SLAVE

Why 2 different Slave Libraries ?


Because there are AVR Chips with hardware TWI/I2C interface (e.g. ATMEGA8 or ATMEGA128) and AVR Chips which have no hardware TWI/I2C interface like ATTINY2313 or ATTINY13) AVRs with hardware TWI/I2C (ATMEGA8 or ATMEGA128.) AVRs which have no hardware TWI/I2C (ATTINY2313 or ATTINY13)

Used Library = i2c_TWI-slave-acknack.LBX (USERACK = ON) i2c_TWI-slave.LBX (USERACK = OFF) The hardware SDA and SCL pins are used

Used Library = i2cslave.LBX


TIMER0 and INTO is used for SDA and SCL Timer0 Pin = SCL INT0 Pin = SDA

Config TWISLAVE

Config I2CISLAVE

Config I2CISLAVE
CONFIG I2CSLAVE = address , INT = interrupt , TIMER = tmr
Address: The slave address you want to assign to the I2C slave chip. This is an address that must be even like 60. So 61 cannot be used. Interrupt: The interrupt that must be used. This is INT0 by default. Tmr: The timer that must be used. This is TIMER0 by default.

Example: see i2c_pcf8574.bas

Config TWISLAVE
CONFIG TWISLAVE = address , BTR = value , BITRATE = value , SAVE=option [,GENCALL=value] [,USERACK=ack]
Address: The slave address that is assigned to the slave chip. This must be an Even number. The address 0 is the general call address and may not be used. While a slave address is 7 bit since bit 0 is used to indicate read/write, BASCOM uses byte notation where you can ignore the last bit. The last bit will be set by BASCOM automatically. BTR: Bytes to receive. With this constant you specify how many bytes will be expected when the slave receives bytes. 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.

Config TWISLAVE continued


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.

Config TWISLAVE continued


Automatically created BYTE variables:
Twi (The variable TWI holds the received value the Master has sent) Twi_btw (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_btr (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.)

The TWI interrupt is enabled but you need to enabled the global interrupt

Config TWISLAVE continued Sub Routines

Sub routines
Keep the code in the sub routines (for example Twi_gotdata) to a minimum. Its the same as an Interrupt Service Routine ! Only use Print commands for testing. Use SAVE option to save all registers (= default).

Config TWISLAVE continued Examples


Slave example
M88-TWI-slave.bas

Master example which belong to the Slave example


M88-TWI-master.bas

Config TWISLAVE continued Software Structure TWI SLAVE


Config TWISLAVE = .

Enable Interrupts

Do Loop

End

Sub Routines

Config TWISLAVE continued Subroutines


You can use TWI_BTR as an Index for your SEND Array (which is read by Master)

Twi_master_needs_byte: Twi = Send_array(twi_btr) Return

Config TWISLAVE continued Subroutines


You can use TWI_BTW as an Index for your RECEIVE Array (which is sent by Master)

Twi_gotdata: Receive_array(twi_btw) = Twi Return

I2C/TWI MASTER
With I2C/TWI MASTER you also have two choices
Use any SDA/SCL pin you want and choose (I2C is handled in software) Force BASCOM-AVR to use the hardware I2C/TWI by using: $lib "i2c_twi.lbx" (I2C is handled in hardware)

I2C/TWI MASTER Use any SDA/SCL pin you want


By default BASCOM will use software routines when you use I2C statements. This because when the first AVR chips were introduced, there was no TWI yet. Atmel named it TWI because Philips is the inventor of I2C. But TWI is the same as I2C.

I2C/TWI MASTER Force BASCOM-AVR to use the hardware I2C/TWI by using: $lib "i2c_twi.lbx"
With the I2C_TWI lib you can use the hardware TWI which has advantages like: It require less code This special Pins have Noise Suppression Circuitry which Rejects Spikes on Bus Lines and Slew-rate Limited Output Drivers Address Recognition causes Wake-up when AVR is in Sleep Mode Therefore you need to use the hardware SDA and SCL Pins according datasheet of AVR (for example ATMEGA32: SDA = PC 1 , SCL = PC 0 ) The hardware TWI will save code but the disadvantage is that you can only use the fixed SCL and SDA pins.

I2C/TWI MASTER Force BASCOM-AVR to use the hardware I2C/TWI by using: $lib "i2c_twi.lbx"
When using the TWI (hardware I2C) AND you have external Pullup Resistors on SDA and SCL (4.7K or 10K) there is no need to use config scl/sda because these pins are fixed just like the UART tx/rx pins. So you dont need config SDA, config SCL and also not i2cinit ! When you want to use the internal Pullup AND there are no external Pullup Resistors on SDA and SCL of the AVR Pin you need to use config SDA, config SCL and also I2CINIT because I2CINIT will then enable the internal Pullup for the configured Pins.

I2C/TWI MASTER Force BASCOM-AVR to use the hardware I2C/TWI by using: $lib "i2c_twi.lbx"
When your default settings for scl/sda (options) point to an invalid SDA and SLC Pin, you should also use config SDA, config SCL and I2CINIT.

Software Structure I2C/TWI MASTER


The preconditions for the following example are: - External Pullup Resistors on SDA and SCL - SDA and SCL Pins are correct configured under Options (I2C, SPI, 1-WIRE)

I2C/TWI MASTER continued

$lib "i2c_twi.lbx

Do I2cstart I2cwbyte &H70 I2cwbyte &B10101010 I2cwbyte 2 I2cstop Loop End ' slave address ' write one Byte to Slave

You have different Voltages like 5V and 3.3V on Slave Side


One solution for this is using Multiplexer like PCA9546 Another solution is using a Level translating I2C-bus repeater like PCA9517 When you also have long cables then consider P82B96 on both Side (Master and Slave Side)

You have long cable distance and several Slaves


Use P82B96 in Multipoint architecture.
I2C MASTER
3.3V

P82B96

Up to 15V

P82B96

1.8V

I2C Slave

P82B96 You also need isolation of I2C Bus: http://www.eetimes.com/design/ embedded/4025023/Optoelectrical-isolation-of-the-I2C-Bus

3.3V

I2C Slave

P82B96

5V

I2C Slave

P82B96

3.3V

I2C Slave

I2c/TWI MASTER with ATXMEGA Family


For example an ATXMEGA128A1 has 4 hardware I2C/TWI Interfaces on Port C, D , E and F You can run the different I2C/TWI Interfaces with different TWI Baud rates (for example one with 100000 and the other with 400000)
Initialization of I2C/TWI Interface of PORT C in BASCOM-AVR: Dim Twi_start As Byte Open "twic" For Binary As #2 'This Variable is used by the I2C functions

I2cinit #2 Config Twic = 100000

' Portc.0 = SDA Pin of Port C (ATXMEGA128A1) ' Portc.1 = SCL Pin of Port C (ATXMEGA128A1) 'set i2c pins to right state 'Set TWI Baud Rate and Enable TWI Master

I2c MASTER with ATXMEGA Family continued


Using the I2C/TWI functions (you always need to add the interface number !):

I2cstart #2 I2cwbyte Writeaddress, #2 I2cwbyte Write_a_byte , #2 I2cstop #2


Using high level functions (example for write one byte and read 3 Bytes)

Array_to_write(1) = Addressbyte I2creceive Array_to_write(1) , 1 , 3 , #2

S-ar putea să vă placă și