|
Forum Index : Microcontroller and PC projects : Saving and recovering multiple string variables onto FRAM memory
| Author | Message | ||||
| Johnfd Newbie Joined: 04/08/2023 Location: AustraliaPosts: 9 |
I've not been able to find methods to save multiple variables onto FRAM. As a result I generated the code below. Using ideas from contributors on the forum I was successful in saving a single variable. I was halfway there. The code below relies on a Procedure (sub routine) to save each variable and a Function to recover the saved variables. It will work for a large number of variables. Because I needed to save about 30 strings in the main program there was plenty of room in the MB85RC256 32 kByte FRAM. None of my strings are greater than 20 characters so I just used memory steps of 25 for a total save of <600 bytes. There's no point in saving space. If you really need to use up to 32kB, by knowing the size of each variable you could pass it in Mem%. Maybe this approach could be used if using only a tiny amount of FRAM like that in some RTC devices. The save program passes the variable and the start memory location to a Procedure while the read function is passed the start of memory only. The read determines the end of a variable by looking for a return that was previously added to the string in the save routine. Here's the code, you may want to remove all the comments if space in the PICO is an issue. In this test program I used different different variables for the output for checking only. SETPIN 21,22,I2C 'set the pins for I2C I2C open 100,100 Dim Entry1$ = "First" 'define and set some strings Dim Entry2$ = "Second abcd12345" Dim Entry3$ = "Thirty" Dim Entry4$ = "Four" Dim Out1$ as string 'these normally not used Dim Out2$ as string 'as they would be same as saved vars Dim Out3$ as string Dim Out4$ as string Dim InputV$ as string 'First save variables in start positions set by second param SaveR(Entry1$,1) SaveR(Entry2$,25) SaveR(Entry3$,50) SaveR(Entry4$,75) 'now read variables in start positions set by parameter Out1$ = ReadV(1) Print Out1$ 'Print statements for checking only Out2$ = ReadV(25) Print Out2$ Out3$ = ReadV(50) Print Out3$ Out4$ = ReadV(75) Print Out4$ Sub SaveR(InputV$,mem%) 'Save from memory location mem% MSB=mem%\256:LSB=mem% Mod 256 'calculate 16 bit memory location Temp$=Chr$(MSB) + Chr$(LSB) + InputV$ + Chr$(13) 'Address+string+CR I2C write &B1010000,1,Len(Temp$),Temp$ 'Write address + string End sub 'end of save procedure 'Start of read function Function ReadV(mem%) as string 'read the variable and return the value Str$ = "" MSB=mem%\256:LSB=mem% Mod 256 'Change the 16 bit memory addresss into 2 bytes I2C write &B1010000,1,2,MSB,LSB 'Set start address Do I2C read &B1010000,1,1,bVar% 'read one byte if bVar%<>13 then Str$=Str$+Chr$(bVar%) 'if a return don't add to string If bVar%=13 Then Exit Do 'if a return exit Loop ReadV = Str$ 'assign to the function output End Function End |
||||
| Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 5978 |
Hi John, Good work, Thanks for sharing. The only thing I could improviseer, is making an index file. A file that container the (1,25,50,…) Then you do not need to remember these Numbers. That is especially interesting when there are variables with different size. Volhout PicomiteVGA PETSCII ROBOTS |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 1991 |
Great timing because I was just thinking about FRAM since I saw it on a grblHAL board. ![]() Mikroe has a FRAM Click module Oh, I see there are both SPI and I2C versions. Edited 2026-07-13 21:26 by PhenixRising |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |