Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 06:56 28 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : [MMBasic] I2C EEPROM Library AT24C32

Author Message
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 11:17am 06 Sep 2014
Copy link to clipboard 
Print this post

Hi all,

this is my new I2C EEPROM Maximite Library v.82 for AT24C32. Many of us know this 32kb chip from cheap DS1307 or DS3231 RTC modules. Most Parts (excluding eeDump? and eeMon) of this library should also work with all 'Mites - I hope so! I did some research to find out the "wear out" behavior. I came to the conclusion that a read event after a write event only after one second delay reliable results delivers (a write takes 0.005 sec or even less!). Therefore I removed the verify routines for short writes, but I kept it for eeErase ... and as demo in eeSaveF4 (not reliable!!).

In most cases I think you dont need a verify because the endurance is 1 Million Write Cycles (says ATMEL) or even more. My test EEPROM showed its first weakness after 3.5 Million Write Cycles! This does not necessarily mean that the data are retained for 100 years. With this EEPROM we can do a reliable Data Logging. There is space for more then 1000 floating point numbers (eeSaveF4).

Please note: The I2C Address must be set for your specific EEPROM!

On Maximites using eeMon you can now easily scroll through the EEPROM content.




The code (still beta):

'********************************************
'********************************************
' EEPROM-Library v0.81
' -----------------------------------------
' by twofingers 09-2014 at TBS
' with parts from MMBasic Library
' for AT24V32 (32kBit EEPROM)
' at DS3231 or DS1307 RTC-Modules etc.
' MMBasic 4.5 for Maximite/Duinomite
'------------------------------------------
' eeWrite$: writes a string to address
' eeRead$ : reads a string from address
' eeGet$ : returns a amount of bytes from address
' eeWriteB: writes a 1 byte to address
' eeReadB : reads 1 byte from address
' address may be between 0 to 4095
'
' eeSaveF : writes a 32 bit floatinpoint number
' eeSaveF4: writes a 32 bit floatinpoint number
' at EEPROM addresses divisible by 4
' (faster then eeSaveF)
' eeLoadF : reads a 32 bit floatinpoint number
' back from EEPROM using PEEK and POKE
' needs eeWriteB and eeReadB
'
' eeErase : deletes the whole eeprom w. verify
'
' eeDump : shows content of eeprom or parts
' eeMon : displays whole eeprom content (scroll)
'
' I2CPROM (I2C address of your EEPROM)
' must always be defined globally
'-------------------------------------------
'
' Remark: A check for defective Bytes
' has a latency of ~1 sec !!
' Therefore one second should be elapsed after
' each write event and then follow a read/compare,
' if a verify is required.
'
'-------------------------------------------
'
' This code may be freely distributed and
' changed. Provided AS IS without any warranty.
' Use it at your own risk.
'********************************************
'********************************************




'#######################################
' Demonstration code
'--------------------
I2CPROM = &H57 'I2CAdress of EEPROM
'if unknown please use the i2CSanner
'or the damn datasheet
'&H57 for my DS3231 modules
'&H50 for my DS1307 RTC modules


Cls
Print "WARNING: THIS DEMO WILL ERASE YOUR EEPROM!":Print
waitkey"press any key for eeSaveF4 demonstration (or CTRL+C to abort)"
Print "writing 1024 floats ... (takes 5s)"

For i = 0 To 1023 'fill with 32 bit floats (= 4 bytes)
eeSaveF4(i*4,i+Pi/10)
Next i

waitkey"press any key for eeLoadF (reading back)"

Cls
For i = 0 To 1023
Print eeLoadF(i*4)
Next i


' Dump demos
waitkey"press any key for eeDump 3800,100"
Cls
eeDump 3800,100

waitkey"press any key for eeDump 3990"
Cls
eeDump 3500

waitkey"press any key for eeDump"
Cls
Print "reading (4096 bytes)... (4s)"
eeDump

waitkey"press any key for eeMon"
Cls
eeMon
Cls
waitkey"press any key for eeErase"
Cls
eeErase

waitkey"press any key for eeMon"
Cls
eeMon
Cls
l1:
waitkey"press any key for eeWrite$ + eeRead$ + eeGet$"
Cls
Print "Writing at 0 'THE BACKSHED DEMO'"
eeWrite$ 0,"THE BACKSHED DEMO"

For i = 1 To 25
a$=a$+"ABCDFEGHIJ"
Next i
A$=A$+"ABCDF"
Print "Writing at 256: "A$:Print:Print
eeWrite$ 256,A$
Print:Print "reading back:"
Print eeRead$(256)
Print
Print eeGet$(0,17)

waitkey"press any key for eeMon"

eeMon
Cls
waitkey"press any key for eeWriteB + eeReadB"

For i = 0 To 255
eeWriteB i,i
Next i


For i = 1 To 255
Print eeReadB(i)
Next i

waitkey"press any key for eeMon"

eeMon
Cls
waitkey"End of demonstration! Press any key."

'--------------------------
' End of demonstration code
'#######################################





'**********************************
' Saves a floatingp. var (v) to AT24C32
' similar to eeSaveF but faster
'
' address should be divisible
' by 4 without a remainder
' (page mode roll over trouble)
' Plz obey mmbasic limits for
' floating point numbers otherwise
' U will get a serious memory error!
'**********************************
Sub eeSaveF4 (address, v)
Local fill(6),i,v1
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

If address Mod 4 Then
Print "Error: address should be divisible without a remainder"
End
EndIf

I2C open 100,100 'ENABLE I2C

fill(0)=Int(address/256) 'MSB
fill(1)=address Mod 256 'LSB
For i = 0 To 3: fill(i+2)=Peek(VAR v,i):Next i

I2C write I2CPROM, 0, 6, fill(0)
If MM.I2C <>0 Then Print "Error(1)! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1

I2C close

End Sub


'******************************
Sub eeSaveF (address, v)
' Saves a floatingp. var to AT24C32
'******************************
Local i
For i = 0 To 3
eeWriteB address+i, Peek(VAR v,i)
Next i
End Sub


'******************************
Function eeLoadF (address)
' Load a floatingp. var from AT24C32
' Attention:
' never use this for strings!
'******************************
Local i
For i = 0 To 3
Poke VAR eeLoadF,i, eeReadB(address+i)
Next i
Function Sub


'******************************
Sub eeWriteB (address, byte)
' Write a byte (0-255) to AT24C32
'******************************
Local msb, lsb
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

