'================================================================ ' File: I2C_FR.BAS for a Ramtron FM24CL64 serial FRAM or any I2C eeprom that ' has a word address ' Compiler: BascomAVR 1.11.6.4 ' Author: Ranjit Diol ' rsdiol@compsys1.com ' http://www.compsys1.com/worknench/ ' Date: 03/26/2002 ' ' IMPORTANT NOTE: ' If using a standard I2C eeprom which requires a wait after each write ' uncomment and adjust the WAITMS statement in the W_write subroutine '================================================================ 'Assigned constants and variables: ' E_addr word variable for the address ' E_ah high byte of address ' E_al low byte of address ' E_dat byte value written ' dat byte value read ' E_ctlw = &HA0 I2C EEprom control/chip select code for a write (check eeprom specs) ' E_ctlr = &HA1 I2C EEprom control/chip select code for a read (check eeprom specs) ' ' I2C EEprom Notes ' The control code format is: ' Ctrl Chip Address Write=0/Read=1 ' 1010 A2 A1 A0 0 ' ' If only one I2C eeprom exists then all 3 chip select pins (A0-A2)are pulled low (0) ' 10100000 (&HA0) is the control code for a write instruction ' 10100001 (&HA1) is the control code for a read instruction '================================================================ $regfile = "m163def.dat" 'MCU $baud = 19200 'I/O baud for default I/O port $crystal = 4000000 'OSC speed 'I2C pins Config Sda = Portc.1 Config Scl = Portc.0 'Declarations Declare Sub E_write(byval E_addr As Word , Dat As Byte) Declare Sub E_read(byval E_addr As Word , E_dat As Byte) 'I2C Eeprom control Const E_ctlw = &HA0 'Control for write Const E_ctlr = &HA1 'Control for read 'I2C Vars Dim E_ah As Byte , E_al As Byte , E_addr As Word , E_dat As Byte , Dat As Byte 'Begin program '============= Main: 'Test read and writes Print "Testing I2C Eeprom" Dat = 0 For E_addr = &H1000 To &H1010 Call E_write(e_addr , Dat) Call E_read(e_addr , E_dat) : Print E_dat; Incr Dat Next E_addr Print Print "End of Test" End '============= ' Routines '============= Sub E_write(byval E_addr As Word , Dat As Byte) E_ah = High(e_addr) E_al = Low(e_addr) I2cstart I2cwbyte E_ctlw I2cwbyte E_ah I2cwbyte E_al I2cwbyte Dat I2cstop 'Waitms 10 'Uncomment for eeproms which require a wait of 5 or 10ms End Sub Sub E_read(byval E_addr As Word , E_dat As Byte) E_ah = High(e_addr) E_al = Low(e_addr) I2cstart I2cwbyte E_ctlw I2cwbyte E_ah I2cwbyte E_al I2cstart I2cwbyte E_ctlr I2crbyte E_dat , Nack I2cstop ' End Sub '=============== 'END OF CODE