Programming Flash - EEPROM memory
This Project we will setup ATmega328P Target Bd at 16 MHz and Flash the Blink-AVRdude program to chip.
The program Writes ASCII characters to EEPROM and then blinks a Yellow and Red LED
This Project will explore the Flash and EEPROM memory inside the ATmega328P.
Learn how to display the EEPROM memory and then write to the EEPROM using AVRdude.
Prerequisites: Know how to use AVRdude with a USB programmer. (usbtiny, usbasp, etc.....) avrdude -p m328p -c usbtiny -v | avrdude -p m328p -c usbasp -P usb -v
- Connect USBtiny to ATmega328P Target Board.
- Test the connection to programmer: avrdude -p m328p -c usbtiny -v
- Set Clock Fuse to 16 MHz avrdude -p m328p -c usbtiny -v -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0xFF:m
- Connect Yellow LED with pull down resistor to Pin D13 on ATmega328P Target bd. (Not needed if using Target Bd built in Yellow LED)
- Connect Red LED with pull down resistor to Pin D10 on ATmega328P Target bd.
- Open Arduino IDE and change the following settings: Under the Tools tab
Board: ="Arduino/Genunio Uno" Programmer: = "USBtiny or USBasp"
- Copy-Paste/Load Blink-AVRdude.ino Arduino file. (Do not Flash at this time)
/* Blink-AVRdude.ino Atmega328P Blink Green & Red LEDs. Write to EEPROM memory and use AVRdude Terminal Mode to Read the EEPROM. Terminal mode command: avrdude -c usbtiny -p atmega328p -v -t dump flash 0 2138 dump eeprom 0 128 */ #include <EEPROM.h> #define LED_YELLOW 13 #define LED_RED 10 void setup() { // initialize digital pin 13 as an output. pinMode(LED_YELLOW, OUTPUT); //Green LED set up at output pinMode(LED_RED, OUTPUT); //Red LED set up at output Serial.begin(9600); Serial.println(" Embedded Workshop "; for (int i = 0; i < 255; i++) //Loop Functionto write ASCII characters EEPROM.write(i, i); // Write to EEprom } void loop() { Serial.println(" Embedded Workshop "); Serial.println("Author Microrustyc October 2018"); digitalWrite(LED_YELLOW, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // wait for a second digitalWrite(LED_YELLOW, LOW); // turn the LED off by making the voltage LOW delay(100); // wait for a second digitalWrite(LED_RED, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // wait for a second digitalWrite(LED_RED, LOW); // turn the LED off by making the voltage LOW delay(100); // wait for a second }
- Under the Sketch Tab Select Export compiled Binary
- Under the Sketch Tab Select Show Folder location You should see the following 3 files:
Notice the two additional Hex files. One has a [Bootloader] and the other does not.
Does the ATmega328P Target board need a [Bootloader]?
Copy those two hex files over to the AVRdude directory.
Under the Sketch Tab Select: Upload Using Programmer
Notice the Sketch uses 2138 bytes
Now lets go and look at the memory inside the ATmega328P
- Open AVRdude in the Terminal Mode with the following command: avrdude -p m328p -c usbtiny -t
- Enter the following Terminal commands and experiment with each one.
>>> dump flash 00 2138 >>> dump eeprom 0 128 >>> write eeprom 00 0x20 0x57 0x6f 0x72 0x6b 0x73 0x68 0x6f 0x70 0x20 0x32 0x30 0x31 0x38 0x20 >>> dump eeprom 0 128
Now lets use the erase command and see what happens.
>>> erase >>> dump flash 00 2138 >>> dump eeprom 0 128 >>> write eeprom 00 0x20 0x57 0x6f 0x72 0x6b 0x73 0x68 0x6f 0x70 0x20 0x32 0x30 0x31 0x38 0x20 >>> dump eeprom 0 128
Notice all memory was cleared and the LED's are not blinking.
The Fuse setting did not change and are not affected by the erase command.
- Exit AVRdude Terminal Mode >>> quit
- Flash the ATmega328P board without using the Arduino IDE. Use the following AVRdude command:
avrdude -c usbtiny -p m328p -U flash:w:Blink-AVRdude.ino.standard.hex
- Open AVRdude in the Terminal Mode with the following command: avrdude -p m328p -c usbtiny -t
- Enter the following Terminal commands again and everything has been flashed.
>>> dump flash 00 2138 >>> dump eeprom 0 128 >>> write eeprom 0x80 0x20 0x57 0x6f 0x72 0x6b 0x73 0x68 0x6f 0x70 0x20 0x32 0x30 0x31 0x38 0x20 >>> dump eeprom 0 128
How to use AVRdude Command to Read Flash memory and Write to Disk .
Read Flash memory and save in a hex file to disk. *Enter the following command in the AVRdude avrdude -c usbtiny -p m328p -U flash:r:Blink-AVRdude2.hex:r
*Erase Chip Memory Command avrdude -p m328p -c usbtiny -e Notice all memory was cleared and the LED's are not blinking..
*Write file back to Flash memory avrdude -c usbtiny -p m328p -U flash:w:Blink-AVRdude2.hex Notice the LED's are Blinking again.
Reading EEPROM Memory and file to disk:
avrdude -c usbtiny -p m328p -U eeprom:r:eeprom.hex:i
Writing to EEPROM from disk:
avrdude -c usbtiny -p m328p -U eeprom:w:eeprom.hex:i
This completes Project 2.