100% found this document useful (3 votes)
479 views5 pages

I2cslave PDF

The document describes an I2C slave library for BASCOM-AVR that allows a microcontroller to emulate an I2C slave device. It discusses configuring the slave address, implementing required labels that are called when the master reads from or writes to the slave, and provides a sample project code that emulates an IO expander chip using the library.

Uploaded by

mrtami123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
479 views5 pages

I2cslave PDF

The document describes an I2C slave library for BASCOM-AVR that allows a microcontroller to emulate an I2C slave device. It discusses configuring the slave address, implementing required labels that are called when the master reads from or writes to the slave, and provides a sample project code that emulates an IO expander chip using the library.

Uploaded by

mrtami123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BASCOM-AVR I2C SLAVE Library

The I2C-Slave library is intended to create I2C slave chips. You can control the chips with the BASCOM I2C statements like I2CINIT, I2CSEND, I2CRECEIVE, I2CWBYTE, etc. Please consult the BASCOM Help file for using I2C in master mode. Before you begin Copy the i2cslave.lib and i2cslave.lbx files into the BASCOM-AVR\LIB directorie. The i2cslave.lib file contains the ASM source. The i2cslave.lbx file contains the compiled ASM source. Slave address Every I2C device must have an address so it can be addressed by the master I2C routines. When you write to an I2C-slave chip the least significant bit (bit0) is used to specify if we want to read from the chip or that we want to write to the chip. When you specify the slave address, do not use bit 0 in the address! For example a PCF8574 has address &H40. To write to the chip use &H40, to read from the chip, use &H41. When emulating a PCF8574 we would specify address &H40. Use the CONFIG statement to specify the slave address: Config I2cslave = &B01000000 ' same as &H40 Optional use : CONFIG I2CSLAVE = address, INT= int , TIMER = tmr Where INT is INT0, INT1 etc. and TIMER is TIMER0, TIMER1 etc. When using other interrupts or timers, you need to change the library source! This directive will enable the global interrupt. After you have configured the slave address, you can insert your code. A do-loop would be best: Do your code here Loop This is a simple never ending loop. You can use a GOTO with a label or a While Wend loop too but ensure that the program will never end. After your main program code you need to insert two labels: When the master needs to read a byte, the following label is always called You must put the data you want to send to the master in variable _a1 which is register R16 I2c_master_needs_data: 'when your code is short, you need to put in a waitms statement 'Take in mind that during this routine, a wait state is active and the master will wait 'After the return, the waitstate is ended Config Portb = Input ' make it an input _a1 = Pinb ' Get input from portB and assign it Return

(c) MCS Electronics

BASCOM-AVR I2C SLAVE Library

When the master writes a byte, the following label is always called It is your task to retrieve variable _A1 and do something with it _A1 is register R16 that could be destroyed/altered by BASIC statements For that reason it is important that you first save this variable I2c_master_has_data: 'when your code is short, you need to put in a waitms statement 'Take in mind that during this routine, a wait state is active and the master will wait 'After the return, the waitstate is ended Bfake = _a1 ' this is not needed but it shows how you can store _A1 in a byte 'after you have stored the received data into bFake, you can alter R16 Config Portb = Output ' make it an output since it could be an input Portb = _a1 'assign _A1 (R16) Return

The additional code inside the labels is copied from the i2cslave_pcf8574.bas example. When the slave address is asked to send data, the label I2C_MASTER_NEEDS_DATA is called by the library. It is you task to assign a value to _A1. This value will be received by the master read code. When the slave library is instructed that data will follow, the label I2C_MASTER_HAS_DATA will be called by the library. In this routine you can retrieve the data by reading _A1. Assign _A1 to a variable before you use BASIC commands since some commands might alter _A1. _A is register R16. Do not forget to use 4K7 pull up resistors on the SCL and SDA lines.

The SCL and SDA pins are fixed since they use INT0 and TIMER/COUNTER0. Chip SCL SDA 90S2313 PORTD.4 PORTD.2 90S2333 PORTD.4 PORTD.2 90S4433 PORTD.4 PORTD.2 Mega8 PORTD.4 PORTD.2 90S2323 PORTB.2 PORTB.1 90S2343 PORTB.2 PORTB.1

