Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

WRITE VARIABLE TO FLASH MEMORY OF MICROCONTROLLER

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Tue Jan 06, 2015 8:20 pm    Post subject: WRITE VARIABLE TO FLASH MEMORY OF MICROCONTROLLER Reply with quote

Hi
I want to save a variable to a predefined address of flash memory of microcontroller.

who can i do it?

thanks

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Tue Jan 06, 2015 11:34 pm    Post subject: Reply with quote

The short answer, is no, you cannot write to flash memory from within a running progam in an AVR processor.

But why not use the eeprom ?

That is provided to store 'variables' which will be used after a power cycle. Note that there are still limitations. You cannot write to eeprom an unlimited number of times, so you must use these 'variables' to store only new values needed after a change. System setup 'variables' are often used in this way.

Please explain further what you are trying to do.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Fri Jan 09, 2015 10:19 am    Post subject: Reply with quote

thank you AdrianJ for all respone in froum

eeprom writing time is very long for my program.writing 2 byte take 17ms time to do.
i write another question in 2 weeks ago about it in froum. but i didnt get an exact answer!
according to datasheet writing a byte to eeprom take 3.5ms,so writing 2 byte take 7ms.
but i check writing time in this way:
1-i set a pin in program such as pinb.0
2- writing 2 byte(an integer variable) to eeprom
3-reset the pin in program

i check the timing in osciloscope, it take 17ms!!!!
why????

during writing to eeprom variable,cpu halted (according to help of bascom) my critical interrupt lost and it is very bad.
----------------------------------------------------------------------
i want to use external eeprom like at24c08 to save time.
if i write to eeprom byte by byte the time did not save,because i should use a waitms 5 for complete writing sequence.
during writing to eeprom,at24c08 do not respond to input command.

i can use page write to write a integer immediately without waitms.so i can write up to 8 byte to a page of eeprom,and then wait to complete writing to eeprom by internal circuit of at24c08.the time of writing is not important for me.

now the question is:
who to write a page into at24c08?


thanks
Back to top
View user's profile
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Fri Jan 09, 2015 10:22 am    Post subject: Reply with quote

my code to write a page in at24c08 is here:

Code:
$regfile = "m32def.dat"
$crystal = 4000000
'-------------------------------------------------------------------------------
Open "comd.6:9600,8,n,1" For Output As #1
Print #1 , "start"
'-------------------------------------------------------------------------------
Config Sda = Portd.4
Config Scl = Portd.5
Config I2cdelay = 1
Const Eeread = 161
Const Eewrite = 160
'-------------------------------------------------------------------------------
Config Portd.7 = Output
Tester Alias Portd.7
'-------------------------------------------------------------------------------
Dim Address As Word , Highaddress As Byte , Lowaddress As Byte , Highintvar As Byte , Lowintvar As Byte
Dim Intvar As Integer
Dim J As Integer
Dim I As Integer
'-------------------------------------------------------------------------------
Config Portd.2 = Output
Intr Alias Portd.2
'-------------------------------------------------------------------------------
J = -30500
For I = 0 To 50
  J = J + I
  'Print #1 , J
  Address = I
  Highaddress = High(address)
  Lowaddress = Low(address)
  Highintvar = High(j)
  Lowintvar = Low(j)
  Print #1 , J
  'Print #1 , I
  'Print #1 , Highintvar
  'Print #1 , Lowintvar
  'Print #1 , "------"
  Gosub Writeeepromserial
  Toggle Tester
Next I

Print #1 , "read"

For I = 0 To 50
  Address = I
  Highaddress = High(address)
  Lowaddress = Low(address)
  Gosub Readeepromserial
  'Print #1 , Highintvar
  'Print #1 , Lowintvar
  Intvar = Makeint(lowintvar , Highintvar)
  Print #1 , Intvar
  'Print #1 , "------"
Next I

End
'-------------------------------------------------------------------------------
Writeeepromserial:
I2cstart
I2cwbyte Eewrite
I2cwbyte Highaddress
I2cwbyte Lowaddress
I2crbyte Highintvar
I2crbyte Lowintvar
I2cstop
Waitms 5
Return
'-------------------------------------------------------------------------------
Readeepromserial:
I2cstart
I2cwbyte Eewrite
I2cwbyte Highaddress
I2cwbyte Lowaddress
I2cstart
I2cwbyte Eeread
I2crbyte Highintvar , Nack
I2cstop
Waitms 5
I2cstart
I2cwbyte Eewrite
I2cwbyte Highaddress
I2cwbyte Lowaddress
I2cstart
I2cwbyte Eeread
I2crbyte Lowintvar , Nack
I2cstop
Waitms 5
Return
'-------------------------------------------------------------------------------
 
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Fri Jan 09, 2015 12:31 pm    Post subject: Reply with quote

Hi,

-Your using the software i2c, better use the hardware i2s (twi) it's faster. (you must change your hardware then)
See http://avrhelp.mcselec.com/config_twi.htm