I2C open 100,100 'ENABLE I2C
msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0, 3, msb, lsb, byte
Pause 1
If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
I2C close
If debug Then Print byte;" written into EEPROM."
End Sub


'******************************
Function eeReadB (address)
' Read a byte (0-255) from AT24C32
'******************************
Local msb, lsb

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

I2C open 100,100 'ENABLE I2C
msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,eeReadB

If MM.I2C <>0 Then Print "ERROR! 1=NAK 2=TIMEOUT"; MM.I2C:eeReadN=-1:End
I2C close

End Function


'******************************
Sub eeWrite$ (address, text$)
' Writes a string to AT24C32
'******************************
Local a,x,msb,lsb,PByte

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

a=0

If Len(text$)+address>4095 Then Print "Error: Address too high!":End

I2C open 100,100 'ENABLE I2C
For x= address To address+Len(text$) 'writes a 0-byte at the end!
a=a+1
msb=Int(x/256) 'MSB
lsb=x Mod 256 'LSB

PByte=0:If a<256 Then PByte=Asc(Mid$(text$,a,1))

I2C write I2CPROM, 0, 3, msb, lsb, PByte
If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1
Next x
I2C close
If debug Then
Print "The string "+Chr$(34);:Print text$;
Print Chr$(34)+" was successfully written at address"address" into EEPROM."
EndIf
End Sub


'******************************
Function eeGet$ (address, amount)
' Returning a amount of bytes from
' address of an AT24C32.
'******************************
Local x,msb,lsb,tmp

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

eeGet$=""

If address>4095 Then Print "Error: Address too high!":End
If amount>255 Then Print "Error: String would be too long!":End

I2C open 100,100 'ENABLE I2C
For x= address To address+amount-1

msb=Int(x/256) 'MSB
lsb=x Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,tmp

If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
eeGet$=eeGet$+Chr$(tmp)

Next x
I2C close
End Function


'******************************
Function eeRead$ (address)
' Reads a zero-terminated string from AT24C32
'******************************
Local msb,lsb,tmp
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

eeRead$=""

If address>4094 Then Print "Error: Address too high! (valid: 0-4094)":End

I2C open 100,100 'ENABLE I2C

Do

msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,tmp

If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
If tmp=0 Then Exit 'leave loop
eeRead$=eeRead$+Chr$(tmp)
address=address+1

Loop While Len(eeRead$)=<255

I2C close
Function end


'**************************************
' ERASE WHOLE EEPROM (fill with zero)
' Warning:
' EEPROM has a limited endurance
'**************************************
Sub eeErase
Local i,K1,eePage,MSB,LSB,xfill(34)
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End
K=&H00 'zero value or something else - K=&HAA, K=&H55 ... K=&HFF
Timer=0

If GetKey$("Ready to erase EEPROM at &H"+Hex$(I2CPROM)+"? <y/n>")<>"y" Then
Print "EEPROM erase aborted!"
End Sub
Else
Print "EEPROM erase confirmed!"
EndIf

For i = 0 To 31: Xfill(i+2)=K: Next i

Print "Erasing ";:vpos=MM.VPos:hpos=MM.HPos
I2C open 100,100 'ENABLE I2C

For eePage = 0 To &H7F '&H7F = 128 Pages a 32 bytes = 4096 bytes
MSB=Int(eePage/8)
LSB=(eePage-MSB*8)*32
Xfill(0)=MSB
Xfill(1)=LSB
I2C write I2CPROM, 0, 34, Xfill(0)
If MM.I2C <>0 Then Print "Error(1)! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1
Print @(HPos+MSB*4,VPos)".";' progressbar
Next eePage

'verify
For eePage = 0 To &H7F '&H7F = 128 * 32 bytes = 4096 bytes
MSB=Int(eePage/8)
LSB=(eePage-MSB*8)*32

I2C write I2CPROM,0,2,MSB,LSB ' set read address
For i=0 To 31
I2C read I2CPROM,0,1,K1
If MM.I2C <>0 Then Print "Error(2)! 1=NAK 2=TIMEOUT"; MM.I2C: End
If K<>K1 Then
Print:Print "Verify error! at"; MSB*256+LSB+i;
Print " Byte: ";K " <>" K1;" - "Bin$(k)" <> "Bin$(K1)
End
EndIf
Next i
Print @(HPos+MSB*4,VPos)":";' progressbar
Next eePage

I2C close
Print:Print "SUCCESS: EEPROM HAS BEEN ERASED!"
Print "Elapsed time: "Timer"ms"
End Sub


'*******************************************
' DUMP EEPROM
' Shows content from AT24C32
' will be used from eeMon or separatly
'
' Syntax:
' eeDump
' Displays whole content of EEPROM, byte 0-4095
'
' eeDump start
' Displays partial content of EEPROM, byte start-4095
'
' eeDump start,amount
' Displays partial content of EEPROM,
' byte start to start+amount
'*******************************************
Sub eeDump start,amount
Local FromByteNr, msb, lsb, i, RTCBUFF(4096)
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

If amount=0 Then amount=4095

FromByteNr=Int(start/16)*16
amount=amount+start-FromByteNr:If amount+FromByteNr>4095 Then amount=4095-FromByteNr

If amount<1 Then
Print "Error: Nothing to display. Amount: " amount,FromByteNr
End Sub
EndIf

amount=(Int((amount+15)/16))*16
' fill buffer
I2C open 100,100 'ENABLE I2C

' reading EEPROM
For i = FromByteNr To FromByteNr+amount
msb=Int(i/256) 'MSB
lsb=i Mod 256 'LSB
I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,RTCBUFF(msb*256+lsb)
If MM.I2C <>0 Then Print "ERROR! 1=NAK 2=TIMEOUT"; MM.I2C,i:End
Next i

I2C close

' display buffer content
For I=FromByteNr To FromByteNr+amount-1 Step 16' 4095 Step 16
Print "Byte ";Format$(I,"%4g");" ||";
For y = 0 To 15
Print Right$("0"+Hex$(RTCBUFF(I+y)),2);"|";
Next y
Print "|";
For y = 0 To 15
If RTCBUFF(I+y) >=32 And RTCBUFF(I+y)<=126 Then ' show ascii only
C$=Chr$(RTCBUFF(I+y))
Else
C$=" "
EndIf
Print C$;
Next y
Print "|"
'waitkey"press any key"
Next I

End Sub