(c) MCS Electronics

BASCOM-AVR I2C SLAVE Library

BASCOM sample project.


'-------------------------------------------------------------------------' I2C SLAVE LIBRARY DEMO ' PCF8574 emulator ' (c) 2002 MCS Electronics '-------------------------------------------------------------------------'This program shows how you could use the I2C slave library to create a PCF8574 'The PCF8574 is an IO extender chip that has 8 pins. 'The pins can be set to a logic level by writing the address followed by a value 'In order to read from the pins you need to make them '1' first 'This program uses a AT90S2313, PORTB is used as the PCF8574 PORT 'The slave library needs INT0 and TIMER0 in order to work. 'SCL is PORTD.4 (T0) 'SDA is PORTD.2 (INT0) 'Use 10K pull up resistors for both SCL and SDA 'The Slave library will only work for chips that have T0 and INT0 connected to the same PORT. 'These chips are : 2313,2323, 2333,2343,4433,tiny22, tiny12,tiny15, M8 'The other chips have build in hardware I2C(slave) support.

'specify the XTAL connected to the chip $crystal = 3684000 'specify the used chip $regfile = "2313def.dat" 'specify the slave address. This is &H40 for the PCF8574 'You always need to specify the address used for write. In this case &H40 , 'The config i2cslave command will enable the global interrupt enable flag ! Config I2cslave = &B01000000 ' same as &H40 'A byte named _i2c_slave_address_received is generated by the compiler. 'This byte will hold the received address. 'A byte named _i2c_slave_address is generated by the compiler. 'This byte must be assigned with the slave address of your choice 'the following constants will be created that are used by the slave library: ' ' ' ' ' ' _i2c_pinmask = &H14 _i2c_slave_port = Portd _i2c_slave_pin = Pind _i2c_slave_ddr = Ddrd _i2c_slave_scl = 4 _i2c_slave_sda = 2

'These values are adjusted automatic depending on the selected chip. 'You do not need to worry about it, only provided as additional info 'by default the PCF8574 port is set to input Config Portb = Input Portb = 255

'all pins high

(c) MCS Electronics

BASCOM-AVR I2C SLAVE Library


by default 'DIM a byte that is not needed but shows how you can store/write the I2C DATA Dim Bfake As Byte

'empty loop Do ' you could put your other program code here 'In any case, do not use END since it will disable interrupts Loop

'here you can write your other program code 'But do not forget, do not use END. Use STOP when needed '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' The following labels are called from the slave library '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 'When the master wants to read a byte, the following label is always called 'You must put the data you want to send to the master in variable _a1 which is register R16 I2c_master_needs_data: 'when your code is short, you need to put in a waitms statement 'Take in mind that during this routine, a wait state is active and the master will wait 'After the return, the waitstate is ended Config Portb = Input ' make it an input _a1 = Pinb ' Get input from portB and assign it Return

'When the master 'It is your task '_A1 is register 'For that reason

writes a byte, the following label is allways called to retrieve variable _A1 and do something with it R16 that could be destroyed/altered by BASIC statements it is important that you first save this variable

I2c_master_has_data: 'when your code is short, you need to put in a waitms statement 'Take in mind that during this routine, a wait state is active and the master will wait 'After the return, the waitstate is ended Bfake = _a1 ' this is not needed but it shows how you can store _A1 in a byte 'after you have stored the received data into bFake, you can alter R16 Config Portb = Output ' make it an output since it could be an input Portb = _a1 'assign _A1 (R16) Return

'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 'You could simply extend this sample so it will use 3 pins of PORT D for the address selection 'For example portD.1 , portd.2 and portD.3 could be used for the address selection 'Then after the CONFIG I2CSLAVE = &H40 statement, you can put code like:

(c) MCS Electronics

BASCOM-AVR I2C SLAVE Library


'Dim switches as Byte ' dim byte 'switches = PIND ' get dip switch value 'switches = switches and &H1110 ' we only need the lower nibble without '_i2c_slave_address = &H40 + switches ' set the proper address

(c) MCS Electronics

You might also like