-Your waiting after every write 5ms, that's also not really needed. Most of the eeproms writing faster then described in the datasheet.
To be sure that the data is written and also to be sure that your not waiting to long for nothing you can do ACK polling, look at this:
Code:

Writeeepromserial:
I2cstart
I2cwbyte Eewrite
I2cwbyte Highaddress
I2cwbyte Lowaddress
I2crbyte Highintvar
I2crbyte Lowintvar
I2cstop
 Do                                                         'To replace the waitms5 by polling for ACK
   I2cstart                                                 'generate start
   I2cwbyte Eewrite                                   'slave adsress
   I2cstop
 Loop Until Err = 0
Return

 


-Also writing a complete page to the eeprom will speed up writing.

-In your test loop your printing some things, this also consumes some time.

-Splitting ADDRESS up in high and low bytes can be done faster with the overlay function, same with J (see this nice tutorial from MAK3 about overlay http://mcselec.com/index.php?option=com_content&task=view&id=307&Itemid=57 )
Code:

Dim Address As Word
Dim Lowaddress As Byte At Address Overlay
Dim Highaddress As Byte At Address + 1 Overlay

Dim J As Integer
Dim Lowintvar As Byte At J Overlay
Dim Highintvar As Byte At J + 1 Overlay

'You can reduce the first part of the main loop then to this
J = -30500
For I = 0 To 50
  J = J + I
  'Print #1 , J
  Address = I
  Print #1 , J
  'Print #1 , I
  'Print #1 , Highintvar
  'Print #1 , Lowintvar
  'Print #1 , "------"
  Gosub Writeeepromserial
  Toggle Tester
Next I
 


-You toggle TESTER pin, so it takes 2 loops to go from low to high to low, don't forget that.

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri Jan 09, 2015 1:09 pm    Post subject: Reply with quote

I have crazy and NOT TESTED idea Very Happy

Code:
$sim
$regfile = "m32def.dat"
$crystal = 4000000

Dim A As Integer
Dim B As Eram Integer
Dim C(2) As Eram Byte At B Overlay
Dim Idx As Byte , Help_byte As Byte

Const Must_be_saved = 1                                     'for this test

Do

If A = Must_be_saved Then Idx = 2

'sequent write 2 x 1 Byte (one time in loop takes 3,5ms)
If Idx > 0 Then
 Select Case Idx
  Case 1
   Help_byte = High(a)
   C(idx) = Help_byte
   Decr Idx
  Case 2
   Help_byte = Low(a)
   C(idx) = Help_byte
   Decr Idx
  End Select
End If

Loop
Back to top
View user's profile Visit poster's website
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Fri Jan 09, 2015 8:35 pm    Post subject: internal EEprom? Reply with quote

Perhaps I missed something. Why not write to the internal EEprom?

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Sat Jan 10, 2015 1:20 pm    Post subject: Reply with quote

Hi

Quote:
Perhaps I missed something. Why not write to the internal EEprom?



I write in first post that internal eeprom writing halt the cpu until writing sequence complete.

the main question was who to write a page in at24c external eeprom.

the code i posted,do not return same variable as writed to eeprom!

thanks
Back to top
View user's profile
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 902
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Sat Jan 10, 2015 4:41 pm    Post subject: wait while writing to eeprom Reply with quote

Look up the HELP under ASM Libraries and Add-Ons.
Instead of eeproms you could use Ramtron memory libraries.
Depending on type I2C or SPI.
No wait while writing because they write in real time and usually (look up the datasheet) you will not ever reach the write limit.
Hubert
Back to top
View user's profile
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Sat Jan 10, 2015 7:34 pm    Post subject: Reply with quote

thank you

after reading your post i search in help of bascom and datasheets,so many question added to my main question Rolling Eyes Shocked Sad

refering to bascom help :

Quote:
Ramtron memory chips are as quick as RAM and can be overwritten almost unlimited times.


1- i can not understand main difference of Ramtron memory chips and eeprom!
can Ramtron memory chips save variable like eeprom without power?


Quote:
By using : $lib "fm24c16.lib"

The EEPROM read and write routines from the library will be used instead of the internal EEPROM.
Thus you can still use : Dim BE as ERAM Byte

And you can use READEEPROM and WRITEEEPROM, but instead of using the internal EEPROM, the external I2C EEPROM is used.

The lib is for the FM24C16. It uses I2C/TWI.



at24c08 also use twi, so can i use writeeeprom and readeeprom instruction for writing and reading from at24c08 or no?


thanks
Back to top
View user's profile
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 101
Location: The Netherlands

netherlands.gif
PostPosted: Sun Jan 11, 2015 9:02 am    Post subject: Reply with quote

Hello,

yes they hold the data without power, for example CY14B101I from cypress

Kind regards,
Tiny
Back to top
View user's profile Visit poster's website
hamedhamedi

Bascom Member



Joined: 12 Feb 2012
Posts: 70

PostPosted: Sun Jan 11, 2015 9:32 am    Post subject: Reply with quote

thank you

Atmel doesnt have part like this?

Regards
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum