⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.bas

📁 82 sample programs written in BASCOM-8051 for 8051 developement
💻 BAS
字号:
'--------------------------------------------------------------
'                     (c) 1997-2000 MCS Electronics
'--------------------------------------------------------------
'  file: I2C.BAS
'  demo: I2CSEND and I2CRECEIVE
'--------------------------------------------------------------
Declare Sub Write_eeprom(adres As Byte , Value As Byte)
Declare Sub Read_eeprom(adres As Byte , Value As Byte)

'declare constants
Const Addressw = 174
Const Addressr = 175

Dim B1 As Byte , Adres As Byte , Value As Byte'dim byte

Call Write_eeprom(1 , 3)                'write value of three to address 1 of EEPROM


Call Read_eeprom(1 , Value) : Print Value'read it back
Call Read_eeprom(5 , Value) : Print Value'again for address 5

Config I2cdelay = 1                                           'default so not needed

'-------- now write to a PCF8474 I/O expander -------
I2csend &H40 , 255                      'all outputs high
I2creceive &H40 , B1                    'retrieve input
Print "Received data " ; B1             'print it
End

Rem Note That The Slaveaddress Is Adjusted Automaticly With I2csend & I2creceive
Rem This Means You Can Specify The Baseaddress Of The Chip.




'sample of writing a byte to EEPROM AT2404
Sub Write_eeprom(adres As Byte , Value As Byte)
    I2cstart                            'start condition
    I2cwbyte Addressw                   'slave address
    I2cwbyte Adres                      'asdress of EEPROM
    I2cwbyte Value                      'value to write
    I2cstop                             'stop condition
    Waitms 10                           'wait for 10 milliseconds
End Sub


'sample of reading a byte from EEPROM AT2404
Sub Read_eeprom(adres As Byte , Value As Byte)
   I2cstart                             'generate start
   I2cwbyte Addressw                    'slave adsress
   I2cwbyte Adres                       'address of EEPROM
   I2cstart                             'repeated start
   I2cwbyte Addressr                    'slave address (read)
   I2crbyte Value , 9                   'read byte
   I2cstop                              'generate stop
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -