Advertisement  

Tuesday, 14 July 2020
     
 
Main Menu
Home Home
Shop Shop
News News
BASCOM-AVR BASCOM-AVR
BASCOM-8051 BASCOM-8051
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-AVR Help BASCOM-AVR Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login
Username

Password

If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News
Check the Shop
  Random 
Random
 

SE-MINI-STAR
SE-MINI-STAR
EUR 9.62


BASCOM-AVR CrossUpgrade (CD version)
BASCOM-AVR CrossUpgrade (CD version)
EUR 83.49


 
     
 

 
   
     
 
I2CSLAVE - Turn an AVR micro into an I2C slave chip Print
It allows you to turn an AVR micro into an I2C slave chip.

 

I2CSLAVE is an addon product for BASCOM-AVR.

It allows you to turn an AVR micro into an I2C slave chip.

You could start your own factory for I2C chips : create your own PCF8574 , I2C LCD display etc.

Works for  2313,2323, 2333,2343,4433, tiny22, tiny12, tiny15 and M8. Other AVR chips have build in hardware for I2C.
 

An example is shown below :

'-------------------------------------------------------------------------------
' 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 = 8000000

'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 read. In this case &H41 ,
'since the I2C protol specifies that a read will have set the LS bit to 1
'The config i2cslave command will enable the global interrupt enable flag !
Config I2cslave = &B01000001 ' same as &H41

'A byte named _i2c_slave_address_received is generated by the compiler.
'This byte will hold the received address. This can be used for simple protocols where you can take
'actions based on 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, ony provided as additional info

'by default the PCF8574 port is set to input
Config Portb = Input
Portb = 255 'all pins high 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 allways 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
Waitms 1
Config Portb = Input ' make it an input
_a1
= Pinb ' Get input from portB and assign it
Return


'When the master writes a byte, the following label is allways 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
Waitms 1
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 = &H41 statement, you can put code like:
'Dim switches as Byte ' dim byte
'switches = PIND + 1 ' get dip switch value and add 1 for the write bit
'switches = switches and &H0F ' we only need the lower nibble
'_i2c_slave_address = &H40 + switches ' set the proper address