'*************************************
Sub eeMon 'eeMonitor
' Shows content of EEPROM
' You can navigate and scroll through
'*************************************
Local i, upArrow,dnArrow,pageUp,pageDn,LinesPerScreen,KeyText$
Local EndKey, HomeKey, EscKey

upArrow=128
dnArrow=129
HomeKey=134
EndKey =135
pageUp =136
pageDn =137
EscKey =27
MsgPos =62*6

LinesPerScreen=MM.VRes/12-3
KeyText$="Control: Cursor up/down, Page up/down, End, Home, Esc"

Cls
Font #1,,1
Print @(60,0)" <EEPROM AT24C32 DUMP &H"+Hex$(I2CPROM)+"> "
Font #1,,0
For i = 0 To LinesPerScreen
eedump i *16,16
Next i
i=i-1

Print @((MM.HRes-Len(KeyText$)*6)/2,MM.VRes-12) KeyText$

Do
Print @(MsgPos,0) Space$(20) 'clear error message

Do:AscKey=KeyDown:Loop While AscKey=0

If AscKey = dnArrow And i < 255 Then
scrollup
i=i+1
Print @(0,MM.VRes-24);:eedump i *16,16
Else
If AscKey = dnArrow Then
PrintFlash MsgPos," UPPER LIMIT! "
EndIf
EndIf

If AscKey = upArrow And i > LinesPerScreen Then
scrolldn
i=i-1
Print @(0,12);:eedump i *16-33*16,16
Else
If AscKey = upArrow Then
PrintFlash MsgPos," LOWER LIMIT! "
EndIf
EndIf

If AscKey = PageUp And i > LinesPerScreen Then
offset=i-LinesPerScreen*2-1
If offset<0 Then offset = 0
For i = offset To offset+LinesPerScreen
eedump i *16,16
Next i
i=i-1
Else
If AscKey = PageUp Then
PrintFlash MsgPos," LOWER LIMIT! "
EndIf
EndIf

If AscKey = PageDn And i < 255 Then
offset=i+1
If offset+LinesPerScreen>=255 Then offset = 255-LinesPerScreen
For i = offset To offset+LinesPerScreen
eedump i *16,16
Next i
i=i-1
Else
If AscKey = PageDn Then
PrintFlash MsgPos," UPPER LIMIT! "
EndIf
EndIf

If AscKey = HomeKey And i > LinesPerScreen Then
offset=0
For i = offset To offset+LinesPerScreen
eedump i *16,16
Next i
i=i-1
EndIf

If AscKey = EndKey And i < 255-LinesPerScreen Then
offset=255-LinesPerScreen
For i = offset To offset+LinesPerScreen
eedump i *16,16
Next i
i=i-1
EndIf

If AscKey = EscKey Then Exit

Loop
End Sub
'********************* END eeMon *************************



Sub PrintFlash MsgPos,text$
Local i,revers
i = 4: revers = 0
Do
Font #1,,revers
Print @(MsgPos,0) text$
Pause 200
i=i-1
revers=Not revers
Loop While i>0
Font #1,,0
End Sub


Sub scrollup '36*12 Zeilen = 432
'blit x,y,x1,y1,w,h
BLIT 0,24,0,12,MM.HRes,MM.VRes-36
End Sub

Sub scrolldn
BLIT 0,12,0,24,MM.HRes,MM.VRes-36
End Sub


' wait for any key, returns the keyChar$
Function GetKey$ prompt$
Local k
k = KeyDown
If prompt$<>"" Then Print prompt$
Do:GetKey$=Inkey$:Loop While GetKey$=""
End Function


' wait for any key
Sub WaitKey prompt$
Local k
k=KeyDown
If prompt$<>"" Then Print prompt$
Do:Loop While Inkey$=""
End Sub


'-----------------------------------------------------------

'**********************************
' I2C-Scanner
' scans I2C-Bus for I2C addresses
' MM-Basic 4.5 / Maximite/Duinomite
' by twofingers 21-08-2014 on TBS
'**********************************
Sub I2CScanner
Local found, i

found=0
Cls
Print "I2C-Scanner from Adr. 8-119":Print:Print

For i = &h08 To &h77 ' gueltige I2C-Adressen von 8-119
I2C open 100, 1000 ' i2c enable 100kHz, 1000ms timeout
I2C read i, 0, 1, temp

Print i;">"; MM.I2C " ";
If MM.I2C = 0 Then
Print:Print:Print "Found I2C-Address at "; i; " ("dec2hex$(i)+")"
found=1
EndIf
I2C close ' i2c disable

Next i
If found = 0 Then Print:Print:Print "NO I2C-Address found!"
End Sub

Function dec2hex$(number) ' uused by I2C-Scanner
dec2hex$ = "&H"+ Hex$(number)
End Function

'***********************************************************

This code should replace the older EEPROM0.5.zip.

edit (Update)
Download:
2014-09-10_073035_EEPRO82.zip

Any corrections, suggestions and critics are welcome!
2,12,17,30 and 73!!

Michael Edited by twofingers 2014-09-11
 
centrex

Guru

Joined: 13/11/2011
Location: Australia
Posts: 320
Posted: 03:39pm 01 Nov 2014
Copy link to clipboard 
Print this post

I have been able to get twofingers code to read and write to the eeprom that comes with rtc modules, as noted in the code it was written for a MaxiMite.
I have the code running on a 28pin 150 using 4.5D, some of the origoal functions have not been included as it specific commands formatting commands for the MaxiMite.
Anyone who wants to tidy it up further is welcome, I have yet to try it on a 170 and 4.6(beta).
Regards
Cliff




'********************************************
'********************************************
' EEPROM-Library v0.81
' -----------------------------------------
' by twofingers 09-2014 at TBS
' with parts from MMBasic Library
' for AT24V32 (32kBit EEPROM)
' at DS3231 or DS1307 RTC-Modules etc.
' MMBasic 4.5 for Maximite/Duinomite

