Using I2ctwi With Bascom-Avr V 0.5
Using I2ctwi With Bascom-Avr V 0.5
Using I2ctwi With Bascom-Avr V 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
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)
MASTER
SLAVE
Used Library = i2c_TWI-slave-acknack.LBX (USERACK = ON) i2c_TWI-slave.LBX (USERACK = OFF) The hardware SDA and SCL pins are used
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.
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.
The TWI interrupt is enabled but you need to enabled the global interrupt
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).
Enable Interrupts
Do Loop
End
Sub Routines
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 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.
$lib "i2c_twi.lbx
Do I2cstart I2cwbyte &H70 I2cwbyte &B10101010 I2cwbyte 2 I2cstop Loop End ' slave address ' write one Byte to Slave
P82B96
Up to 15V
P82B96
1.8V
I2C Slave
3.3V
I2C Slave
P82B96
5V
I2C Slave
P82B96
3.3V
I2C Slave
' 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