How to store a Number in flash?

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello 

 

I'm working on a project want to read the 4 MHz calibration byte and store that in the last  home of flash, and then program my atmega 32 AVR using avrdude. 

In a batch file Ive written a code that do that for me . In my C code I have written a code which reads the last home of the flash and store that in OSCCAL. 

when all the lockbits are disabled , my program works properly . But when I Set the lockbits I cant read that home . 

please help me to solve the problem!!!

 

SomeOne suggested me to use bootloader section but i dont know anything about that . does anyOne know the way to use bootloader ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tnt1374 wrote:
when I Set the lockbits I cant read that home

Isn't that the whole point of Lock Bits?

 

Why not use the EEPROM?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I set The lockbits to 0x00 . 

I've heared that EEPROM memory is not reliable because the data may be erased in sudden overvoltages and other events . 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tnt1374 wrote:
is not reliable because the data may be erased in sudden overvoltages and other events
Read what the datasheet actually says about that. The bottom line is that if you enable BOD (as you should anyway) you shouldn't have problems. Having said that, because the default state of EEAR is 0 then if any location might ever get corrupted it is most likely to be location 0. So a lot of people deliberately avoid storing anything in location 0.

 

For the calibration byte that you will put into OSCCAL you probably want to store it in the LAST not the first byte of EEPROM anyway as you want it "out of the way" in case you chose to use other locations in EEPROM for normal program data - so that should be fine.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If you're not using a bootloader, then there's no advantage to using the lockbits modes you have chosen.

 

With your calibration value stored in the last byte of flash, that places it in the BLS, even if you're not using a bootloader.

 

The lock bits responsible for your trouble are BLB11 and BLB12.  Together, they select the BLB1 mode.  You have chosen BLB1 mode 3, which prevents reading flash in the BLS from the application section.

 

A better mode to use is BLB1 mode 2.  This will lift the above restriction.

 

In addition, you should probably change the BLB0 mode from mode 3 to mode 2 as well.  Otherwise, as your application grows during development, any code located in the BLS which tries to read flash located in the application section will fail.

 

The LB mode bits are fine as you have them (mode 3).

 

In summary, use the following lock bit byte:

0b101000  BLB1 BLB0 LB

 

In hexidecimal, that's 0x28.

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]