'Modified by Centrex to run on uMITE
'28 pin 150 MMBasic 4.5D (11-2014)
'The origonal here
' http://www.thebackshed.com/Forum/forum_posts.asp?TID=6917&PN =9
'------------------------------------------
' eeWrite$: writes a string to address
' eeRead$ : reads a string from address
' eeGet$ : returns an amount of bytes from address
' eeWriteB: writes 1 byte to address
' eeReadB : reads 1 byte from address
' address may be between 0 to 4095
'
' eeSaveF : writes a 32 bit floatinpoint number
' eeSaveF4: writes a 32 bit floatinpoint number
' at EEPROM addresses divisible by 4
' (faster then eeSaveF)
' eeLoadF : reads a 32 bit floatinpoint number
' back from EEPROM using PEEK and POKE
' needs eeWriteB and eeReadB
'
' eeErase : deletes the whole eeprom w. verify
' this has been remmed out for the time being
'
' eeDump : shows content of eeprom or parts
' eeMon : displays whole eeprom content (scroll)
' remmed out for the time being
'
' I2CPROM (I2C address of your EEPROM)
' must always be defined globally
'-------------------------------------------
'
' Remark: A check for defective Bytes
' has a latency of ~1 sec !!
' Therefore one second should be elapsed after
' each write event and then follow a read/compare,
' if a verify is required.
'
'-------------------------------------------
'
'
' This code may be freely distributed and
' changed. Provided AS IS without any warranty.
' Use it at your own risk.
'********************************************
'********************************************




'#######################################
' Demonstration code
'--------------------
I2CPROM = &H56 'I2CAdress of EEPROM
'if unknown please use the i2CSanner
'or then datasheet



setpin 2,ain ' to test eeSave floating point.
print pin(2) '

'Print "WARNING: THIS DEMO WILL ERASE YOUR EEPROM!":Print
Print "The ERASE and Monitor functions has been remmed out for the time being."

'waitkey"press any key for eeSaveF4 demonstration (or CTRL+C to abort)"
Print "writing analog input)"

For i = 0 To 9 'fill with 32 bit floats (= 4 bytes)
eeSaveF4(i*4,pin(2))
Next i

'waitkey"press any key for eeLoadF (reading back)"

For i = 0 To 9
Print eeLoadF(i*4)
Next i


Print "reading (4096 bytes)... (4s)"
eeDump ' this will dump the whole of the memory
'*********************************************************** ***
'The following require a lot of modification to work on the uMITE
'waitkey"press any key for eeMon"
'eeMon
'waitkey"press any key for eeErase"
'eeErase

print
'waitkey"press any key for eeWrite$ + eeRead$ + eeGet$"
Print "Writing at 0 'THE BACKSHED DEMO'"
eeWrite$ 0,"THE BACKSHED DEMO"

Print:Print "reading back:"

Print
Print eeGet$(0,17)


end ' end DEMO




'**********************************
' Saves a floatingp. var (v) to AT24C32
' similar to eeSaveF but faster
'
' address should be divisible
' by 4 without a remainder
' (page mode roll over trouble)
' Plz obey mmbasic limits for
' floating point numbers otherwise
' U will get a serious memory error!
'**********************************
Sub eeSaveF4 (address, v)
Local fill(6),i,v1
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

If address Mod 4 Then
Print "Error: address should be divisible without a remainder"
End
EndIf

I2C open 100,100 'ENABLE I2C

fill(0)=Int(address/256) 'MSB
fill(1)=address Mod 256 'LSB
For i = 0 To 3: fill(i+2)=Peek(VAR v,i):Next i

I2C write I2CPROM, 0, 6, fill(0)
If MM.I2C <>0 Then Print "Error(1)! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1

I2C close

End Sub


'******************************
Sub eeSaveF (address, v)
' Saves a floatingp. var to AT24C32
'******************************
Local i
For i = 0 To 3
eeWriteB address+i, Peek(VAR v,i)
Next i
End Sub


'******************************
Function eeLoadF (address)
' Load a floatingp. var from AT24C32
' Attention:
' never use this for strings!
'******************************
Local i
For i = 0 To 3
Poke VAR eeLoadF,i, eeReadB(address+i)
Next i
Function Sub


'******************************
Sub eeWriteB (address, byte)
' Write a byte (0-255) to AT24C32
'******************************
Local msb, lsb
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

I2C open 100,100 'ENABLE I2C
msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0, 3, msb, lsb, byte
Pause 1
If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
I2C close
If debug Then Print byte;" written into EEPROM."
End Sub


'******************************
Function eeReadB (address)
' Read a byte (0-255) from AT24C32
'******************************
Local msb, lsb

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

I2C open 100,100 'ENABLE I2C
msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,eeReadB

If MM.I2C <>0 Then Print "ERROR! 1=NAK 2=TIMEOUT"; MM.I2C:eeReadN=-1:End
I2C close

End Function


'******************************
Sub eeWrite$ (address, text$)
' Writes a string to AT24C32
'******************************
Local a,x,msb,lsb,PByte

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

a=0

If Len(text$)+address>4095 Then Print "Error: Address too high!":End

I2C open 100,100 'ENABLE I2C
For x= address To address+Len(text$) 'writes a 0-byte at the end!
a=a+1
msb=Int(x/256) 'MSB
lsb=x Mod 256 'LSB

PByte=0:If a<256 Then PByte=Asc(Mid$(text$,a,1))

I2C write I2CPROM, 0, 3, msb, lsb, PByte
If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1
Next x
I2C close
If debug Then
Print "The string "+Chr$(34);:Print text$;
Print Chr$(34)+" was successfully written at address"address" into EEPROM."
EndIf
End Sub


'******************************
Function eeGet$ (address, amount)
' Returning a amount of bytes from
' address of an AT24C32.
'******************************
Local x,msb,lsb,tmp

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

eeGet$=""

If address>4095 Then Print "Error: Address too high!":End
If amount>255 Then Print "Error: String would be too long!":End

I2C open 100,100 'ENABLE I2C
For x= address To address+amount-1

msb=Int(x/256) 'MSB
lsb=x Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,tmp

If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
eeGet$=eeGet$+Chr$(tmp)

Next x
I2C close
End Function


'******************************
Function eeRead$ (address)
'eeRead$ (address):
' Reads a zero-terminated string from AT24C32
'******************************
Local msb,lsb,tmp
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

eeRead$=""

If address>4094 Then Print "Error: Address too high! (valid: 0-4094)":End

I2C open 100,100 'ENABLE I2C

Do

msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,tmp

If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
If tmp=0 Then Exit 'leave loop
eeRead$=eeRead$+Chr$(tmp)
address=address+1

Loop While Len(eeRead$)=<255

I2C close
Function end
'return


'**************************************
' ERASE WHOLE EEPROM (fill with zero)
' Warning:
' EEPROM has a limited endurance
'**************************************
'Sub eeErase
Local i,K1,eePage,MSB,LSB,xfill(34)
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End
K=&H00 'zero value or something else - K=&HAA, K=&H55 ... K=&HFF
Timer=0

