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

📄 ex32 - 24lc32.bs2

📁 proteus得一些经典例子
💻 BS2
字号:
' {$STAMP BS2}
' ==============================================================================
'
' File...... Ex32 - 24LC32.BS2
' Purpose... 24LC32 control via I2C
' Author.... Parallax
' E-mail.... stamptech@parallaxinc.com
' Started...
' Updated... 01 MAY 2002
'
'
' ==============================================================================
' ------------------------------------------------------------------------------
' Program Description
' ------------------------------------------------------------------------------
'
' This program demonstrates essential I2C routines and communication with the
' Microchip 24LC32 serial EEPROM.
'
' The connections for this program conform to the BS2p I2CIN and I2COUT
' commands. Use this program for the BS2, BS2e or BS2sx. There is a separate
' program for the BS2p.
' ------------------------------------------------------------------------------
' I/O Definitions
' ------------------------------------------------------------------------------
'
SDA CON 8 ' I2C serial data line
SCL CON 9 ' I2C serial clock line
' ------------------------------------------------------------------------------
' Constants
' ------------------------------------------------------------------------------
DevType CON %1010 << 4 ' device type
DevAddr CON %000 << 1 ' address = %000 -> %111
Wr2432 CON DevType | DevAddr | 0 ' write to 24LC32
Rd2432 CON DevType | DevAddr | 1 ' read from 24LC32
ACK CON 0 ' acknowledge bit
NAK CON 1 ' no ack bit
CrsrXY CON 2 ' DEBUG Position Control
' ------------------------------------------------------------------------------
' Variables
' ------------------------------------------------------------------------------
i2cSDA VAR Nib ' I2C serial data pin
i2cData VAR Byte ' data to/from device
i2cWork VAR Byte ' work byte for TX routine
i2cAck VAR Bit ' ACK bit from device
eeAddr VAR Word ' address: 0 - 4095
test VAR Nib
outVal VAR Byte ' output to EEPROM
inVal VAR Byte ' input from EEPROM
' ------------------------------------------------------------------------------
' Initialization
' ------------------------------------------------------------------------------
Initialize:
PAUSE 250 ' let DEBUG open
DEBUG CLS, "24LC32 Demo", CR, CR ' setup output screen
DEBUG "Address... ", CR
DEBUG "Output.... ", CR
DEBUG "Input..... ", CR
i2cSDA = SDA ' define SDA pin
' ------------------------------------------------------------------------------
' Program Code
' ------------------------------------------------------------------------------
Main:
FOR eeAddr = 0 TO 4095 ' test all locations
DEBUG CrsrXY, 11, 2, CR, DEC eeAddr, " "
FOR test = 0 TO 3 ' use four patterns
LOOKUP test, [$FF, $AA, $55, $00], outVal
DEBUG CrsrXY, 11, 3, IHEX2 outVal
i2cData = outVal
GOSUB Write_Byte
PAUSE 10
GOSUB Read_Byte
inVal = i2cData
DEBUG CrsrXY, 11, 4, IHEX2 inVal, " "
IF (inVal <> outVal) THEN Bad_Addr
DEBUG "Pass "
GOTO Next_Addr
Bad_Addr:
DEBUG "Fail "
Next_Addr:
PAUSE 50
NEXT
NEXT
DEBUG CR, CR, "Done!"
END
' ------------------------------------------------------------------------------
' Subroutines
' ------------------------------------------------------------------------------
' Byte to be written is passed in i2cData
' -- address passed in eeAddr
Write_Byte:
GOSUB I2C_Start ' send Start
i2cWork = Wr2432 ' send write command
GOSUB I2C_TX_Byte
IF (i2cAck = NAK) THEN Write_Byte ' wait until not busy
i2cWork = eeAddr / 256 ' send word address (1)
GOSUB I2C_TX_Byte
i2cWork = eeAddr // 256 ' send word address (0)
GOSUB I2C_TX_Byte
i2cWork = i2cData ' send data
GOSUB I2C_TX_Byte
GOSUB I2C_Stop
RETURN
' Byte read is returned in i2cData
' -- address passed in eeAddr
Read_Byte:
GOSUB I2C_Start ' send Start
i2cWork = Wr2432 ' send write command
GOSUB I2C_TX_Byte
IF (i2cAck = NAK) THEN Write_Byte ' wait until not busy
i2cWork = eeAddr / 256 ' send word address (1)
GOSUB I2C_TX_Byte
i2cWork = eeAddr // 256 ' send word address (0)
GOSUB I2C_TX_Byte
GOSUB I2C_Start
i2cWork = Rd2432 ' send read command
GOSUB I2C_TX_Byte
GOSUB I2C_RX_Byte_Nak
GOSUB I2C_Stop
i2cData = i2cWork
RETURN
' ------------------------------------------------------------------------------
' Low Level I2C Subroutines
' ------------------------------------------------------------------------------
' --- Start ---
I2C_Start: ' I2C start bit sequence
INPUT i2cSDA
INPUT SCL
LOW i2cSDA ' SDA -> low while SCL high
Clock_Hold:
IF (Ins.LowBit(SCL) = 0) THEN Clock_Hold ' device ready?
RETURN
' --- Transmit ---
I2C_TX_Byte:
SHIFTOUT i2cSDA, SCL, MSBFIRST, [i2cWork\8] ' send byte to device
SHIFTIN i2cSDA, SCL, MSBPRE, [i2cAck\1] ' get acknowledge bit
RETURN
' --- Receive ---
I2C_RX_Byte_Nak:
i2cAck = NAK ' no ACK = high
GOTO I2C_RX
I2C_RX_Byte:
i2cAck = ACK ' ACK = low
I2C_RX:
SHIFTIN i2cSDA, SCL, MSBPRE, [i2cWork\8] ' get byte from device
SHIFTOUT i2cSDA, SCL, LSBFIRST, [i2cAck\1] ' send ack or nak
RETURN
' --- Stop ---
I2C_Stop: ' I2C stop bit sequence
LOW i2cSDA
INPUT SCL
INPUT i2cSDA ' SDA --> high while SCL high
RETURN

⌨️ 快捷键说明

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