Forum > Embedded - AVR
Store string in flash memory
pascalbythree:
Does anybody have a snip procedure in FPC, to store a string in the flash memory?
I need 1 procedure to read it and 1 procedure to write it.
So safe the value for the next boot of the MCU
Using the Attiny841
Greets, Wouter van Wegen, PascalByThree !
dseligo:
Below is how I do it.
This are snippets from larger program, so it won't work out of the box, but you'll get the general idea:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const LCD_Lines = 4; // broj redova na displeju LCD_LineLength = 20; type String10 = String[10]; String16 = String[16]; TLCDString = String[LCD_LineLength]; const opcija_da_ne: array [0 .. 1] of TLCDString = ('Ne', 'Da'); {$ifdef CPUAVR}section '.progmem';{$endif} opcija_nacin_rada: array [0 .. 1] of TLCDString = ('Hladenje', 'Grijanje'); {$ifdef CPUAVR}section '.progmem';{$endif} opcija_meni_kretanje: array [0 .. 1] of TLCDString = ('U krug', 'Fiksno'); {$ifdef CPUAVR}section '.progmem';{$endif} function progmemByte(const address: pointer): byte; assembler; nostackframe;asm movw ZL, r24 lpm r24, Zend; function progmemChar(const address: pointer): char; assembler; nostackframe;asm movw ZL, r24 lpm r24, Zend; procedure TMenu.LoadOptionText(var AText: String; const s_progmem: TLCDString);var len, i: byte;begin len := progmemByte(@s_progmem[0]); AText[0] := chr(len); for i := 1 to len do AText[i] := progmemChar(@s_progmem[i]);end; // get stringvar sValue: TLCDString; LoadOptionText(sValue, @opcija_nacin_rada);
dseligo:
--- Quote from: pascalbythree on November 27, 2023, 12:14:49 pm ---Does anybody have a snip procedure in FPC, to store a string in the flash memory?
I need 1 procedure to read it and 1 procedure to write it.
--- End quote ---
Now I see you also said 'write it'. So you probably mean EEPROM and not Flash memory.
Here are functions to read/write bytes and words, you can easy use them to make it work with string:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure EEPROM_write(Address: Int16; Data: byte);begin while (EECR and (1 shl EEPE)) <> 0 do begin ; end; EEAR := Address; EEDR := Data; EECR := EECR or (1 shl EEMPE); EECR := EECR or (1 shl EEPE);end; procedure EEPROM_writew(Address: Int16; Data: Word);begin EEPROM_write(Address, Data and 255); // zapiĊĦem u little-endian EEPROM_write(Address + 1, Data shr 8);end; function EEPROM_read(Address: Int16): Byte;begin while (EECR and (1 shl EEPE )) <> 0 do begin ; end; EEAR := Address; EECR := EECR or (1 shl EERE); Result := EEDR;end; function EEPROM_readw(Address: Int16): Word;begin Result := EEPROM_read(Address) + EEPROM_read(Address + 1) * 256;end;
pascalbythree:
Thank you, now going to try this working on it for a few days...
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure EEPROM_writew(Address: Int16; Data: Word);
It compiles...
Do you also got a example Value for the address variable ?
dseligo:
--- Quote from: pascalbythree on November 27, 2023, 05:20:01 pm ---Thank you, now going to try this working on it for a few days...
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure EEPROM_writew(Address: Int16; Data: Word);
It compiles...
Do you also got a example Value for the address variable ?
--- End quote ---
ATtiny841 has 512 bytes of EEPROM, so Address can be from 0 to 511. If you use EEPROM_writew that from 0 to 510 because that procedure writes two bytes in a row.
Navigation
[0] Message Index
[#] Next page