If GetKey$("Ready to erase EEPROM at &H"+Hex$(I2CPROM)+"? <y/n>")<>"y" Then
Print "EEPROM erase aborted!"
End Sub
Else
Print "EEPROM erase confirmed!"
EndIf

For i = 0 To 31: Xfill(i+2)=K: Next i

Print "Erasing ";:vpos=MM.VPos:hpos=MM.HPos
I2C open 100,100 'ENABLE I2C

For eePage = 0 To &H7F '&H7F = 128 Pages a 32 bytes = 4096 bytes
MSB=Int(eePage/8)
LSB=(eePage-MSB*8)*32
Xfill(0)=MSB
Xfill(1)=LSB
I2C write I2CPROM, 0, 34, Xfill(0)
If MM.I2C <>0 Then Print "Error(1)! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1
Print @(HPos+MSB*4,VPos)".";' progressbar
Next eePage

'verify
For eePage = 0 To &H7F '&H7F = 128 * 32 bytes = 4096 bytes
MSB=Int(eePage/8)
LSB=(eePage-MSB*8)*32

I2C write I2CPROM,0,2,MSB,LSB ' set read address
For i=0 To 31
I2C read I2CPROM,0,1,K1
If MM.I2C <>0 Then Print "Error(2)! 1=NAK 2=TIMEOUT"; MM.I2C: End
If K<>K1 Then
Print:Print "Verify error! at"; MSB*256+LSB+i;
Print " Byte: ";K " <>" K1;" - "Bin$(k)" <> "Bin$(K1)
End
EndIf
Next i
Print @(HPos+MSB*4,VPos)":";' progressbar
Next eePage

I2C close
Print:Print "SUCCESS: EEPROM HAS BEEN ERASED!"
Print "Elapsed time: "Timer"ms"
End Sub


'*******************************************
' DUMP EEPROM
' Shows content from AT24C32
' will be used from eeMon or separatly
'
' Syntax:
' eeDump
' Displays whole content of EEPROM, byte 0-4095
'
' eeDump start
' Displays partial content of EEPROM, byte start-4095
'
' eeDump start,amount
' Displays partial content of EEPROM,
' byte start to start+amount
'*******************************************
Sub eeDump start,amount
Local FromByteNr, msb, lsb, i, RTCBUFF(4096)
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

If amount=0 Then amount=4095

FromByteNr=Int(start/16)*16
amount=amount+start-FromByteNr:If amount+FromByteNr>4095 Then amount=4095-FromByteNr

If amount<1 Then
Print "Error: Nothing to display. Amount: " amount,FromByteNr
End Sub
EndIf

amount=(Int((amount+15)/16))*16
' fill buffer
I2C open 100,100 'ENABLE I2C

' reading EEPROM
For i = FromByteNr To FromByteNr+amount
msb=Int(i/256) 'MSB
lsb=i Mod 256 'LSB
I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,RTCBUFF(msb*256+lsb)
If MM.I2C <>0 Then Print "ERROR! 1=NAK 2=TIMEOUT"; MM.I2C,i:End
Next i

I2C close

' display buffer content
For I=FromByteNr To FromByteNr+amount-1 Step 16' 4095 Step 16
Print "Byte ";I;"|| "; 'Format$(I,"%4g");" ||";
For y = 0 To 15
Print Right$("0"+Hex$(RTCBUFF(I+y)),2);"|";
Next y
Print "|";
For y = 0 To 15
If RTCBUFF(I+y) >=32 And RTCBUFF(I+y)<=126 Then ' show ascii only
C$=chr$(RTCBUFF(I+y))
Else
C$=" "
EndIf
Print C$;
Next y
Print "|"
'waitkey"press any key"
Next I

End Sub



'-----------------------------------------------------------

'**********************************
' I2C-Scanner
' scans I2C-Bus for I2C addresses
' MM-Basic 4.5 / Maximite/Duinomite
' by twofingers 21-08-2014 on TBS
'**********************************
sub I2CScanner
Local found, i

found=0

Print "I2C-Scanner from Adr. 8-119":Print:Print

For i = &h08 To &h77 ' gueltige I2C-Adressen von 8-119
I2C open 100, 1000 ' i2c enable 100kHz, 1000ms timeout
I2C read i, 0, 1, temp

Print i;">"; MM.I2C " ";
If MM.I2C = 0 Then
Print:Print:Print "Found I2C-Address at "; i; " ("dec2hex$(i)+")"
found=1
EndIf
I2C close ' i2c disable

Next i
If found = 0 Then Print:Print:Print "NO I2C-Address found!"
End Sub

Function dec2hex$(number) ' uused by I2C-Scanner
dec2hex$ = "&H"+ Hex$(number)
End Function

'***********************************************************
Edited by centrex 2014-11-03
Cliff
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 11:20am 26 Mar 2015
Copy link to clipboard 
Print this post

UPDATE
This (quick & dirty) version should work with mmBasic 4.6 MK2 for AT24V32:

2015-03-26_210818_eeProm_Lib_micromite_mk2_v0.82.zip
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:21pm 26 Mar 2015
Copy link to clipboard 
Print this post

This is excellent!!!! Thank you for this, I will be making good use of this.
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 12:07pm 29 May 2016
Copy link to clipboard 
Print this post

Deleted & moved to another thread.

Accidently posted my long comments against the wrong thread.

Edited by Phil23 2016-05-30
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 12:40pm 24 Aug 2016
Copy link to clipboard 
Print this post

Just been playing about with this.

I've seen a few instances of:-

[Code]WARNING: THIS DEMO WILL ERASE YOUR EEPROM!

The Monitor functions has been remmed out for the time being.
---- This demo requires a VT100 terminal -----

Press any key for eeSaveF4 demonstration (or CTRL+C to abort)
writing 10 Rnd() numbers into the eeProm
Error(1)! 1=NAK 2=TIMEOUT 2
>
[/code]

The Error(1) doesn't always occur at the same place in the demo;
running it repeatedly I get to various stages before it stops.

Maybe a compatibility issue with the newer firmware?
Testing on an E64 with V5.2.

Thanks

Phil
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 02:46pm 24 Aug 2016
Copy link to clipboard 
Print this post

  Phil23 said   Just been playing about with this.

I've seen a few instances of:-

[Code]WARNING: THIS DEMO WILL ERASE YOUR EEPROM!

