writing/reading from flash memory?

I'm trying to read sensor values in from the analog input pins, and store those values in the flash memory on the atmega8. Can anyone show me some simple C code that writes to and reads from the flash memory? I have found some that depends on IAR, but I am just using the Arduino environment on MacOS X, so I would like something I can compile from there.

Thanks for the help - I am very new to this!

If you can fit your data into the EEPROM, that is much easier to deal with. You will want to include <avr/eeprom.h> and then use the eeprom_read_byte() and and eeprom_write_byte() functions contained in it.
http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html

Flash writing is more complicated, but you may need to do it if you need more space. It looks like the AVR bootloader support functions might be a good start there. avr-libc: <avr/boot.h>: Bootloader Support Utilities Alternatively, I have used the guts of the bootcloner which in turn used the guts of the Arduino bootloader ( I think). http://www.arduino.cc/playground/BootCloner/BootCloner

Be aware: There may be an issue with fuse settings that could prevent you from writing the flash from inside the flash. You will want to check the ATmega8 manual on flash writing.

Thanks for the advice. Yes, I do need the greater space on the flash.

I tried to edit the bootcloner example (http://www.arduino.cc/playground/BootCloner/BootCloner), and you're right, I will have to edit the fuse settings. The problem now is that I can't set the fuses correctly.

Here's what I'm running in my loop() -- you'll want to look at the link to see the functions' source:

  Serial.println("Setting Fuses");
  CMD_Write_Fuse_Low(FuseLow);  // #define FuseLow     0xDF
  delay(20);
  CMD_Write_Fuse_High(FuseHigh);  // #define FuseHigh    0xCA
  delay(20);
  CMD_Write_Lock(LockBits);  // #define LockBits    0xEF
  delay(20);

  Read_Fuse();

And I get this out, so the fuses aren't getting programmed:

Setting Fuses Lock Bits: 11111111 Fuse Low: 11111111 Fuse High: 11111111

Is there something else I need to do (erase chip, change lock bits, etc.) to be able to change the fuses? I looked at the atmega8 datasheet at all the lock/fuse bits tables, and I can't see what I'm doing wrong.

Any ideas?

Thanks again.

I steered you wrong with bootcloner, you will need to be using a 2nd Arduino to use that code and interconnect them with the SPI.

The code in the bootloader is for writing into your own flash. And it is the lock bits that may prevent you from writing into your flash. I’m not sure what the default lock bits are. I’ve erased mine during development. But the good news is that if you do need to change your lock bits… you are already familiar with bootcloner.