View previous topic :: View next topic |
Author |
Message |
snow
Joined: 28 Jun 2005 Posts: 200 Location: Ashburton / Mid Canterbury / New Zealand
|
Posted: Fri Nov 01, 2019 8:02 pm Post subject: Advice on setting up Eram for an array |
|
|
Hi
I've got a project on and it will be needing to store data in the eeprom.
What i need to do is store X and Y data in a memory locations so it can be read back in different orders. EG get data from point 17, X=105, Y=236.
I would like to store the information in order EG point 1 to 255. What would the code to undertake this be??
I haven't done a lot with Arrays and EEproms. Is this the best way to achieve this??
Thanks
Snow
(BASCOM-AVR version : 2.0.8.2 ) |
|
Back to top |
|
|
snow
Joined: 28 Jun 2005 Posts: 200 Location: Ashburton / Mid Canterbury / New Zealand
|
Posted: Fri Nov 01, 2019 9:01 pm Post subject: |
|
|
This is what I've got which works. Is this the recommended way to do this??
Code: |
'-----------------------------------------------------------------------------------------
'name : EEPROM_Testing.bas
'purpose : Writing and reading to EEPROM
'micro : Mega48
'-----------------------------------------------------------------------------------------
$regfile = "m48def.dat" ' specify the used micro
$crystal = 8000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
Declare sub StorePosition(W_Point as byte,PosX as integer ,PosY as integer)
Declare sub ReadPosition (R_Point as byte)
Dim X as integer
Dim Y as integer
Dim Point as byte
'---Main---
X=0
Y=100
For Point = 1 to 10 'Store 10 lots of data into the EEPROM
X=X+1
Y=Y+1
Call StorePosition(Point,X,Y)
Next Point
For Point = 1 to 10 'Read the 10 lots of data back from the EEPROM
Call ReadPosition(Point)
Print "X:";X; " Y:";Y
Next Point
End
'-----------------------------------------------------------
Sub StorePosition(w_Point as byte,posX as integer ,PosY as integer)
Local eeprom_position as byte
eeprom_position = w_Point * 4 ' Each stored position uses 4 bytes of data
eeprom_position=eeprom_position-3 ' Don't use EEPROM location 0 as per instructions. This is not realy needed just wastes the first 3 memory
' locations of the EEPROM if not used
Writeeeprom posX , eeprom_position 'Use a variable as the pointer to eeprom location
Incr eeprom_position 'Skip one byte as the stored data is an integer (2 bytes)
Incr eeprom_position
Writeeeprom PosY , eeprom_position 'Use a variable as the pointer to eeprom location
End Sub
Sub ReadPosition (r_Point as byte)
Local eeprom_position as byte
eeprom_position = r_Point * 4
eeprom_position=eeprom_position-3 ' Set the start read point in eerom
Readeeprom X , eeprom_position ' Use a variable as the pointer to eeprom location
Incr eeprom_position ' Skip one byte as the stored data is an integer (2 bytes)
Incr eeprom_position
Readeeprom Y , eeprom_position ' Use a variable as the pointer to eeprom location
End sub
|
|
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Nov 03, 2019 9:04 am Post subject: |
|
|
I think this can be much easier with ERAM and OVERLAY for some four byte variable.
Code: | $regfile = "m48def.dat" '4KB FLASH, 1KB SRAM, 256B EEPROM
$crystal = 8000000
$baud = 19200
$hwstack = 32
$swstack = 10
$framesize = 40
$sim
'256B/4=64 max cell for four bytes
Dim Myeeprom(64) As Eram Long
Dim Mytype As Long
Dim Myx As Integer At Mytype Overlay
Dim Myy As Integer At Mytype + 2 Overlay
Dim N As Byte
'test write
For N = 1 To 64
Myx = N
Myy = N
Myeeprom(n) = Mytype
Next
'test read back
For N = 1 To 64
Mytype = Myeeprom(n)
Print "X=" ; Myx ; " Y=" ; Myy
Next
End
|
|
|
Back to top |
|
|
snow
Joined: 28 Jun 2005 Posts: 200 Location: Ashburton / Mid Canterbury / New Zealand
|
Posted: Sun Nov 03, 2019 9:50 am Post subject: |
|
|
Thanks for your input. That makes the code a lot shorter.
One question
Quote: |
Dim Myx As Integer At Mytype Overlay
|
Where does the MyType get the address value assigned? When this is sim'd up and the code line 'Myeeprom(n) = Mytype ' is executed
MyType is loaded with the value 65537. Where does this value come from for the overlay address at the start of the EEprom?
Thanks
Snow |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Nov 03, 2019 10:13 am Post subject: |
|
|
I dont know if I understand your question but value you present is quite expected
Value 65537 DEC is 00_01_00_01 HEX. Two times for our Myx=1 and Myy=1. Value of Mytype dont mather cause we watch for two bytes in our order.
I dont know how Bascom handles those adressess for Me but it does and I know I can do this that way.
I can gues that for Myx=2 and Myy=2 value of Mytype will be 131 074
|
|
Back to top |
|
|
|