The Monitor functions has been remmed out for the time being.
---- This demo requires a VT100 terminal -----

Press any key for eeSaveF4 demonstration (or CTRL+C to abort)
writing 10 Rnd() numbers into the eeProm
Error(1)! 1=NAK 2=TIMEOUT 2
>
[/code]

The Error(1) doesn't always occur at the same place in the demo;
running it repeatedly I get to various stages before it stops.

Maybe a compatibility issue with the newer firmware?
Testing on an E64 with V5.2.

Thanks

Phil


Hi Phil,

I just tried the code (eeprom mircomite mk2 v0.82.bas) today more than ten times without any issues (MMBasic V5.1). I would NOT expect to encounter this error with V5.2. The code was originally made for MMBasic 4.6B.

Maybe you can change the value for the PAUSE statement (now=1, up to 5 or 10).

For eePage = 0 To &H7F '&H7F = 128 Pages a 32 bytes = 4096 bytes
MSB=Int(eePage/8)
LSB=(eePage-MSB*8)*32
Xfill(0)=MSB
Xfill(1)=LSB
I2C write I2CPROM, 0, 34, Xfill(0)
If MM.I2C <>0 Then Print "Error(1)! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1
'Print @(HPos+MSB*4,VPos)".";' progressbar
PRINT CHR$(27);"[H";
Print "erasing page: "eePage
Next eePage

Other possible reasons for this timeout issue can be a "bad" connection or chip tolerances.

Regards
Michael
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 05:31pm 24 Aug 2016
Copy link to clipboard 
Print this post

Thanks,

Tried both 5 & 10 in the place of all the Pause 1's.

May have seen a marginal improvement, but the furtherest I got was to erasing Page 24.


Sometimes it still errors at the first step.

Will try another RTC a bit later.

Does seem a little timing dependent maybe.


Phil.
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 11:43am 26 Aug 2016
Copy link to clipboard 
Print this post

Just been testing this a bit further this morning.

Swapped 3 different RTC's on two Different MM's.

First one:-

28 Pin backpack with 5.2;

Ran it about 3 times on each RTC;
Runs faultlessly every time, I can't make it fail.

Second one:-

Explore64 with 5.2;
Same 3 times on each RTC;

Still the same result.

Tried one clock on both 3.3 & 5V & no change.

Also tried various clock speeds on the E64 down to 20MHz but still the same.

Cheers

Phil.

 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8582
Posted: 08:55pm 26 Aug 2016
Copy link to clipboard 
Print this post

Phil

Can you try E64 with 5.1 and let us know the result

Thanks
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 09:56pm 26 Aug 2016
Copy link to clipboard 
Print this post

Hi Peter,

Just flashed it back to 5.1 & ran the code several times.

Still a very similar result; no pattern to at which point it crashes.

No options were set after flashing, chip just in it's default state.

Only tried the one RTC module, but I can't get any to fail on the MX170 V5.2.

Only other feedback is the LCD was still connected but no options configured.

Thanks

Phil.

Edit:-

Will try it on the E100 later.Edited by Phil23 2016-08-28
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8582
Posted: 10:55pm 26 Aug 2016
Copy link to clipboard 
Print this post

Phil

Is this the same E64 you are having SDcard problems with?

Methinks you may have a dry joint or short. NB: I2CCLK and SPIIN are next to each other pins 43 and 44
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1664
Posted: 11:30pm 26 Aug 2016
Copy link to clipboard 
Print this post

Thanks Peter,

And yes it is the one & only.

Plan on swapping it with an E100 in the morning;
ATM it's my main Coms controller & data display.

Will have a close look at it & see what I can see.


Cheers

Phil.

PS. just ran it a dozen or so times.

eeSaveF4 & eeReadF succeed with a high % rate 80 or better.

Reading (4049) has more like a 90% failure rate & always at different counts.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3015
Posted: 07:32am 28 Feb 2017
Copy link to clipboard 
Print this post

When running on an Explore-64 with MMBasic V3.5B, in Centrex's code, both "Function eeGet$ (address, amount)" and "Function eeRead$ (address)" produce this error: "Error: Invalid function definition".

Removing the space before the opening "(" fixes this (thanks, matherp). In addition, eeRead$ terminates with "FUNCTION END" and it should be "END FUNCTION".

For SUB eeWrite$, the "$" should be removed.

With these changes, eeWrite, eeRead$, eeGet$, and I2CScanner work for me to read a 24AT32 eeprom on a DS3132 module (address &H57). I haven't used the other subroutines and functions.

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 09:03am 28 Feb 2017
Copy link to clipboard 
Print this post

Hi Lizby!

Sorry for the trouble!!

I think you should use the updated version

2015-03-26_210818_eeprom_mircomite_mk2_v0.82.zip

from 3/2015.

'********************************************
'********************************************
' EEPROM-Library v0.82
' -----------------------------------------
' by twofingers 09-2014 at TBS
' with parts from MMBasic Library
' for AT24V32 (32kBit EEPROM)
' at DS3231 or DS1307 RTC-Modules etc.
'

'Modified by Centrex to run on uMITE
'28 pin 150 MMBasic 4.5D (11-2014)
'The origonal here
' http://www.thebackshed.com/Forum/forum_posts.asp?TID=6917&PN =9

' Modified (q&d) for MMBasic 4.6B (03-2015) by twofingers@TBS
' needs VT100 emulation for eeDump,
'------------------------------------------
' eeWriteStr: writes a string to address
' eeRead$ : reads a string from address
' eeGet$ : returns an amount of bytes from address
' eeWriteB: writes 1 byte to address
' eeReadB : reads 1 byte from address
' address may be between 0 to 4095
'
' eeSaveF : writes a 32 bit floatinpoint number
' eeSaveF4: writes a 32 bit floatinpoint number
' at EEPROM addresses divisible by 4
' (faster then eeSaveF)
' eeLoadF : reads a 32 bit floatinpoint number
' back from EEPROM using PEEK and POKE
' needs eeWriteB and eeReadB
'
' eeErase : deletes the whole eeprom w. verify
' this has been remmed out for the time being
'
' eeDump : shows content of eeprom or parts
' eeMon : displays whole eeprom content (scroll)
' remmed out for the time being
'
' I2CPROM (I2C address of your EEPROM)
' must always be defined globally
'-------------------------------------------
'
' Remark: A check for defective Bytes
' has a latency of ~1 sec !!
' Therefore one second should be elapsed after
' each write event and then follow a read/compare,
' if a verify is required.
'
'-------------------------------------------
'
'
' This code may be freely distributed and
' changed. Provided AS IS without any warranty.
' Use it at your own risk.
'********************************************
'********************************************

