Code BasicStamp BS2 pour l'eeprom 24LC256

Forum sur la programmation Basic Stamp
Répondre
andromeda
Messages : 706
Enregistré le : 30 mars 2004 0:07
Contact :

Code BasicStamp BS2 pour l'eeprom 24LC256

Message par andromeda » 05 mars 2007 11:24

Code : Tout sélectionner

'{ $STAMP BS2 }

'--{ I2C Tutorial Software }---------------------------------------------------

' Copyright 2001 High-TechGarage
' Get the most current software at: http://www.High-TechGarage.com/software/
' Revision 1.1 03/05/02

' This software is from the I2C Serial EEPROM tutorial and is meant to interface
' with the Microchip 2-wire serial EEPROM device 24LC256. This code should work with
' other EEPROMs which do not have memory orgainzed in page blocks.
' The tutorial is located at:
' http://www.High-TechGarage.com/tutorial/i2c.php

' PINOUT for the 24LC256 (consult data sheets for other devices)

' ______
' A0 -| |- Vcc
' A1 -| |- WP
' A2 -| |- SCL
' Vss -|______|- SDA

' PIN DESCRIPTION CONNECT TO:
' --- ----------- -----------
' A0 Chip select 0 ground
' A1 Chip select 1 ground
' A2 Chip select 2 ground
' Vss Ground ground
' SDA Serial data P0 on STAMP
' SCL Serial clock P1 on STAMP
' WP Write-protect ground or leave floating
' Vcc Supply voltage +5V

' Up to 8 24LC256 devices can be connected on the same I2C bus by configuring
' the address pins in a unique manner. For example, a device with A2 and A0 to GND
' and A1 to +5v would have the address of 010. In our example, all address pins are
' grounded giving the addres 000.


'--{ Pinouts defined }-------------------------------------------------------

SDA con 0 ' I2C serial data input/output
SCL con 1 ' I2C serial clock


'--{ I2C definitions }-------------------------------------------------------

i2c_read con 1 ' R/W configuration bit (1 = read)
i2c_write con 0 ' R/W configuration bit (0 = write)
i2c_out var byte ' data sent over I2C bus
i2c_in var byte ' data received over I2C bus
i2c_ack var bit ' acknowledgement bit


'--{ EEPROM definitions }-----------------------------------------------------

device con %1010000 ' Control code for EEPROM plus address pins
EEdata var byte ' data to/from the EEPROM
EEaddress var word ' address location to store EEPROM data (0-255)


'--{ General variables }-----------------------------------------------------

counter var byte ' general purpose counter


'===[ MAIN PROGRAM STARTS HERE ]===========================================================

' For the first example, let's write a value of 197 to memory location 2042.

EEdata = 197 ' data
EEaddress = 2042 ' address
gosub ByteWrite

debug "Wrote ", dec EEdata, " to memory location ", dec EEaddress, cr, cr

pause 1000

' Now let's see what was written at memory location 2042.
' (It should be what we just wrote, that is 163).

EEaddress = 2042
gosub RandomRead

debug "At memory location ", dec EEaddress, " the data was ", dec i2c_in, cr, cr
debug "END"

end

'===[ MAIN PROGRAM ENDS HERE ]=============================================================


'--{ EEPROM Subroutines }----------------------------------------------------

ByteWrite: ' write a byte in EEPROM
gosub I2C_START
for counter = 0 to 2
lookup counter, [ (device<<1 | i2c_write), EEaddress.highbyte, EEaddress.lowbyte], i2c_out
gosub I2C_TX
next
i2c_out = EEdata
gosub I2C_TX
gosub I2C_STOP
return

CurrentRead: ' Read the current address of EEPROM
gosub I2C_START
i2c_out = device << 1 | i2c_read
gosub I2C_TX
gosub I2C_RX_NO_ACK ' terminate communication without an ACK
return

RandomRead:
gosub I2C_START
for counter = 0 to 2
lookup counter, [ (device<<1 | i2c_write), EEaddress.highbyte, EEaddress.lowbyte], i2c_out
gosub I2C_TX
next
gosub I2C_START
i2c_out = device << 1 | i2c_read
gosub I2C_TX
gosub I2C_RX_NO_ACK ' terminate communication without an ACK
return


'--{ I2C subroutines }-------------------------------------------------------

I2C_TX: ' I2C transmit -> send data to the slave
shiftout SDA,SCL,1,[i2c_out]
shiftin SDA,SCL,0,[i2c_ack1]
if i2C_ack = 0 then I2C_RETURN
return

I2C_RX: ' I2C receive with ACK -> receive data from slave
shiftin SDA,SCL,0,[i2c_in]
shiftout SDA,SCL,1,[%01]
return

I2C_RX_NO_ACK: ' I2C receive without ACK -> receive data from slave
shiftin SDA,SCL,0,[i2c_in]
gosub I2C_STOP

I2C_RETURN: ' return to the program
return

I2C_START: ' I2C start (start communication on I2C bus)
high SDA
high SCL
low SDA
low SCL
return

I2C_STOP: ' I2C stop (terminate communication on I2C bus)
low SDA
high SCL
high SDA
pause 1
return
Moderator - Forum

Répondre