option default Float

'#######################################
' Demonstration code
'--------------------

dim integer I2CPROM = &H57 'I2CAdress of EEPROM
'if unknown please use the i2CSanner
'or the resp. datasheet
dim integer i
dim float rnd_val
dim string Your_Name$, TestStr$

VT100_CLS
Print "WARNING: THIS DEMO WILL ERASE YOUR EEPROM!":Print
Print "The Monitor functions has been remmed out for the time being."
Print "---- This demo requires a VT100 terminal -----"
Print
waitkey"Press any key for eeSaveF4 demonstration (or CTRL+C to abort)"
Print "writing 10 Rnd() numbers into the eeProm"

For i = 0 To 9 'fill with 32 bit floats (= 4 bytes)
rnd_val=rnd 'get a random number
eeSaveF4(i*4,rnd_val)
Print rnd_val
Next i

waitkey "press any key for eeLoadF (reading back)"

For i = 0 To 9
Print eeLoadF(i*4)
Next i

waitkey "press any key"
VT100_CLS
Print "reading (4096 bytes)... (takes about 5s)"

eeDump ' this will dump the whole of the memory
'*********************************************************** ***
'The following require a lot of modification to work on the uMITE
'waitkey"press any key for eeMon"
'eeMon
Print
waitkey"press any key for eeErase"
VT100_CLS
eeErase

print
waitkey"press any key for eeWriteStr + eeRead$ + eeGet$"
VT100_CLS
For i = 1 To 25
a$=a$+"ABCDFEGHIJ"
Next i
A$=A$+"ABCDF" '255 characters
Print "Writing at 256: "A$:Print:Print
eeWriteStr 256,A$
Print:Print "reading back a 256 bytes string: (eeRead$)"
Print eeRead$(256)
Print
Print
LINE INPUT "Your name?: ", Your_Name$
Print "Writing at byte pos 0 'Your name' and 'THE BACKSHED DEMO END'"

TestStr$=Your_Name$+" THE BACKSHED DEMO END"
eeWriteStr 0,TestStr$

VT100_CLS
Print "Dump first 512 bytes:"

eeDump 0,512

Print:waitkey "press any key for reading back: (eeGet$)"
Print eeGet$(len(Your_Name$),len(TestStr$))

end ' end DEMO




'**********************************
' Saves a floatingp. var (v) to AT24C32
' similar to eeSaveF but faster
'
' address should be divisible
' by 4 without a remainder
' (page mode roll over trouble)
' Plz obey mmbasic limits for
' floating point numbers otherwise
' U will get a serious memory error!
'**********************************
Sub eeSaveF4(address, v)
Local fill(6),i,v1
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

If address Mod 4 Then
Print "Error: address should be divisible without a remainder"
End
EndIf

I2C open 100,100 'ENABLE I2C

fill(0)=Int(address/256) 'MSB
fill(1)=address Mod 256 'LSB
For i = 0 To 3: fill(i+2)=Peek(VAR v,i):Next i

I2C write I2CPROM, 0, 6, fill(0)
If MM.I2C <>0 Then Print "Error(1)! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1

I2C close

End Sub


'******************************
Sub eeSaveF(address, v)
' Saves a floatingp. var to AT24C32
'******************************
Local i
For i = 0 To 3
eeWriteB address+i, Peek(VAR v,i)
Next i
End Sub


'******************************
Function eeLoadF(address)
' Load a floatingp. var from AT24C32
' Attention:
' never use this for strings!
'******************************
Local i
For i = 0 To 3
Poke VAR eeLoadF,i, eeReadB(address+i)
Next i
Function Sub


'******************************
Sub eeWriteB(address, byte)
' Write a byte (0-255) to AT24C32
'******************************
Local msb, lsb
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

I2C open 100,100 'ENABLE I2C
msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0, 3, msb, lsb, byte
Pause 1
If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
I2C close
If debug Then Print byte;" written into EEPROM."
End Sub


'******************************
Function eeReadB(address)
' Read a byte (0-255) from AT24C32
'******************************
Local msb, lsb

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

I2C open 100,100 'ENABLE I2C
msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,eeReadB

If MM.I2C <>0 Then Print "ERROR! 1=NAK 2=TIMEOUT"; MM.I2C:eeReadN=-1:End
I2C close

End Function


'******************************
Sub eeWriteStr(address, text$)
' Writes a string to AT24C32
'******************************
Local a,x,msb,lsb,PByte

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

a=0

If Len(text$)+address>4095 Then Print "Error: Address too high!":End

I2C open 100,100 'ENABLE I2C
For x= address To address+Len(text$) 'writes a 0-byte at the end!
a=a+1
msb=Int(x/256) 'MSB
lsb=x Mod 256 'LSB

PByte=0:If a<256 Then PByte=Asc(Mid$(text$,a,1))

I2C write I2CPROM, 0, 3, msb, lsb, PByte
If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1
Next x
I2C close
If debug Then
Print "The string "+Chr$(34);:Print text$;
Print Chr$(34)+" was successfully written at address"address" into EEPROM."
EndIf
End Sub


'******************************
Function eeGet$(address, amount)
' Returning a amount of bytes from
' address of an AT24C32.
'******************************
Local x,msb,lsb,tmp

' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

eeGet$=""

If address>4095 Then Print "Error: Address too high!":End
If amount>255 Then Print "Error: String would be too long!":End

I2C open 100,100 'ENABLE I2C
For x= address To address+amount-1

msb=Int(x/256) 'MSB
lsb=x Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,tmp

If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
eeGet$=eeGet$+Chr$(tmp)

Next x
I2C close
End Function


'******************************
Function eeRead$(address)
'eeRead$ (address):
' Reads a zero-terminated string from AT24C32
'******************************
Local msb,lsb,tmp
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

eeRead$=""

If address>4094 Then Print "Error: Address too high! (valid: 0-4094)":End

I2C open 100,100 'ENABLE I2C

Do

msb=Int(address/256) 'MSB
lsb=address Mod 256 'LSB

I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,tmp

If MM.I2C <>0 Then Print "Error! 1=NAK 2=TIMEOUT"; MM.I2C: End
If tmp=0 Then Exit 'leave loop
eeRead$=eeRead$+Chr$(tmp)
address=address+1

Loop While Len(eeRead$)=<255

I2C close
Function end



'**************************************
' ERASE WHOLE EEPROM (fill with zero)
' Warning:
' EEPROM has a limited endurance
'**************************************
Sub eeErase
Local i,K1,eePage,MSB,LSB,xfill(34)
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End
K=&H00 'zero value or something else - K=&HAA, K=&H55 ... K=&HFF
Timer=0
Print
If GetKey$("Ready to erase EEPROM at &H"+Hex$(I2CPROM)+"? <y/n>")<>"y" Then
Print "EEPROM erase aborted!"
End Sub
Else
Print "EEPROM erase confirmed!"
EndIf

For i = 0 To 31: Xfill(i+2)=K: Next i

Print "Erasing ";:vpos=MM.VPos:hpos=MM.HPos
I2C open 100,100 'ENABLE I2C

For eePage = 0 To &H7F '&H7F = 128 Pages a 32 bytes = 4096 bytes
MSB=Int(eePage/8)
LSB=(eePage-MSB*8)*32
Xfill(0)=MSB
Xfill(1)=LSB
I2C write I2CPROM, 0, 34, Xfill(0)
If MM.I2C <>0 Then Print "Error(1)! 1=NAK 2=TIMEOUT"; MM.I2C: End
Pause 1
'Print @(HPos+MSB*4,VPos)".";' progressbar
PRINT CHR$(27);"[H";
Print "erasing page: "eePage
Next eePage

'verify
For eePage = 0 To &H7F '&H7F = 128 * 32 bytes = 4096 bytes
MSB=Int(eePage/8)
LSB=(eePage-MSB*8)*32

I2C write I2CPROM,0,2,MSB,LSB ' set read address
For i=0 To 31
I2C read I2CPROM,0,1,K1
If MM.I2C <>0 Then Print "Error(2)! 1=NAK 2=TIMEOUT"; MM.I2C: End
If K<>K1 Then
Print:Print "Verify error! at"; MSB*256+LSB+i;
Print " Byte: ";K " <>" K1;" - "Bin$(k)" <> "Bin$(K1)
End
EndIf
Next i
'Print @(HPos+MSB*4,VPos)":";' progressbar
PRINT CHR$(27);"[H";"Erasing page: "eePage" "
Next eePage

I2C close
Print:Print "SUCCESS: EEPROM HAS BEEN ERASED!"
Print "Elapsed time: "Timer"ms"
End Sub


'*******************************************
' DUMP EEPROM
' Shows content from AT24C32
' will be used from eeMon or separatly
'
' Syntax:
' eeDump
' Displays whole content of EEPROM, byte 0-4095
'
' eeDump start
' Displays partial content of EEPROM, byte start-4095
'
' eeDump start,amount
' Displays partial content of EEPROM,
' byte start to start+amount
'*******************************************
Sub eeDump start,amount
Local integer FromByteNr, msb, lsb, i
local float RTCBUFF(4096)
Local string Key$
' I2CPROM=&H57 ' 24C32-DS3231
If I2CPROM = 0 Then Print "Error: Please define I2CPROM globally": End

If amount=0 Then amount=4095

FromByteNr=Int(start/16)*16
amount=amount+start-FromByteNr:If amount+FromByteNr>4095 Then amount=4095-FromByteNr

If amount<1 Then
Print "Error: Nothing to display. Amount: " amount,FromByteNr
End Sub
EndIf

amount=(Int((amount+15)/16))*16
' fill buffer
I2C open 400,100 'ENABLE I2C

' reading EEPROM
For i = FromByteNr To FromByteNr+amount
msb=Int(i/256) 'MSB
lsb=i Mod 256 'LSB
I2C write I2CPROM, 0,2,msb,lsb
I2C read I2CPROM, 0,1,RTCBUFF(msb*256+lsb)
If MM.I2C <>0 Then Print "ERROR! 1=NAK 2=TIMEOUT"; MM.I2C,i:End
Next i

I2C close
Print "Press any key to continue ... (Esc=cancel)"
' display buffer content
For I=FromByteNr To FromByteNr+amount-1 Step 16' 4095 Step 16
Print "Byte ";str$(I,4);" ||";
For y = 0 To 15
Print Right$("0"+Hex$(RTCBUFF(I+y)),2);"|";
Next y
Print "|";
For y = 0 To 15
If RTCBUFF(I+y) >=32 And RTCBUFF(I+y)<=126 Then ' show ascii only
C$=chr$(RTCBUFF(I+y))
Else
C$=" "
EndIf
Print C$;
Next y
Print "|"
'waitkey"press any key"
Do:Key$=inkey$:Loop While Key$=""
if Key$=chr$(27) then
Do:pause 200: Loop While inkey$<>""
exit sub
endif
Next I

End Sub



'-----------------------------------------------------------

'**********************************
' I2C-Scanner
' scans I2C-Bus for I2C addresses
' MM-Basic 4.5 / Maximite/Duinomite
' by twofingers 21-08-2014 on TBS
'**********************************
sub I2CScanner
Local found, i

found=0

Print "I2C-Scanner from Adr. 8-119":Print:Print

For i = &h08 To &h77 ' gueltige I2C-Adressen von 8-119
I2C open 100, 1000 ' i2c enable 100kHz, 1000ms timeout
I2C read i, 0, 1, temp

Print i;">"; MM.I2C " ";
If MM.I2C = 0 Then
Print:Print:Print "Found I2C-Address at "; i; " ("dec2hex$(i)+")"
found=1
EndIf
I2C close ' i2c disable

Next i
If found = 0 Then Print:Print:Print "NO I2C-Address found!"
End Sub

Function dec2hex$(number) ' uused by I2C-Scanner
dec2hex$ = "&H"+ Hex$(number)
End Function

'***********************************************************

' wait for any key
Sub WaitKey prompt$
Local k
k=KeyDown
If prompt$<>"" Then Print prompt$
Do:Loop While Inkey$=""
End Sub

Function GetKey$(prompt$) as string
Local k
k = KeyDown
If prompt$<>"" Then Print prompt$
Do:GetKey$=Inkey$:Loop While GetKey$=""
End Function

sub VT100_CLS ' VT100 escape sequences, thx to redrok
PRINT CHR$(27);"[2J"; ' Clear Screen
PRINT CHR$(27);"[H"; ' Curser to Home
end sub


Best regards
MichaelEdited by twofingers 2017-03-01
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024